codeparrot/xlcost-text-to-code huggingface dataset:
Count N | Function to find the number of arrays following the given condition ; Initialize answer ; Calculate nPm ; Print ans ; Driver Code ; Given N and M ; Function Call
def noOfArraysPossible ( N , M ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans = ans * ( M - i ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE M = 3 NEW_LINE noOfArraysPossible ( N , M ) NEW_LINE DEDENT
Permutations of an array having sum of Bitwise AND of adjacent elements at least K | Python3 program for the above approach ; Function to print all permutations of arr [ ] such that the sum of Bitwise AND of all adjacent element is at least K ; To check if any permutation exists or not ; Sort the given array ; Find all possible permutations ; Stores the sum of bitwise AND of adjacent elements of the current permutation ; Traverse the current permutation of arr [ ] ; Update the sum ; If the sum is at least K , then print the current permutation ; Set the flag variable ; Print the current permutation ; If flag is unset , then print - 1 ; Driver Code ; Function Call
def next_permutation ( a ) : NEW_LINE INDENT for i in reversed ( range ( len ( a ) - 1 ) ) : NEW_LINE INDENT if a [ i ] < a [ i + 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT j = next ( j for j in reversed ( range ( i + 1 , len ( a ) ) ) if a [ i ] < a [ j ] ) NEW_LINE a [ i ] , a [ j ] = a [ j ] , a [ i ] NEW_LINE a [ i + 1 : ] = reversed ( a [ i + 1 : ] ) NEW_LINE return True NEW_LINE DEDENT def printPermutation ( arr , n , k ) : NEW_LINE INDENT flag = False NEW_LINE arr . sort ( ) NEW_LINE while True : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT sum += arr [ i ] & arr [ i + 1 ] NEW_LINE DEDENT if ( sum >= k ) : NEW_LINE INDENT flag = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if ( next_permutation ( arr ) == False ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE K = 8 NEW_LINE N = len ( arr ) NEW_LINE printPermutation ( arr , N , K ) NEW_LINE
Positive integers up to N that are not present in given Array | Function to find positive integers from 1 to N that are not present in the array ; Declare bitset ; Iterate from 0 to M - 1 ; Iterate from 0 to n - 1 ; Iterate from bset . _Find_first ( ) to bset . size ( ) - 1 ; Driver Code
def findMissingNumbers ( arr , n ) : NEW_LINE INDENT M = 15 NEW_LINE bset = [ 0 ] * M NEW_LINE for i in range ( M ) : NEW_LINE INDENT bset [ i ] = i NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT bset [ arr [ i ] - 1 ] = 0 NEW_LINE DEDENT bset = [ i for i in bset if i != 0 ] NEW_LINE for i in range ( len ( bset ) ) : NEW_LINE INDENT if ( bset [ i ] + 1 > n ) : NEW_LINE INDENT break NEW_LINE DEDENT print ( bset [ i ] + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE findMissingNumbers ( arr , n ) NEW_LINE DEDENT
Sum of the first N terms of XOR Fibonacci series | Function to calculate the sum of the first N terms of XOR Fibonacci Series ; Base case ; Stores the sum of the first N terms ; Iterate from [ 0 , n - 3 ] ; Store XOR of last 2 elements ; Update sum ; Update the first element ; Update the second element ; Print the final sum ; Driver code ; Function call
def findSum ( a , b , N ) : NEW_LINE INDENT if N == 1 : NEW_LINE INDENT print ( a ) NEW_LINE return NEW_LINE DEDENT s = a + b NEW_LINE for i in range ( 0 , N - 2 ) : NEW_LINE INDENT x = a ^ b NEW_LINE s += x NEW_LINE a = b NEW_LINE b = x NEW_LINE DEDENT print ( s ) NEW_LINE return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 2 NEW_LINE b = 5 NEW_LINE N = 8 NEW_LINE findSum ( a , b , N ) NEW_LINE DEDENT
Sum of the first N terms of XOR Fibonacci series | Function to calculate sum of the first N terms of XOR Fibonacci Series ; Store the sum of first n terms ; Store xor of a and b ; Case 1 : If n is divisible by 3 ; Case 2 : If n % 3 leaves remainder 1 ; Case 3 : If n % 3 leaves remainder 2 on division by 3 ; Print the final sum ; Driver code ; Function call
def findSum ( a , b , N ) : NEW_LINE INDENT sum = 0 NEW_LINE x = a ^ b NEW_LINE if N % 3 == 0 : NEW_LINE INDENT sum = ( N // 3 ) * ( a + b + x ) NEW_LINE DEDENT elif N % 3 == 1 : NEW_LINE INDENT sum = ( N // 3 ) * ( a + b + x ) + a NEW_LINE DEDENT else : NEW_LINE INDENT sum = ( N // 3 ) * ( a + b + x ) + a + b NEW_LINE DEDENT print ( sum ) NEW_LINE return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 2 NEW_LINE b = 5 NEW_LINE N = 8 NEW_LINE findSum ( a , b , N ) NEW_LINE DEDENT
Length of the longest subarray whose Bitwise XOR is K | Function to find the length of the longest subarray whose bitwise XOR is equal to K ; Stores prefix XOR of the array ; Stores length of longest subarray having bitwise XOR equal to K ; Stores index of prefix XOR of the array ; Insert 0 into the map ; Traverse the array ; Update prefixXOR ; If ( prefixXOR ^ K ) present in the map ; Update maxLen ; If prefixXOR not present in the Map ; Insert prefixXOR into the map ; Driver Code
def LongestLenXORK ( arr , N , K ) : NEW_LINE INDENT prefixXOR = 0 NEW_LINE maxLen = 0 NEW_LINE mp = { } NEW_LINE mp [ 0 ] = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT prefixXOR ^= arr [ i ] NEW_LINE if ( prefixXOR ^ K ) in mp : NEW_LINE INDENT maxLen = max ( maxLen , ( i - mp [ prefixXOR ^ K ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT mp [ prefixXOR ] = i NEW_LINE DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 7 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE print ( LongestLenXORK ( arr , N , K ) ) NEW_LINE DEDENT
Two Dimensional Difference Array | Python3 Program to implement the above approach ; Function to initialize the difference array ; Function to add k to the specified submatrix ( r1 , c1 ) to ( r2 , c2 ) ; Function to print the modified array ; Function to perform the given queries ; Difference array ; Function to initialize the difference array ; Count of queries ; Perform Queries ; Driver Code ; Given Matrix ; Given Queries
N = 3 NEW_LINE M = 3 NEW_LINE def intializeDiff ( D , A ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT D [ i ] [ 0 ] = A [ i ] [ 0 ] ; NEW_LINE D [ i ] [ M ] = 0 ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT D [ i ] [ j ] = A [ i ] [ j ] - A [ i ] [ j - 1 ] ; NEW_LINE DEDENT DEDENT DEDENT def update ( D , k , r1 , c1 , r2 , c2 ) : NEW_LINE INDENT for i in range ( r1 , r2 + 1 ) : NEW_LINE INDENT D [ i ] [ c1 ] += k ; NEW_LINE D [ i ] [ c2 + 1 ] -= k ; NEW_LINE DEDENT DEDENT def printArray ( A , D ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT A [ i ] [ j ] = D [ i ] [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT A [ i ] [ j ] = D [ i ] [ j ] + A [ i ] [ j - 1 ] ; NEW_LINE DEDENT print ( A [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def performQueries ( A , Queries ) : NEW_LINE INDENT D = [ [ 0 for j in range ( M + 1 ) ] for i in range ( N ) ] NEW_LINE intializeDiff ( D , A ) ; NEW_LINE Q = len ( Queries ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT update ( D , Queries [ i ] [ 0 ] , Queries [ i ] [ 1 ] , Queries [ i ] [ 2 ] , Queries [ i ] [ 3 ] , Queries [ i ] [ 4 ] ) ; NEW_LINE DEDENT printArray ( A , D ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 1 , 2 , 3 ] , [ 1 , 1 , 0 ] , [ 4 , - 2 , 2 ] ] ; NEW_LINE Queries = [ [ 2 , 0 , 0 , 1 , 1 ] , [ - 1 , 1 , 0 , 2 , 2 ] ] ; NEW_LINE performQueries ( A , Queries ) ; NEW_LINE DEDENT
Minimum value to be added to maximize Bitwise XOR of the given array | Python program for the above approach ; Function to find complement of an integer ; Count the number of bits of maxElement ; Return 1 's complement ; Function to find the value required to be added to maximize XOR of the given array ; Stores the required value to be added to the array ; Stores the maximum array element ; Traverse the array ; Update XOR of the array ; Find maximum element in array ; Calculate 1 s ' complement ; Return the answer ; Driver Code
import math NEW_LINE def onesComplement ( n , maxElement ) : NEW_LINE INDENT bits = math . floor ( math . log2 ( maxElement ) ) + 1 NEW_LINE return ( ( 1 << bits ) - 1 ) ^ n NEW_LINE DEDENT def findNumber ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE maxElement = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = res ^ arr [ i ] NEW_LINE if ( maxElement < arr [ i ] ) : NEW_LINE INDENT maxElement = arr [ i ] NEW_LINE DEDENT DEDENT res = onesComplement ( res , maxElement ) NEW_LINE return ( res ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findNumber ( arr , N ) ) NEW_LINE
Count ways to select K array elements lying in a given range | Function to calculate factorial of all the numbers up to N ; Factorial of 0 is 1 ; Calculate factorial of all the numbers upto N ; Calculate factorial of i ; Function to find count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of ways to select at leas K elements whose values in range [ L , R ] ; Stores count of numbers having Value lies in the range [ L , R ] ; Traverse the array ; Check if the array elements Lie in the given range ; Update cntNum ; Stores factorial of numbers upto N ; Calculate total ways to select at least K elements whose values Lies in [ L , R ] ; Update cntWays ; Driver code
def calculateFactorial ( N ) : NEW_LINE INDENT fact = [ 0 ] * ( N + 1 ) NEW_LINE fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def cntWaysSelection ( arr , N , K , L , R ) : NEW_LINE INDENT cntWays = 0 NEW_LINE cntNum = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] >= L and arr [ i ] <= R ) : NEW_LINE INDENT cntNum += 1 NEW_LINE DEDENT DEDENT fact = list ( calculateFactorial ( cntNum ) ) NEW_LINE for i in range ( K , cntNum + 1 ) : NEW_LINE INDENT cntWays += fact [ cntNum ] // ( fact [ i ] * fact [ cntNum - i ] ) NEW_LINE DEDENT return cntWays NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 4 , 6 , 13 , 5 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE L = 4 NEW_LINE R = 10 NEW_LINE print ( cntWaysSelection ( arr , N , K , L , R ) ) NEW_LINE DEDENT
Bitwise AND of all unordered pairs from a given array | Function to calculate bitwise AND of all pairs from the given array ; Stores bitwise AND of all possible pairs ; Generate all possible pairs ; Calculate bitwise AND of each pair ; Driver Code
def TotalAndPair ( arr , N ) : NEW_LINE INDENT totalAND = ( 1 << 30 ) - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT totalAND &= ( arr [ i ] & arr [ j ] ) NEW_LINE DEDENT DEDENT return totalAND NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 12 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE print ( TotalAndPair ( arr , N ) ) NEW_LINE DEDENT
Modify N by adding its smallest positive divisor exactly K times | Python 3 program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; Iterate over the range [ 1 , K ] ; Update N ; Driver Code
import math NEW_LINE def smallestDivisorGr1 ( N ) : NEW_LINE INDENT for i in range ( 2 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return N NEW_LINE DEDENT def findValOfNWithOperat ( N , K ) : NEW_LINE INDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT N += smallestDivisorGr1 ( N ) NEW_LINE DEDENT return N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE K = 4 NEW_LINE print ( findValOfNWithOperat ( N , K ) ) NEW_LINE DEDENT
Sum of all possible triplet products from given ranges | Python3 program to implement the above approach ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores sum required sum ; Iterate over all possible values of i ; Iterate over all possible values of j ; Iterate over all possible values of k ; Stores the product of ( i * j * k ) ; Update sum ; Driver Code
M = 1000000007 NEW_LINE def findTripleSum ( A , B , C ) : NEW_LINE INDENT sum = 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 prod = ( ( ( i % M ) * ( j % M ) ) % M * ( k % M ) ) % M NEW_LINE sum = ( sum + prod ) % M NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 10 NEW_LINE B = 100 NEW_LINE C = 1000 NEW_LINE print ( findTripleSum ( A , B , C ) ) NEW_LINE DEDENT
Sum of all possible triplet products from given ranges | Python3 implementation to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores modulo multiplicative inverse of 8 ; Stores the sum of all possible values of ( i * j * k ) ; Update res ; Driver Code
M = 1000000007 NEW_LINE def power ( x , N ) : NEW_LINE INDENT global M NEW_LINE res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT res = ( res * x ) % M NEW_LINE DEDENT x = ( x * x ) % M NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def modinv ( X ) : NEW_LINE INDENT return power ( X , M - 2 ) NEW_LINE DEDENT def findTripleSum ( A , B , C ) : NEW_LINE INDENT global M NEW_LINE MMI = modinv ( 8 ) NEW_LINE res = 0 NEW_LINE res = ( ( ( ( A % M * ( A + 1 ) % M ) % M * ( B % M * ( B + 1 ) % M ) % M ) % M * ( C % M * ( C + 1 ) % M ) % M ) % M * MMI ) % M NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 10 NEW_LINE B = 100 NEW_LINE C = 1000 NEW_LINE print ( findTripleSum ( A , B , C ) ) NEW_LINE DEDENT
Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix | Python3 program for the above approach ; Function to find the maximum of minimum of array constructed from any two rows of the given matrix ; Initialize global max as INT_MIN ; Iterate through the rows ; Iterate through remaining rows ; Initialize row_min as INT_MAX ; Iterate through the column values of two rows ; Find max of two elements ; Update the row_min ; Update the global_max ; Print the global max ; Driver Code ; Given matrix mat [ ] [ ] ; Given number of rows and columns ; Function Call
import sys NEW_LINE def getMaximum ( N , M , mat ) : NEW_LINE INDENT global_max = - 1 * ( sys . maxsize ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT row_min = sys . maxsize NEW_LINE for k in range ( 0 , M ) : NEW_LINE INDENT m = max ( mat [ i ] [ k ] , mat [ j ] [ k ] ) NEW_LINE row_min = min ( row_min , m ) NEW_LINE DEDENT global_max = max ( global_max , row_min ) NEW_LINE DEDENT DEDENT return global_max NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 5 , 0 , 3 , 1 , 2 ] , [ 1 , 8 , 9 , 1 , 3 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 9 , 1 , 0 , 3 , 7 ] , [ 2 , 3 , 0 , 6 , 3 ] , [ 6 , 4 , 1 , 7 , 0 ] ] NEW_LINE N = 6 NEW_LINE M = 5 NEW_LINE print ( getMaximum ( N , M , mat ) ) NEW_LINE DEDENT
Maximize sum of MEX values of each node in an N | Function to create an N - ary Tree ; Traverse the edges ; Add edges ; Function to get the maximum sum of MEX values of tree rooted at 1 ; Initialize mex ; Iterate through all children of node ; Recursively find maximum sum of MEX values of each node in tree rooted at u ; Store the maximum sum of MEX of among all subtrees ; Increase the size of tree rooted at current node ; Resulting MEX for the current node of the recursive call ; Driver Code ; Given N nodes ; Given N - 1 edges ; Stores the tree ; Generates the tree ; Returns maximum sum of MEX values of each node
def makeTree ( tree , edges , N ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE tree [ u ] . append ( v ) NEW_LINE DEDENT return tree NEW_LINE DEDENT def dfs ( node , tree ) : NEW_LINE INDENT mex = 0 NEW_LINE size = 1 NEW_LINE for u in tree [ node ] : NEW_LINE INDENT temp = dfs ( u , tree ) NEW_LINE mex = max ( mex , temp [ 0 ] ) NEW_LINE size += temp [ 1 ] NEW_LINE DEDENT return [ mex + size , size ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE edges = [ [ 1 , 4 ] , [ 1 , 5 ] , [ 5 , 2 ] , [ 5 , 3 ] , [ 4 , 7 ] , [ 7 , 6 ] ] NEW_LINE tree = [ [ ] for i in range ( N + 1 ) ] NEW_LINE tree = makeTree ( tree , edges , N ) NEW_LINE print ( dfs ( 1 , tree ) [ 0 ] ) NEW_LINE DEDENT
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Function to count divisors of n ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Divisor count of x ; Iterate from x + 1 and check for each element ; Driver Code ; Given X ; Function call
def divisorCount ( n ) : NEW_LINE INDENT x = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i == n // i ) : NEW_LINE INDENT x += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT x += 2 ; NEW_LINE DEDENT DEDENT if ( i * i > n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return x ; NEW_LINE DEDENT def minvalue_y ( x ) : NEW_LINE INDENT a = divisorCount ( x ) ; NEW_LINE y = x + 1 ; NEW_LINE while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) : NEW_LINE INDENT y += 1 ; NEW_LINE DEDENT return y ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 ; NEW_LINE print ( minvalue_y ( x ) ) ; NEW_LINE DEDENT
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Check if x is perfect square ; Driver Code
def minvalue_y ( x ) : NEW_LINE INDENT n = int ( pow ( x , 1 / 2 ) ) NEW_LINE if ( n * n == x ) : NEW_LINE INDENT return x + 1 NEW_LINE DEDENT return ( pow ( n + 1 , 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 NEW_LINE print ( minvalue_y ( x ) ) NEW_LINE DEDENT
Sum of first N natural numbers with alternate signs | Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If N is an even number ; Update alternateSum ; If N is an odd number ; Update alternateSum ; Driver Code
def alternatingSumOfFirst_N ( N ) : NEW_LINE INDENT alternateSum = 0 ; NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT alternateSum = ( - N ) // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT alternateSum = ( N + 1 ) // 2 ; NEW_LINE DEDENT return alternateSum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE print ( alternatingSumOfFirst_N ( N ) ) ; NEW_LINE DEDENT
Minimum value to be added to the prefix sums at each array indices to make them positive | Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call
def minStartValue ( nums ) : NEW_LINE INDENT minValue = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT sum += nums [ i ] NEW_LINE minValue = min ( minValue , sum ) NEW_LINE DEDENT startValue = 1 - minValue NEW_LINE return startValue NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT nums = [ - 3 , 2 , - 3 , 4 , 2 ] NEW_LINE print ( minStartValue ( nums ) ) NEW_LINE DEDENT
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once | Function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; Stores the count of elements in prefix and suffix of array elements ; Stores the sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto index i ; Stores sum of suffix of index i ; Stores the desired result ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix map ; Store the difference between the subarrays ; Count number of ways to split the array at index i such that subarray sums are equal ; Update the final result ; Return the result ; Driver Code ; Function Call
def countSubArrayRemove ( arr , N ) : NEW_LINE INDENT prefix_element_count = { } NEW_LINE suffix_element_count = { } NEW_LINE total_sum_of_elements = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT total_sum_of_elements += arr [ i ] NEW_LINE suffix_element_count [ arr [ i ] ] = suffix_element_count . get ( arr [ i ] , 0 ) + 1 NEW_LINE i -= 1 NEW_LINE DEDENT prefix_sum = 0 NEW_LINE suffix_sum = 0 NEW_LINE count_subarray_equal_sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT prefix_sum += arr [ i ] NEW_LINE prefix_element_count [ arr [ i ] ] = prefix_element_count . get ( arr [ i ] , 0 ) + 1 NEW_LINE suffix_sum = total_sum_of_elements - prefix_sum NEW_LINE suffix_element_count [ arr [ i ] ] = suffix_element_count . get ( arr [ i ] , 0 ) - 1 NEW_LINE difference = prefix_sum - suffix_sum NEW_LINE number_of_subarray_at_i_split = ( prefix_element_count . get ( difference , 0 ) + suffix_element_count . get ( - difference , 0 ) ) NEW_LINE count_subarray_equal_sum += number_of_subarray_at_i_split NEW_LINE DEDENT return count_subarray_equal_sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countSubArrayRemove ( arr , N ) ) NEW_LINE DEDENT
Count set bits in Bitwise XOR of all adjacent elements upto N | Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
def countXORSetBitsAdjElemRange1_N ( N ) : NEW_LINE INDENT total_set_bits = 0 NEW_LINE bit_Position = 1 NEW_LINE while ( N ) : NEW_LINE INDENT total_set_bits += ( ( N + 1 ) // 2 * bit_Position ) NEW_LINE N -= ( N + 1 ) // 2 NEW_LINE bit_Position += 1 NEW_LINE DEDENT return total_set_bits NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( countXORSetBitsAdjElemRange1_N ( N ) ) NEW_LINE DEDENT