codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all the numbers present at given level in Pascal 's triangle | Function to find sum of numbers at Lth level in Pascals Triangle ; Driver Code
def summ ( h ) : NEW_LINE INDENT return pow ( 2 , h - 1 ) NEW_LINE DEDENT L = 3 NEW_LINE print ( summ ( L ) ) NEW_LINE
Product of values of all possible non | Function to find product of all elements in all subsets ; Driver Code
def product ( a , n ) : NEW_LINE INDENT ans = 1 NEW_LINE val = pow ( 2 , n - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans *= pow ( a [ i ] , val ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 2 NEW_LINE a = [ 3 , 7 ] NEW_LINE print ( product ( a , n ) ) NEW_LINE
Program to find Nth odd Fibonacci Number | Function to find nth odd fibonacci number ; Driver Code
def oddFib ( n ) : NEW_LINE INDENT n = ( 3 * n + 1 ) // 2 NEW_LINE a = - 1 NEW_LINE b = 1 NEW_LINE c = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return c NEW_LINE DEDENT n = 4 NEW_LINE print ( oddFib ( n ) ) NEW_LINE
Find a pair ( n , r ) in an integer array such that value of nPr is maximum | Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code
def findPair ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT i = 0 NEW_LINE first = - 1 NEW_LINE second = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT print ( " n ▁ = " , first , " and ▁ r ▁ = " , second ) NEW_LINE DEDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE
Sum of values of all possible non | Function to return the required sum ; Find the sum of the array elements ; Every element appears 2 ^ ( n - 1 ) times ; Driver code
def sum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in arr : NEW_LINE INDENT sum += i NEW_LINE DEDENT sum = sum * pow ( 2 , n - 1 ) NEW_LINE return sum NEW_LINE DEDENT arr = [ 2 , 1 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sum ( arr , n ) ) NEW_LINE
Printing the Triangle Pattern using last term N | Python3 code for printing the Triangle Pattern using last term N ; Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Code
from math import sqrt NEW_LINE def triangle ( n ) : NEW_LINE INDENT k = 2 * n - 2 ; NEW_LINE ch = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) ; NEW_LINE DEDENT k = k - 1 ; NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT print ( ch , end = " ▁ " ) ; NEW_LINE ch += 1 ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def maxHeight ( n ) : NEW_LINE INDENT ans = ( sqrt ( 1 + 8.0 * n ) - 1 ) // 2 ; NEW_LINE return int ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; NEW_LINE triangle ( maxHeight ( N ) ) ; NEW_LINE DEDENT
Number of ways in which N can be represented as the sum of two positive integers | Function to return the number of distinct ways to represent n as the sum of two integers ; Driver code
def ways ( n ) : NEW_LINE INDENT return n // 2 NEW_LINE DEDENT n = 2 NEW_LINE print ( ways ( n ) ) NEW_LINE
Number of ways to erase exactly one element in the Binary Array to make XOR zero | Function to find the number of ways ; Calculate the number of 1 ' s ▁ and ▁ 0' s ; Considering the 4 cases ; Driver code
def no_of_ways ( a , n ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT DEDENT if ( count_1 % 2 == 0 ) : NEW_LINE INDENT return count_0 NEW_LINE DEDENT else : NEW_LINE INDENT return count_1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE a1 = [ 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a1 , n ) ) NEW_LINE n = 5 NEW_LINE a2 = [ 1 , 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a2 , n ) ) NEW_LINE n = 5 NEW_LINE a3 = [ 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a3 , n ) ) NEW_LINE n = 6 NEW_LINE a4 = [ 1 , 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a4 , n ) ) NEW_LINE DEDENT
Minimum absolute difference between N and any power of 2 | Python3 implementation of the approach ; Function to return the highest power of 2 less than or equal to n ; Function to return the smallest power of 2 greater than or equal to n ; Function that returns the minimum absolute difference between n and any power of 2 ; Driver code
from math import log NEW_LINE def prevPowerof2 ( n ) : NEW_LINE INDENT p = int ( log ( n ) ) NEW_LINE return pow ( 2 , p ) NEW_LINE DEDENT def nextPowerOf2 ( n ) : NEW_LINE INDENT p = 1 NEW_LINE if ( n and ( n & ( n - 1 ) ) == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT while ( p < n ) : NEW_LINE INDENT p <<= 1 NEW_LINE DEDENT return p NEW_LINE DEDENT def minDiff ( n ) : NEW_LINE INDENT low = prevPowerof2 ( n ) NEW_LINE high = nextPowerOf2 ( n ) NEW_LINE return min ( n - low , high - n ) NEW_LINE DEDENT n = 6 NEW_LINE print ( minDiff ( n ) ) NEW_LINE
Maximum possible number with the given operation | Function to return the maximum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits greater than or equal to 5 need not to be changed as changing them will lead to a smaller number ; The resulting integer cannot have leading zero ; Driver code
def maxInt ( string ) : NEW_LINE INDENT string2 = " " NEW_LINE for i in range ( 0 , len ( string ) ) : NEW_LINE INDENT if ( string [ i ] < '5' ) : NEW_LINE INDENT string2 += str ( ( ord ( '9' ) - ord ( string [ i ] ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT string2 += str ( string [ i ] ) NEW_LINE DEDENT DEDENT if ( string2 [ 0 ] == '0' ) : NEW_LINE INDENT string2 [ 0 ] = '9' NEW_LINE DEDENT return string2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = "42" NEW_LINE print ( maxInt ( string ) ) NEW_LINE DEDENT
Find the ratio of number of elements in two Arrays from their individual and combined average | Python3 program to Find the Ratio of number of Elements in two Arrays from their individual and combined Average ; function to find the ratio of number of array elements ; calculating GCD of them ; make neumarator and denominator coprime ; Driver Code
from math import gcd NEW_LINE def FindRatio ( a , b , c ) : NEW_LINE INDENT up = abs ( b - c ) NEW_LINE down = abs ( c - a ) NEW_LINE g = gcd ( up , down ) NEW_LINE up //= g NEW_LINE down //= g NEW_LINE print ( up , " : " , down ) NEW_LINE DEDENT a = 4 NEW_LINE b = 10 NEW_LINE c = 6 NEW_LINE FindRatio ( a , b , c ) NEW_LINE
Maximum distance between two 1 's in Binary representation of N | Python3 program to find the Maximum distance between two 1 's in Binary representation of N ; Compute the binary representation ; if N is a power of 2 then return - 1 ; else find the distance between the first position of 1 and last position of 1 ; Driver code
def longest_gap ( N ) : NEW_LINE INDENT distance = 0 NEW_LINE count = 0 NEW_LINE first_1 = - 1 NEW_LINE last_1 = - 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE r = N & 1 NEW_LINE if ( r == 1 ) : NEW_LINE INDENT if first_1 == - 1 : NEW_LINE INDENT first_1 = count NEW_LINE DEDENT else : NEW_LINE INDENT first_1 = first_1 NEW_LINE DEDENT last_1 = count NEW_LINE DEDENT N = N // 2 NEW_LINE DEDENT if ( last_1 <= first_1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT distance = last_1 - first_1 - 1 NEW_LINE return distance NEW_LINE DEDENT DEDENT N = 131 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 8 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 17 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 33 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE
Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code
def isPossible ( x , y , k ) : NEW_LINE INDENT minMoves = abs ( x ) + abs ( y ) NEW_LINE if ( k >= minMoves and ( k - minMoves ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT x = 5 NEW_LINE y = 8 NEW_LINE k = 20 NEW_LINE if ( isPossible ( x , y , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check whether N is a Factorion or not | Python3 implementation of the approach ; Function that returns true if n is a Factorion ; fact [ i ] will store i ! ; A copy of the given integer ; To store the sum of factorials of the digits of n ; Get the last digit ; Add the factorial of the current digit to the sum ; Remove the last digit ; Driver code
MAX = 10 NEW_LINE def isFactorion ( n ) : NEW_LINE INDENT fact = [ 0 ] * MAX NEW_LINE fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , MAX ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] NEW_LINE DEDENT org = n NEW_LINE sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT d = n % 10 NEW_LINE sum += fact [ d ] NEW_LINE n = n // 10 NEW_LINE DEDENT if ( sum == org ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 40585 NEW_LINE if ( isFactorion ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the possible permutation of the bits of N | Python3 implementation of the approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code
TOTAL_BITS = 32 ; NEW_LINE def isPossible ( n ) : NEW_LINE INDENT cnt = bin ( n ) . count ( '1' ) ; NEW_LINE if ( cnt == TOTAL_BITS // 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 524280 ; NEW_LINE if ( isPossible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Check if two Integer are anagrams of each other | Python3 implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code
TEN = 10 NEW_LINE def updateFreq ( n , freq ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT digit = n % TEN NEW_LINE freq [ digit ] += 1 NEW_LINE n //= TEN NEW_LINE DEDENT DEDENT def areAnagrams ( a , b ) : NEW_LINE INDENT freqA = [ 0 ] * TEN NEW_LINE freqB = [ 0 ] * TEN NEW_LINE updateFreq ( a , freqA ) NEW_LINE updateFreq ( b , freqB ) NEW_LINE for i in range ( TEN ) : NEW_LINE INDENT if ( freqA [ i ] != freqB [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = 240 NEW_LINE b = 204 NEW_LINE if ( areAnagrams ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible | Function to find the permutation of the first N natural numbers such that the sum of ( i % Pi ) is maximum possible and return the maximum sum ; Driver code ; Function call
def Max_Sum ( n ) : NEW_LINE INDENT return ( n * ( n - 1 ) ) // 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; NEW_LINE print ( Max_Sum ( n ) ) ; NEW_LINE DEDENT
Check if the number formed by the last digits of N numbers is divisible by 10 or not | Function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; Last digit of the last element ; Number formed will be divisible by 10 ; Driver code
def isDivisible ( arr , n ) : NEW_LINE INDENT lastDigit = arr [ n - 1 ] % 10 ; NEW_LINE if ( lastDigit == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 65 , 46 , 37 , 99 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( isDivisible ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Count of Multiples of A , B or C less than or equal to N | Function to return the gcd of a and b ; Function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; Calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def lcm ( x , y ) : NEW_LINE INDENT return ( x * y ) // gcd ( x , y ) NEW_LINE DEDENT def divTermCount ( a , b , c , num ) : NEW_LINE INDENT return ( num // a + num // b + num // c - num // lcm ( a , b ) - num // lcm ( c , b ) - num // lcm ( a , c ) + num // ( lcm ( lcm ( a , b ) , c ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 7 ; b = 3 ; c = 5 ; n = 100 ; NEW_LINE print ( divTermCount ( a , b , c , n ) ) ; NEW_LINE DEDENT
Array containing power of 2 whose XOR and Sum of elements equals X | Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array
def getArray ( n ) : NEW_LINE INDENT ans = [ ] ; NEW_LINE p2 = 1 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT ans . append ( p2 ) ; NEW_LINE DEDENT n >>= 1 ; NEW_LINE p2 *= 2 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 ; NEW_LINE ans = getArray ( n ) ; NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT
Find out the correct position of the ball after shuffling | Python3 implementation of the above approach ; Function to generate the index of the glass containing the ball ; Change the index ; Change the index ; Print the index ; Driver Code ; Storing all the shuffle operation
M = 3 ; N = 2 ; NEW_LINE def getIndex ( n , shuffle ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT if ( shuffle [ i ] [ 0 ] == n ) : NEW_LINE INDENT n = shuffle [ i ] [ 1 ] ; NEW_LINE DEDENT elif ( shuffle [ i ] [ 1 ] == n ) : NEW_LINE INDENT n = shuffle [ i ] [ 0 ] ; NEW_LINE DEDENT DEDENT print ( n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE shuffle = [ [ 3 , 1 ] , [ 2 , 1 ] , [ 1 , 2 ] ] ; NEW_LINE getIndex ( n , shuffle ) ; NEW_LINE DEDENT
Count the number of subsequences of length k having equal LCM and HCF | Returns factorial of n ; Returns nCr for the given values of r and n ; Map to store the frequencies of each elements ; Loop to store the frequencies of elements in the map ; Using nCR formula to calculate the number of subsequences of a given length ; Driver Code ; Function calling
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return fact ( n ) // ( fact ( r ) * fact ( n - r ) ) NEW_LINE DEDENT def number_of_subsequences ( arr , k , n ) : NEW_LINE INDENT s = 0 NEW_LINE m = dict ( ) NEW_LINE for i in arr : NEW_LINE INDENT m [ i ] = m . get ( i , 0 ) + 1 NEW_LINE DEDENT for j in m : NEW_LINE INDENT s = s + nCr ( m [ j ] , k ) NEW_LINE DEDENT return s NEW_LINE DEDENT arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( number_of_subsequences ( arr , k , n ) ) NEW_LINE
Find the sum of all possible pairs in an array of N elements | Function to return the sum of the elements of all possible pairs from the array ; To store the required sum ; For every element of the array ; It appears ( 2 * n ) times ; Driver code
def sumPairs ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( arr [ i ] * ( 2 * n ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( sumPairs ( arr , n ) ) ; NEW_LINE DEDENT
Minimum sum obtained from groups of four elements from the given array | Function to return the minimum required sum ; To store the required sum ; Sort the array in descending order ; The indices which give 0 or 1 as the remainder when divided by 4 will be the maximum two elements of the group ; Driver code
def minSum ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE arr . sort ( reverse = True ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 4 < 2 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 10 , 2 , 2 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( minSum ( arr , n ) ) ; NEW_LINE DEDENT
Count of triples ( A , B , C ) where A * C is greater than B * B | function to return the count of the valid triplets ; Driver Code ; function calling
def countTriplets ( A , B , C ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , A + 1 ) : NEW_LINE INDENT for j in range ( 1 , B + 1 ) : NEW_LINE INDENT for k in range ( 1 , C + 1 ) : NEW_LINE INDENT if ( i * k > j * j ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT A = 3 NEW_LINE B = 2 NEW_LINE C = 2 NEW_LINE print ( countTriplets ( A , B , C ) ) NEW_LINE
Count of triples ( A , B , C ) where A * C is greater than B * B | Counts the number of triplets for a given value of b ; Count all triples in which a = i ; Smallest value j such that i * j > B2 ; Count all ( i , B2 , x ) such that x >= j ; count all ( x , B2 , y ) such that x >= j this counts all such triples in which a >= j ; As all triples with a >= j have been counted reduce A to j - 1. ; Counts the number of triples that satisfy the given constraints ; GetCount of triples in which b = i ; Driver Code ; Function calling
def getCount ( A , B2 , C ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while ( i < A ) : NEW_LINE INDENT j = ( B2 // i ) + 1 NEW_LINE if ( C >= j ) : NEW_LINE INDENT count = count + C - j + 1 NEW_LINE DEDENT if ( A >= j and C >= i ) : NEW_LINE INDENT count = count + ( C - i + 1 ) * ( A - j + 1 ) NEW_LINE DEDENT if ( A >= j ) : NEW_LINE INDENT A = j - 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countTriplets ( A , B , C ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , B + 1 ) : NEW_LINE INDENT ans = ( ans + getCount ( A , i * i , C ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT A = 3 NEW_LINE B = 2 NEW_LINE C = 2 NEW_LINE print ( countTriplets ( A , B , C ) ) NEW_LINE
Summation of floor of harmonic progression | Python3 implementation of the approach ; Function to return the summation of the given harmonic series ; To store the summation ; Floor of sqrt ( n ) ; Summation of floor ( n / i ) ; From the formula ; Driver code
from math import floor , sqrt , ceil NEW_LINE def getSum ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE k = ( n ) ** ( .5 ) NEW_LINE for i in range ( 1 , floor ( k ) + 1 ) : NEW_LINE INDENT summ += floor ( n / i ) NEW_LINE DEDENT summ *= 2 NEW_LINE summ -= pow ( floor ( k ) , 2 ) NEW_LINE return summ NEW_LINE DEDENT n = 5 NEW_LINE print ( getSum ( n ) ) NEW_LINE
Count of distinct remainders when N is divided by all the numbers from the range [ 1 , N ] | Function to return the count of distinct remainders that can be obtained when n is divided by every element from the range [ 1 , n ] ; If n is even ; If n is odd ; Driver code
def distinctRemainders ( n ) : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT return n // 2 NEW_LINE DEDENT return ( ( n // 2 ) + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( distinctRemainders ( n ) ) NEW_LINE DEDENT
Count total unset bits in all the numbers from 1 to N | Function to return the count of unset bits in the binary representation of all the numbers from 1 to n ; To store the count of unset bits ; For every integer from the range [ 1 , n ] ; A copy of the current integer ; Count of unset bits in the current integer ; If current bit is unset ; Driver code
def countUnsetBits ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = i ; NEW_LINE while ( temp ) : NEW_LINE INDENT if ( temp % 2 == 0 ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT temp = temp // 2 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( countUnsetBits ( n ) ) ; NEW_LINE DEDENT
Find if a degree sequence can form a simple graph | Havel | Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver code
def graphExists ( a ) : NEW_LINE INDENT while True : NEW_LINE INDENT a = sorted ( a , reverse = True ) NEW_LINE if a [ 0 ] == 0 and a [ len ( a ) - 1 ] == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT v = a [ 0 ] NEW_LINE a = a [ 1 : ] NEW_LINE if v > len ( a ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( v ) : NEW_LINE INDENT a [ i ] -= 1 NEW_LINE if a [ i ] < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT a = [ 3 , 3 , 3 , 3 ] NEW_LINE if ( graphExists ( a ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Number of sub arrays with negative product | Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Driver code
def negProdSubArr ( arr , n ) : NEW_LINE INDENT positive = 1 NEW_LINE negative = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = - 1 NEW_LINE DEDENT if ( i > 0 ) : NEW_LINE INDENT arr [ i ] *= arr [ i - 1 ] NEW_LINE DEDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT positive += 1 NEW_LINE DEDENT else : NEW_LINE INDENT negative += 1 NEW_LINE DEDENT DEDENT return ( positive * negative ) NEW_LINE DEDENT arr = [ 5 , - 4 , - 3 , 2 , - 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( negProdSubArr ( arr , n ) ) NEW_LINE