codeparrot/xlcost-text-to-code huggingface dataset:
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 or not ; If N is not a power of 2 ; Bitwise AND operation ; Driver Code
def checkEvenPower ( N ) : NEW_LINE INDENT if ( ( N & ( N - 1 ) ) != 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT N = N & 0x55555555 NEW_LINE return ( N > 0 ) NEW_LINE DEDENT N = 4 NEW_LINE print ( 1 if checkEvenPower ( N ) else 0 ) NEW_LINE
Sum of quotients of division of N by powers of K not exceeding N | Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Prthe result ; Driver Code ; Given N and K
def findSum ( N , K ) : NEW_LINE INDENT ans = 0 NEW_LINE i = 1 NEW_LINE while ( i <= N ) : NEW_LINE INDENT ans += N // i NEW_LINE i = i * K NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , K = 10 , 2 NEW_LINE findSum ( N , K ) NEW_LINE DEDENT
Count Arithmetic Progressions having sum S and common difference equal to D | Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver Code
def countAPs ( S , D ) : NEW_LINE INDENT S = S * 2 NEW_LINE answer = 0 NEW_LINE for i in range ( 1 , S ) : NEW_LINE INDENT if i * i > S : NEW_LINE INDENT break NEW_LINE DEDENT if ( S % i == 0 ) : NEW_LINE INDENT if ( ( ( S // i ) - D * i + D ) % 2 == 0 ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT if ( ( D * i - ( S // i ) + D ) % 2 == 0 ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , D = 12 , 1 NEW_LINE print ( countAPs ( S , D ) ) ; NEW_LINE DEDENT
Minimize insertions to make sum of every pair of consecutive array elements at most K | Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K ; Stores if it is possible to make sum of each pair of adjacent elements at most K ; Stores the count of insertions ; Stores the previous value to index i ; Traverse the array ; If arr [ i ] is greater than or equal to K ; Mark possible 0 ; If last + arr [ i ] is greater than K ; Increment res by 1 ; Assign arr [ i ] to last ; If possible to make the sum of pairs of adjacent elements at most K ; Otherwise print " - 1" ; Driver Code
def minimumInsertions ( arr , N , K ) : NEW_LINE INDENT possible = 1 NEW_LINE res = 0 NEW_LINE last = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= K ) : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT if ( last + arr [ i ] > K ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT last = arr [ i ] NEW_LINE DEDENT if ( possible ) : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE K = 6 NEW_LINE N = len ( arr ) NEW_LINE minimumInsertions ( arr , N , K ) NEW_LINE DEDENT
Maximum sum of K | Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver code ; Stores the count of distinct elements
def distinct ( arr , n ) : NEW_LINE INDENT mpp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mpp [ arr [ i ] ] = 1 NEW_LINE DEDENT return len ( mpp ) NEW_LINE DEDENT def maxSubSum ( arr , n , k , totalDistinct ) : NEW_LINE INDENT if ( k > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT maxm = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE st = set ( ) NEW_LINE for j in range ( i , i + k , 1 ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE st . add ( arr [ j ] ) NEW_LINE DEDENT if ( len ( st ) == totalDistinct ) : NEW_LINE INDENT maxm = max ( sum , maxm ) NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 ] NEW_LINE K = 6 NEW_LINE N = len ( arr ) NEW_LINE totalDistinct = distinct ( arr , N ) NEW_LINE print ( maxSubSum ( arr , N , K , totalDistinct ) ) NEW_LINE DEDENT
Maximum sum of K | Function to count the number of distinct elements present in the array ; Insert array elements into set ; Return the st size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the mp ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call
def distinct ( arr , N ) : NEW_LINE INDENT st = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT st . add ( arr [ i ] ) NEW_LINE DEDENT return len ( st ) NEW_LINE DEDENT def maxSubarraySumUtil ( arr , N , K , totalDistinct ) : NEW_LINE INDENT if ( K > N ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mx = 0 NEW_LINE sum = 0 NEW_LINE mp = { } NEW_LINE for i in range ( 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 sum += arr [ i ] NEW_LINE if ( i >= K ) : NEW_LINE INDENT if ( arr [ i - K ] in mp ) : NEW_LINE INDENT mp [ arr [ i - K ] ] -= 1 NEW_LINE sum -= arr [ i - K ] NEW_LINE DEDENT if ( arr [ i - K ] in mp and mp [ arr [ i - K ] ] == 0 ) : NEW_LINE INDENT mp . remove ( arr [ i - K ] ) NEW_LINE DEDENT DEDENT if ( len ( mp ) == totalDistinct ) : NEW_LINE INDENT mx = max ( mx , sum ) NEW_LINE DEDENT DEDENT return mx NEW_LINE DEDENT def maxSubarraySum ( arr , K ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE totalDistinct = distinct ( arr , N ) NEW_LINE print ( maxSubarraySumUtil ( arr , N , K , totalDistinct ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 ] NEW_LINE K = 6 NEW_LINE maxSubarraySum ( arr , K ) NEW_LINE DEDENT
Number of Irreflexive Relations on a Set | Python3 program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver Code
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT global mod NEW_LINE res = 1 NEW_LINE x = x % mod NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def irreflexiveRelation ( N ) : NEW_LINE INDENT return power ( 2 , N * N - N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( irreflexiveRelation ( N ) ) NEW_LINE DEDENT
Count Arithmetic Progressions having sum N and common difference equal to 1 | Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Prcount - 1 ; Given value of N ; Function call to count required number of AP series
def countAPs ( N ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while ( i * i <= 2 * N ) : NEW_LINE INDENT res = 2 * N NEW_LINE if ( res % i == 0 ) : NEW_LINE INDENT op = res / i - i + 1 NEW_LINE if ( op % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( i * i != res and ( i - res / i + 1 ) % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT print ( count - 1 ) NEW_LINE DEDENT N = 963761198400 NEW_LINE countAPs ( N ) NEW_LINE
Check if concatenation of first and last digits forms a prime number or not for each array element | Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver Code ; Computes and stores primes using Sieve ; Function call to perform queries
sieve = [ 0 for i in range ( 105 ) ] NEW_LINE def buildSieve ( ) : NEW_LINE INDENT global sieve NEW_LINE for i in range ( 2 , 100 ) : NEW_LINE INDENT sieve [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if ( sieve [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , 100 , i ) : NEW_LINE INDENT sieve [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT def isAnyPrime ( first , last ) : NEW_LINE INDENT global sieve NEW_LINE num1 = first * 10 + last NEW_LINE num2 = last * 10 + first NEW_LINE if ( sieve [ num1 ] == 1 or sieve [ num2 ] == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def performQueries ( q ) : NEW_LINE INDENT for i in range ( len ( q ) ) : NEW_LINE INDENT A = q [ i ] NEW_LINE last = A % 10 NEW_LINE first = 0 NEW_LINE while ( A >= 10 ) : NEW_LINE INDENT A = A // 10 NEW_LINE DEDENT first = A NEW_LINE if ( isAnyPrime ( first , last ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = [ 30 , 66 ] NEW_LINE buildSieve ( ) NEW_LINE performQueries ( q ) NEW_LINE DEDENT
Check if a given number N has at least one odd divisor not exceeding N | Function to check whether N has at least one odd divisor not exceeding N - 1 or not ; Stores the value of N ; Reduce the given number N by dividing it by 2 ; If N is divisible by an odd divisor i ; Check if N is an odd divisor after reducing N by dividing it by 2 ; Otherwise ; Driver Code ; Function Call
def oddDivisor ( N ) : NEW_LINE INDENT X = N NEW_LINE while ( N % 2 == 0 ) : NEW_LINE INDENT N //= 2 NEW_LINE DEDENT i = 3 NEW_LINE while ( i * i <= X ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT i += 2 NEW_LINE DEDENT if ( N != X ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT N = 10 NEW_LINE print ( oddDivisor ( N ) ) NEW_LINE
Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M | Stores the prime factor of numbers ; Function to find GCD of a and b ; Base Case ; Otherwise , calculate GCD ; Function to precompute the prime numbers till 1000000 ; Initialize all the positions with their respective values ; Iterate over the range [ 2 , sqrt ( 10 ^ 6 ) ] ; If i is prime number ; Mark it as the factor ; Utility function to count the number of steps to make X and Y equal ; Initialise steps ; Iterate x is at most 1 ; Divide with the smallest prime factor ; If X and Y can 't be  made equal ; Return steps ; Function to find the minimum number of steps required to make X and Y equal ; Generate all the prime factors ; Calculate GCD of x and y ; Divide the numbers by their gcd ; If not possible , then return - 1 ; Return the resultant number of steps ; Driver Code
primes = [ 0 ] * ( 1000006 ) NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def preprocess ( ) : NEW_LINE INDENT for i in range ( 1 , 1000001 ) : NEW_LINE INDENT primes [ i ] = i NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= 1000000 ) : NEW_LINE INDENT if ( primes [ i ] == i ) : NEW_LINE INDENT for j in range ( 2 * i , 1000001 , i ) : NEW_LINE INDENT if ( primes [ j ] == j ) : NEW_LINE INDENT primes [ j ] = i NEW_LINE DEDENT DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def Steps ( x , m ) : NEW_LINE INDENT steps = 0 NEW_LINE flag = False NEW_LINE while ( x > 1 ) : NEW_LINE INDENT if ( primes [ x ] > m ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT x //= primes [ x ] NEW_LINE steps += 1 NEW_LINE DEDENT if ( flag != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return steps NEW_LINE DEDENT def minimumSteps ( x , y , m ) : NEW_LINE INDENT preprocess ( ) NEW_LINE g = gcd ( x , y ) NEW_LINE x = x // g NEW_LINE y = y // g NEW_LINE x_steps = Steps ( x , m ) NEW_LINE y_steps = Steps ( y , m ) NEW_LINE if ( x_steps == - 1 or y_steps == - 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return x_steps + y_steps NEW_LINE DEDENT X = 160 NEW_LINE Y = 180 NEW_LINE M = 10 NEW_LINE print ( minimumSteps ( X , Y , M ) ) NEW_LINE
Length of longest subsequence consisting of Non | Python3 program for the above approach ; Function to check if n is a deficient number or not ; Stores sum of divisors ; Iterate over the range [ 1 , sqrt ( N ) ] ; If n is divisible by i ; If divisors are equal , add only one of them ; Otherwise add both ; Function to print the longest subsequence which does not contain any deficient numbers ; Stores the count of array elements which are non - deficient ; Traverse the array ; If element is non - deficient ; Return the answer ; Driver Code
import math NEW_LINE def isNonDeficient ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + i NEW_LINE sum = sum + ( n // i ) NEW_LINE DEDENT DEDENT DEDENT return sum >= 2 * n NEW_LINE DEDENT def LongestNonDeficientSubsequence ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isNonDeficient ( arr [ i ] ) ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 13 , 55 , 240 , 32 , 24 , 27 , 56 , 80 , 100 , 330 , 89 ] NEW_LINE N = len ( arr ) NEW_LINE print ( LongestNonDeficientSubsequence ( arr , N ) ) NEW_LINE DEDENT
Number of subarrays consisting only of Pronic Numbers | Function to check if a number is pronic number or not ; Iterate over the range [ 1 , sqrt ( N ) ] ; Return true if N is pronic ; Otherwise , return false ; Function to count the number of subarrays consisting of pronic numbers ; Stores the count of subarrays ; Stores the number of consecutive array elements which are pronic ; Traverse the array ; If i is pronic ; Return the total count ; Driver Code
def isPronic ( n ) : NEW_LINE INDENT for i in range ( int ( n ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if i * ( i + 1 ) == n : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countSub ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE ispro = 0 NEW_LINE for i in arr : NEW_LINE INDENT if isPronic ( i ) : NEW_LINE INDENT ispro += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ispro = 0 NEW_LINE DEDENT ans += ispro NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 6 , 12 , 3 , 4 ] NEW_LINE print ( countSub ( arr ) ) NEW_LINE
Replace all array elements with the nearest power of its previous element | Python3 program for the above approach ; Function to calculate log x for given base ; Function to replace all array elements with the nearest power of previous adjacent nelement ; For the first element , set the last array element to its previous element ; Traverse the array ; Find K for which x ^ k is nearest to arr [ i ] ; Find the power to x nearest to arr [ i ] ; Update x ; Return the array ; Driver Code ; Function Call
import math NEW_LINE def LOG ( x , base ) : NEW_LINE INDENT return int ( math . log ( x ) / math . log ( base ) ) NEW_LINE DEDENT def repbyNP ( arr ) : NEW_LINE INDENT x = arr [ - 1 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT k = LOG ( arr [ i ] , x ) NEW_LINE temp = arr [ i ] NEW_LINE if abs ( x ** k - arr [ i ] ) < abs ( x ** ( k + 1 ) - arr [ i ] ) : NEW_LINE INDENT arr [ i ] = x ** k NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = x ** ( k + 1 ) NEW_LINE DEDENT x = temp NEW_LINE DEDENT return arr NEW_LINE DEDENT arr = [ 2 , 4 , 6 , 3 , 11 ] NEW_LINE print ( repbyNP ( arr ) ) NEW_LINE
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver Code
def lastElement ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE if n % 2 == 0 : NEW_LINE INDENT multiplier = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT multiplier = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] * multiplier NEW_LINE multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( int ( lastElement ( arr , N ) ) ) NEW_LINE DEDENT
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR | Function to maximize the count of pairs with even XOR possible in an array by given operations ; Stores count of odd array elements ; Traverse the array ; If arr [ i ] is odd ; Stores the total number of even pairs ; Driver Code ; Input ; Function call to count the number of pairs whose XOR is even
def countPairs ( arr , N ) : NEW_LINE INDENT odd = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT ans = ( N - odd + odd // 2 - 1 ) + odd // 2 NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | Python3 program for the above approach ; Function to count the number of pairs whose Bitwise AND is greater than the Bitwise XOR ; Stores the frequency of MSB of array elements ; Traverse the array ; Increment count of numbers having MSB at log ( arr [ i ] ) ; Stores total number of pairs ; Traverse the Map ; Return total count of pairs ; Driver Code
from math import log2 NEW_LINE def countPairs ( arr , N ) : NEW_LINE INDENT freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = int ( log2 ( arr [ i ] ) ) NEW_LINE freq [ x ] = freq . get ( x , 0 ) + 1 NEW_LINE DEDENT pairs = 0 NEW_LINE for i in freq : NEW_LINE INDENT pairs += freq [ i ] - 1 NEW_LINE DEDENT return pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 9 , 15 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Maximize difference between odd and even indexed array elements by swapping unequal adjacent bits in their binary representations | Python program for the above approach ; Function to count total number of bits present in a number ; Function to count total set bits in a number ; Stores the count of set bits ; Right shift by 1 ; Return the final count ; Function to find maximum number by shifting two unequal bits ; Count set bits in number n ; Iterate the string bits ; Function to find minimum number by shifting two unequal bits ; Iterate the set bit ; Function to find the maximum difference ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; Update CaseOne ; Stores the maximum difference ; Assign value O ; Traverse the array ; Update caseTwo ; Return maximum of caseOne and CaseTwo ; Drivers Code ; Function Call
import math NEW_LINE def countBit ( n ) : NEW_LINE INDENT return int ( math . log ( n , 2 ) ) + 1 NEW_LINE DEDENT def countSetBit ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while n : NEW_LINE INDENT ans += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def maximize ( n ) : NEW_LINE INDENT bits = countBit ( n ) NEW_LINE setBits = countSetBit ( n ) NEW_LINE ans = 0 NEW_LINE for i in range ( bits ) : NEW_LINE INDENT if i < setBits : NEW_LINE INDENT ans |= 1 NEW_LINE DEDENT if i != setBits - 1 : NEW_LINE INDENT ans <<= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def minimize ( n ) : NEW_LINE INDENT setBits = countSetBit ( n ) NEW_LINE ans = 0 NEW_LINE for i in range ( setBits ) : NEW_LINE INDENT ans |= 1 NEW_LINE if i != setBits - 1 : NEW_LINE INDENT ans <<= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def maxDiff ( arr ) : NEW_LINE INDENT caseOne = 0 NEW_LINE SumOfOdd = 0 NEW_LINE SumOfeven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT SumOfOdd += minimize ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT SumOfeven += maximize ( arr [ i ] ) NEW_LINE DEDENT DEDENT caseOne = abs ( SumOfOdd - SumOfeven ) NEW_LINE caseTwo = 0 NEW_LINE SumOfOdd = 0 NEW_LINE SumOfeven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT SumOfOdd += maximize ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT SumOfeven += minimize ( arr [ i ] ) NEW_LINE DEDENT DEDENT caseTwo = abs ( SumOfOdd - SumOfeven ) NEW_LINE return max ( caseOne , caseTwo ) NEW_LINE DEDENT arr = [ 54 , 32 , 11 , 23 ] NEW_LINE print ( maxDiff ( arr ) ) NEW_LINE
Sum of numbers obtained by the count of set and non | Functino to find the number after processing the diagonal elements ; Store the required number ; Checking for each position ; Store the number of set bits & non - set bits at position i ; Traverse the diagonal elements ; Update count of S if current element is set at position i ; Else update NS ; If number of set bits is > number of non - set bits , add set bits value to the ans ; Return the answer ; Function to find the sum of the numbers generated after processing both the diagonals of the matrix ; Store the primary diagonal elements ; Store the secondary diagonal elements ; Function Call to get the required numbers and return their sum ; Driver Code ; Function Call
def processDiagonal ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE getBit = 1 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT S = 0 NEW_LINE NS = 0 NEW_LINE for j in arr : NEW_LINE if getBit & j : NEW_LINE INDENT S += 1 NEW_LINE DEDENT else : NEW_LINE INDENT NS += 1 NEW_LINE DEDENT if S > NS : NEW_LINE ans += 2 ** i NEW_LINE getBit <<= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def findSum ( mat ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE priDiag = [ ] NEW_LINE while i < len ( mat ) : NEW_LINE INDENT priDiag . append ( mat [ i ] [ j ] ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT i = 0 NEW_LINE j = len ( mat ) - 1 NEW_LINE secDiag = [ ] NEW_LINE while i < len ( mat ) : NEW_LINE INDENT secDiag . append ( mat [ i ] [ j ] ) NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return processDiagonal ( priDiag ) + processDiagonal ( secDiag ) NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE print ( findSum ( mat ) ) NEW_LINE