codeparrot/xlcost-text-to-code huggingface dataset:
Count inversions in a sequence generated by appending given array K times | Function to count the number of inversions in K copies of given array ; Stores count of inversions in the given array ; Stores the count of pairs of distinct array elements ; Traverse the array ; Generate each pair ; Check for each pair , if the condition is satisfied or not ; If pairs consist of distinct elements ; Count inversiosn in the sequence ; Prthe answer ; Given array ; Given K ; Size of the array
def totalInversions ( arr , K , N ) : NEW_LINE INDENT inv = 0 NEW_LINE X = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and i < j ) : NEW_LINE INDENT inv += 1 NEW_LINE DEDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT X += 1 NEW_LINE DEDENT DEDENT DEDENT totalInv = X * K * ( K - 1 ) // 2 + inv * K NEW_LINE print ( totalInv ) NEW_LINE DEDENT arr = [ 2 , 1 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE totalInversions ( arr , K , N ) NEW_LINE
Count ways to generate an array having distinct elements at M consecutive indices | Modular function to calculate factorial ; Stores factorial of N ; Iterate over the range [ 1 , N ] ; Update result ; Function to count ways to replace array elements having 0 s with non - zero elements such that any M consecutive elements are distinct ; Store m consecutive distinct elements such that arr [ i ] is equal to B [ i % M ] ; Stores frequency of array elements ; Traverse the array arr ; If arr [ i ] is non - zero ; If B [ i % M ] is equal to 0 ; Update B [ i % M ] ; Update frequency of arr [ i ] ; If a duplicate element found in M consecutive elements ; Handling the case of inequality ; Stores count of 0 s in B ; Traverse the array , B ; If B [ i ] is 0 ; Update cnt ; Calculate factorial ; Driver Code ; Given M ; Given array ; Size of the array ; Function Call
def Fact ( N ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT result = ( result * i ) NEW_LINE DEDENT return result NEW_LINE DEDENT def numberOfWays ( M , arr , N ) : NEW_LINE INDENT B = [ 0 ] * ( M ) NEW_LINE counter = [ 0 ] * ( M + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT if ( B [ i % M ] == 0 ) : NEW_LINE INDENT B [ i % M ] = arr [ i ] NEW_LINE counter [ arr [ i ] ] += 1 NEW_LINE if ( counter [ arr [ i ] ] > 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT DEDENT elif ( B [ i % M ] != arr [ i ] ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT if ( B [ i ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT print ( Fact ( cnt ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 4 NEW_LINE arr = [ 1 , 0 , 3 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE numberOfWays ( M , arr , N ) NEW_LINE DEDENT
Maximize sum by traversing diagonally from each cell of a given Matrix | Function to find the maximum sum ; Loop to traverse through the upper triangular matrix and update the maximum sum to ans ; Traverse through the lower triangular matrix ; Driver Code ; Given matrix ; Given dimension
def MaximumSum ( arr , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT x , y , sum = 0 , i , 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum , x , y = sum + arr [ x ] [ y ] , x + 1 , y + 1 NEW_LINE DEDENT if ( sum > ans ) : NEW_LINE INDENT ans = sum NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT x , y , sum = i , 0 , 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum , x , y = sum + arr [ x ] [ y ] , x + 1 , y + 1 NEW_LINE DEDENT if ( sum > ans ) : NEW_LINE INDENT ans = sum NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 3 , 5 , 10 ] , [ 1 , 3 , 5 ] ] NEW_LINE n = len ( arr ) NEW_LINE print ( MaximumSum ( arr , n ) ) NEW_LINE DEDENT
Count array elements exceeding all previous elements as well as the next array element | Function to count array elements satisfying the given condition ; If there is only one array element ; Traverse the array ; Update the maximum element encountered so far ; Count the number of array elements strictly greater than all previous and immediately next elements ; Print the count ; Given array ; Size of the array
def numberOfIntegers ( arr , N ) : NEW_LINE INDENT cur_max = 0 NEW_LINE count = 0 NEW_LINE if ( N == 1 ) : NEW_LINE INDENT count = 1 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] > cur_max ) : NEW_LINE INDENT cur_max = arr [ i ] NEW_LINE if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT if ( arr [ N - 1 ] > cur_max ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT arr = [ 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE numberOfIntegers ( arr , N ) NEW_LINE
Count ways to represent N as sum of powers of 2 | Python3 program for above implementation ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver Code
from math import log2 NEW_LINE def numberOfWays ( n , k ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n >= pow ( 2 , k ) ) : NEW_LINE INDENT curr_val = pow ( 2 , k ) NEW_LINE return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return numberOfWays ( n , k - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE k = log2 ( n ) NEW_LINE print ( numberOfWays ( n , k ) ) NEW_LINE DEDENT
Count ways to obtain triplets with positive product consisting of at most one negative element | Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code
def possibleTriplets ( arr , N ) : NEW_LINE INDENT freq = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT DEDENT return ( freq * ( freq - 1 ) * ( freq - 2 ) ) // 6 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , - 9 , - 3 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( possibleTriplets ( arr , N ) ) NEW_LINE DEDENT
Maximize difference between sum of even and odd | Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] o 1 - based indexing ; Reverse the array ; Convert arr [ ] o 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
def maxPossibleDiff ( arr , N ) : NEW_LINE INDENT arr . append ( - 1 ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE arr . append ( - 1 ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE maxDiff = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT maxDiff += arr [ i ] NEW_LINE DEDENT if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT maxDiff -= arr [ i ] NEW_LINE DEDENT DEDENT print ( maxDiff ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE maxPossibleDiff ( arr , N ) NEW_LINE DEDENT
Reverse all elements of given circular array starting from index K | Function to print array arr [ ] ; Print the array ; Function to reverse elements of given circular array starting from index k ; Initialize two variables as start = k and end = k - 1 ; Initialize count = N / 2 ; Loop while count > 0 ; Swap the elements at index ( start % N ) and ( end % N ) ; Update the start and end ; If end equals to - 1 set end = N - 1 ; Print the circular array ; Driver Code ; Function Call
def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def reverseCircularArray ( arr , N , K ) : NEW_LINE INDENT start , end = K , K - 1 NEW_LINE count = N // 2 NEW_LINE while ( count ) : NEW_LINE INDENT temp = arr [ start % N ] NEW_LINE arr [ start % N ] = arr [ end % N ] NEW_LINE arr [ end % N ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE if ( end == - 1 ) : NEW_LINE INDENT end = N - 1 NEW_LINE DEDENT count -= 1 NEW_LINE DEDENT printArray ( arr , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 , 2 , 4 , 1 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE reverseCircularArray ( arr , N , K ) NEW_LINE DEDENT
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix | Function to find the matrix , A [ ] [ ] satisfying the given conditions ; Store the final matrix ; Initialize all the elements of the matrix A with 1 ; Traverse the matrix B [ ] [ ] row - wise ; If B [ i ] [ j ] is equal to 0 ; Mark all the elements of ith row of A [ ] [ ] as 0 ; Mark all the elements of jth column of A [ ] [ ] as 0 ; Check if the matrix B [ ] [ ] can be made using matrix A [ ] [ ] ; Store the bitwise OR of all elements of A [ ] [ ] in ith row and jth column ; Traverse through ith row ; Traverse through jth column ; If B [ i ] [ j ] is not equal to c , pr " Not ▁ Possible " ; Print the final matrix A [ ] [ ] ; Driver Code ; Function Call
def findOriginalMatrix ( B , N , M ) : NEW_LINE INDENT A = [ [ 0 ] * M ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT A [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( B [ i ] [ j ] == 0 ) : NEW_LINE INDENT for k in range ( M ) : NEW_LINE INDENT A [ i ] [ k ] = 0 NEW_LINE DEDENT for k in range ( N ) : NEW_LINE INDENT A [ k ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT c = 0 NEW_LINE for k in range ( M ) : NEW_LINE INDENT if ( c == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT c += A [ i ] [ k ] NEW_LINE DEDENT for k in range ( N ) : NEW_LINE INDENT if ( c == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT c += A [ k ] [ j ] NEW_LINE DEDENT if ( c != B [ i ] [ j ] ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT print ( A [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT B = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] NEW_LINE N = len ( B ) NEW_LINE M = len ( B [ 0 ] ) NEW_LINE findOriginalMatrix ( B , N , M ) NEW_LINE
Minimize cost to make X and Y equal by given increments | Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
def gcd ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return x NEW_LINE DEDENT return gcd ( y , x % y ) NEW_LINE DEDENT def lcm ( x , y ) : NEW_LINE INDENT return ( x * y ) // gcd ( x , y ) NEW_LINE DEDENT def minimumCost ( x , y ) : NEW_LINE INDENT lcm_ = lcm ( x , y ) NEW_LINE costx = ( lcm_ - x ) // x NEW_LINE costy = ( lcm_ - y ) // y NEW_LINE return costx + costy NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 5 NEW_LINE y = 17 NEW_LINE print ( minimumCost ( x , y ) ) NEW_LINE DEDENT
Find GCD between the sum of two given integers raised to the power of N and their difference | Python program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Driver code ; Given p , q and n ; Function Call
import math NEW_LINE mod = 1000000007 ; NEW_LINE def power ( a , n , d ) : NEW_LINE INDENT res = 1 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( ( n % 2 ) != 0 ) : NEW_LINE INDENT res = ( ( res % d ) * ( a % d ) ) % d ; NEW_LINE DEDENT a = ( ( a % d ) * ( a % d ) ) % d ; NEW_LINE n /= 2 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def gcd ( p , q , n ) : NEW_LINE INDENT if ( p == q ) : NEW_LINE INDENT return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; NEW_LINE DEDENT candidate = 1 ; NEW_LINE num = p - q ; NEW_LINE sq = ( int ) ( math . sqrt ( num ) ) ; NEW_LINE for i in range ( 1 , sq ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT X = power ( p , n , i ) ; NEW_LINE Y = power ( q , n , i ) ; NEW_LINE temp = ( X + Y ) % i ; NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT candidate = max ( candidate , i ) ; NEW_LINE DEDENT temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT candidate = max ( candidate , num / i ) ; NEW_LINE DEDENT DEDENT DEDENT return candidate % mod ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 10 ; NEW_LINE q = 6 ; NEW_LINE n = 5 ; NEW_LINE print ( ( int ) ( gcd ( p , q , n ) ) ) ; NEW_LINE DEDENT
Split given isosceles triangle of height H into N equal parts | Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Driver Code ; Given N ; Given H ; Function call
def findPoint ( n , h ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT print ( " { 0 : . 2f } " . format ( ( ( i / n ) ** 0.5 ) * h ) , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE h = 2 NEW_LINE findPoint ( n , h ) NEW_LINE DEDENT
Count all possible values of K less than Y such that GCD ( X , Y ) = GCD ( X + K , Y ) | Function to find the gcd of a and b ; Function to find the number of Ks ; Find gcd ; Calculating value of totient function for n ; Driver Code ; Given X and Y
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def calculateK ( x , y ) : NEW_LINE INDENT g = gcd ( x , y ) NEW_LINE n = y // g NEW_LINE res = n NEW_LINE i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT res -= ( res // i ) NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n //= i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( n != 1 ) : NEW_LINE INDENT res -= ( res // n ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 3 NEW_LINE y = 15 NEW_LINE print ( calculateK ( x , y ) ) NEW_LINE DEDENT
Check if a Float value is equivalent to an Integer value | Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; Driver Code
def isInteger ( N ) : NEW_LINE INDENT X = int ( N ) NEW_LINE temp2 = N - X NEW_LINE if ( temp2 > 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1.5 NEW_LINE if ( isInteger ( N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Find the nearest power of 2 for every array element | Python program to implement the above approach ; Function to find the nearest power of two for every array element ; Traverse the array ; Calculate log of current array element ; Find the nearest ; Driver Code
import math NEW_LINE def nearestPowerOfTwo ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT lg = ( int ) ( math . log2 ( arr [ i ] ) ) NEW_LINE a = ( int ) ( math . pow ( 2 , lg ) ) NEW_LINE b = ( int ) ( math . pow ( 2 , lg + 1 ) ) NEW_LINE if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) : NEW_LINE INDENT print ( a , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( b , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 5 , 2 , 7 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE nearestPowerOfTwo ( arr , N ) NEW_LINE
Flip bits of the sum of count of set bits of two given numbers | Python3 program for the above approach ; Function to count number of set bits in integer ; Variable for counting set bits ; Function to invert bits of a number ; Calculate number of bits of N - 1 ; ; Function to invert the sum of set bits in A and B ; Stores sum of set bits ; Driver Code
import math NEW_LINE def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT n &= ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def invertBits ( n ) : NEW_LINE INDENT x = ( int ) ( math . log ( n ) / math . log ( 2 ) ) NEW_LINE m = 1 << x NEW_LINE m = m | m - 1 NEW_LINE n = n ^ m NEW_LINE return n NEW_LINE DEDENT def invertSum ( A , B ) : NEW_LINE INDENT temp = countSetBits ( A ) + countSetBits ( B ) NEW_LINE print ( invertBits ( temp ) ) NEW_LINE DEDENT A = 5 NEW_LINE B = 7 NEW_LINE invertSum ( A , B ) NEW_LINE
Sum of Bitwise XOR of each array element with all other array elements | Function to calculate for each array element , sum of its Bitwise XOR with all other array elements ; Declare an array of size 64 to store count of each bit ; Traversing the array ; Check if bit is present of not ; Increase the bit position ; Reduce the number to half ; Traverse the array ; Stores the bit position ; Stores the sum of Bitwise XOR ; Check if bit is present of not ; Reduce the number to its half ; Print the sum for A [ i ] ; Given arr1 [ ] ; Size of N ; Function Call
def XOR_for_every_i ( A , N ) : NEW_LINE INDENT frequency_of_bits = [ 0 ] * 32 NEW_LINE for i in range ( N ) : NEW_LINE INDENT bit_position = 0 NEW_LINE M = A [ i ] NEW_LINE while ( M ) : NEW_LINE INDENT if ( M & 1 != 0 ) : NEW_LINE INDENT frequency_of_bits [ bit_position ] += 1 NEW_LINE DEDENT bit_position += 1 NEW_LINE M >>= 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT M = A [ i ] NEW_LINE value_at_that_bit = 1 NEW_LINE XOR_sum = 0 NEW_LINE for bit_position in range ( 32 ) : NEW_LINE INDENT if ( M & 1 != 0 ) : NEW_LINE INDENT XOR_sum += ( ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ) NEW_LINE DEDENT else : NEW_LINE INDENT XOR_sum += ( ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ) NEW_LINE DEDENT M >>= 1 NEW_LINE value_at_that_bit <<= 1 NEW_LINE DEDENT print ( XOR_sum , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT A = [ 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE XOR_for_every_i ( A , N ) NEW_LINE
Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | Function to minimize the maximum difference between any pair of elements of the array by the given operations ; Traverse the array ; If current element is even ; Insert it into even ; Otherwise ; Make it even by multiplying by 2 and insert it into set ; Calculate difference between first and the last element of the set ; Iterate until difference is minimized ; Erase the current element ; Reduce current element by half and insert it into the Set ; Update difference ; Return the resultant difference ; Driver Code
def minimumMaxDiff ( nums ) : NEW_LINE INDENT s = { } NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT if ( nums [ i ] % 2 == 0 ) : NEW_LINE INDENT s [ nums [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT s [ nums [ i ] * 2 ] = 1 NEW_LINE DEDENT DEDENT sr = list ( s . keys ( ) ) NEW_LINE res = sr [ - 1 ] - sr [ 0 ] NEW_LINE while ( list ( s . keys ( ) ) [ - 1 ] % 2 == 0 ) : NEW_LINE INDENT r = list ( s . keys ( ) ) NEW_LINE x = r [ - 1 ] NEW_LINE del s [ x ] NEW_LINE s [ x // 2 ] = 1 NEW_LINE rr = list ( s . keys ( ) ) NEW_LINE res = min ( res , rr [ - 1 ] - r [ 0 ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 9 ] NEW_LINE print ( minimumMaxDiff ( arr ) ) NEW_LINE DEDENT
XOR of all even numbers from a given range | Function to calculate XOR of numbers in the range [ 1 , n ] ; If n is divisible by 4 ; If n mod 4 is equal to 1 ; If n mod 4 is equal to 2 ; Function to find XOR of even numbers in the range [ l , r ] ; Update xor_r ; Update xor_l ; Driver Code
def bitwiseXorRange ( n ) : NEW_LINE INDENT if ( n % 4 == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT if ( n % 4 == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 4 == 2 ) : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def evenXorRange ( l , r ) : NEW_LINE INDENT xor_r = 2 * bitwiseXorRange ( r // 2 ) NEW_LINE xor_l = 2 * bitwiseXorRange ( ( l - 1 ) // 2 ) NEW_LINE return xor_l ^ xor_r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 10 NEW_LINE r = 20 NEW_LINE print ( evenXorRange ( l , r ) ) NEW_LINE DEDENT
Program to calculate Variance of first N Natural Numbers | Function to calculate Variance of first N natural numbers ; Driver Code
def find_Variance ( n ) : NEW_LINE INDENT numerator = n * n - 1 NEW_LINE ans = ( numerator * 1.0 ) / 12 NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE a = find_Variance ( N ) NEW_LINE print ( " { 0 : . 6f } " . format ( a ) ) NEW_LINE DEDENT
Count digits present in each element of a given Matrix | Python3 program for the above approach ; Function to count the number of digits in each element of the given matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Count the number of digits ; Print the result ; Driver Code ; Given matrix
from math import floor , log10 NEW_LINE M = 3 NEW_LINE N = 3 NEW_LINE def countDigit ( arr ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT X = arr [ i ] [ j ] NEW_LINE d = floor ( log10 ( X ) * 1.0 ) + 1 NEW_LINE print ( d , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ] NEW_LINE countDigit ( arr ) NEW_LINE DEDENT
Difference between sum of odd and even frequent elements in an Array | Function to find the sum of all even and odd frequent elements in an array ; Stores the frequency of array elements ; Traverse the array ; Update frequency of current element ; Stores sum of odd and even frequent elements ; Traverse the map ; If frequency is odd ; Add sum of all occurrences of current element to sum_odd ; If frequency is even ; Add sum of all occurrences of current element to sum_even ; Calculate difference between their sum ; Return diff ; Driver code
def findSum ( arr , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT sum_odd , sum_even = 0 , 0 NEW_LINE for itr in mp : NEW_LINE INDENT if ( mp [ itr ] % 2 != 0 ) : NEW_LINE INDENT sum_odd += ( itr ) * ( mp [ itr ] ) NEW_LINE DEDENT if ( mp [ itr ] % 2 == 0 ) : NEW_LINE INDENT sum_even += ( itr ) * ( mp [ itr ] ) NEW_LINE DEDENT DEDENT diff = sum_even - sum_odd NEW_LINE return diff NEW_LINE DEDENT arr = [ 1 , 5 , 5 , 2 , 4 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findSum ( arr , N ) ) NEW_LINE