text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray...
def currSubArrayScore ( a , b , l , r ) : NEW_LINE INDENT straightScore = 0 NEW_LINE reverseScore = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT straightScore += a [ i ] * b [ i ] NEW_LINE reverseScore += a [ r - ( i - l ) ] * b [ i ] NEW_LINE DEDENT return max ( straightScore , reverseScore ) NEW_LINE DED...
Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal leng...
def maxScoreSubArray ( a , b , n ) : NEW_LINE INDENT res = 0 NEW_LINE for mid in range ( n ) : NEW_LINE INDENT straightScore = a [ mid ] * b [ mid ] NEW_LINE reverseScore = a [ mid ] * a [ mid ] NEW_LINE prev = mid - 1 NEW_LINE next = mid + 1 NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE wh...
Length of the smallest subarray with maximum possible sum | Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the arra...
def minimumSizeSubarray ( arr , N ) : NEW_LINE INDENT i , j = 0 , N - 1 NEW_LINE while ( i < N and arr [ i ] == 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == N ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT while ( j >= 0 and arr [ j ] == 0 ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT return ( j - i + 1 ) NEW_LINE...
Count ways to represent N as XOR of distinct integers not exceeding N | Python program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code
from math import * NEW_LINE def countXorPartition ( N ) : NEW_LINE INDENT a = 2 ** floor ( N - log ( N + 1 ) / log ( 2 ) ) NEW_LINE print ( int ( a ) ) NEW_LINE DEDENT N = 5 NEW_LINE countXorPartition ( N ) NEW_LINE
Count numbers less than N whose modulo with A is equal to B | Function to count numbers less than N , whose modulo with A gives B ; If the value of B at least A ; If the value of B is 0 or not ; Stores the resultant count of numbers less than N ; Update the value of ans ; Print the value of ans ; Driver Code
def countValues ( A , B , C ) : NEW_LINE INDENT if ( B >= A ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT print ( C // A ) NEW_LINE return NEW_LINE DEDENT ans = C // A NEW_LINE if ( ans * A + B <= C ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DED...
Maximum sum of a subsequence whose Bitwise AND is non | Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bi...
def maximumSum ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for bit in range ( 32 ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] & ( 1 << bit ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT ans = max ( ans , sum ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if ...
Check if an array can be reduced to at most length K by removal of distinct elements | Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array element...
def maxCount ( arr , N , K ) : NEW_LINE INDENT st = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT st . add ( arr [ i ] ) NEW_LINE DEDENT if ( N - len ( st ) <= K ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ '...
Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ;...
def max_sum ( a , v , q , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE m = - 10 ** 9 ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT p = v [ i ] [ 0 ] [ 0 ] ; NEW_LINE q = v [ i ] [ 0 ] [ 1 ] ; NEW_LINE k = v [ i ] [ 1 ] ; NEW_LINE a [ p ] += k ; NEW_LINE if ( q + 1 <= n ) : NEW_LINE INDENT a [ q + 1 ] -= k ; NEW_LINE DEDE...
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is fou...
def findWinner ( X , Y ) : NEW_LINE INDENT playerA = ( X ^ Y ) NEW_LINE flag = False NEW_LINE for i in range ( 1 , X + 1 , 1 ) : NEW_LINE INDENT for j in range ( 1 , Y + 1 , 1 ) : NEW_LINE INDENT val = ( i ^ j ) NEW_LINE if ( val > playerA ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ...
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver Code
def findWinner ( X , Y ) : NEW_LINE INDENT first = ( X ^ Y ) NEW_LINE second = ( X + Y ) NEW_LINE if ( first == second ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = 2 , 4 NEW_LINE findWinner ...
Smallest number required to be added to M to make it divisible by N | Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; ...
def findNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N + K - rem NEW_LINE DEDENT DEDENT def findSmallest ( M , N ) : NEW_LINE INDENT x = findNum ( M , N ) NEW_LINE return x - M NEW_LINE DEDENT if __name__ == ' _ _ m...
Prefix Factorials of a Prefix Sum Array | Function to find the factorial of prefix sum at every possible index ; Find the prefix sum array ; Stores the factorial of all the element till the sum of array ; Find the factorial array ; Find the factorials of each array element ; Print the resultant array ; Driver code
def prefixFactorialArray ( A , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT A [ i ] += A [ i - 1 ] NEW_LINE DEDENT fact = [ 0 for x in range ( A [ N - 1 ] + 1 ) ] NEW_LINE fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , A [ N - 1 ] + 1 ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] NEW_LINE DEDENT...
Maximum frequency of any array element possible by at most K increments | Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array...
def maxFrequency ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE sum = 0 NEW_LINE res = 0 NEW_LINE for end in range ( N ) : NEW_LINE INDENT sum += arr [ end ] NEW_LINE while ( ( end - start + 1 ) * arr [ end ] - sum > K ) : NEW_LINE INDENT sum -= arr [ start ] NEW_LINE star...
Count pairs ( i , j ) from an array such that i < j and arr [ j ] | Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ...
def countPairs ( arr , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - x * i ) in mp ) : NEW_LINE INDENT mp [ arr [ i ] - x * i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] - x * i ] = 1 NEW_LINE DEDENT DEDENT for key , value in mp...
Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores t...
def recreationalSpot ( arr , N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT preMin = [ 0 ] * N NEW_LINE preMin [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preMin [ i ] = min ( preMin [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT stack = [ ] NEW_LINE for j in range...
Check if a number N can be expressed as the sum of powers of X or not | Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver Code
def ToCheckPowerofX ( n , x ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT rem = n % x NEW_LINE if ( rem >= 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // x NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE X = 3 NEW_LINE if ( ToCheckPowero...
Find the maximum between N and the number formed by reversing 32 | Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating ...
def reverseBin ( N ) : NEW_LINE INDENT S = " " NEW_LINE i = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( N & ( 1 << i ) ) : NEW_LINE INDENT S += '1' NEW_LINE DEDENT else : NEW_LINE INDENT S += '0' NEW_LINE DEDENT DEDENT S = list ( S ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = ' ' . join ( S ) NEW_LINE M = 0 NE...
Sum of Euler Totient Functions obtained for each divisor of N | Function to find the sum of Euler Totient Function of divisors of N ; Return the value of N ; Driver Code
def sumOfDivisors ( N ) : NEW_LINE INDENT return N NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( sumOfDivisors ( N ) ) NEW_LINE DEDENT
Square root of two Complex Numbers | Python3 program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ;...
from math import sqrt NEW_LINE def complexRoot ( A , B ) : NEW_LINE INDENT ans = [ ] NEW_LINE X1 = abs ( sqrt ( ( A + sqrt ( A * A + B * B ) ) / 2 ) ) NEW_LINE Y1 = B / ( 2 * X1 ) NEW_LINE ans . append ( [ X1 , Y1 ] ) NEW_LINE X2 = - 1 * X1 NEW_LINE Y2 = B / ( 2 * X2 ) NEW_LINE if ( X2 != 0 ) : NEW_LINE INDENT ans . ap...
Minimum time required to schedule K processes | Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; De...
def minTime ( A , n , K ) : NEW_LINE INDENT max_ability = A [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT max_ability = max ( max_ability , A [ i ] ) NEW_LINE DEDENT tmp = [ 0 for i in range ( max_ability + 1 ) ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp [ A [ i ] ] += 1 NEW_LIN...
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array...
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 findlcm ( arr , n ) : NEW_LINE INDENT ans = arr [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) NEW_LINE DEDENT ...
Check if N can be represented as sum of positive integers containing digit D at least once | Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of inte...
def findDigit ( N , D ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT a = N % 10 NEW_LINE if ( a == D ) : NEW_LINE INDENT return True NEW_LINE DEDENT N /= 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def check ( N , D ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT if ( findDigit ( N , D ) == True ) : NE...
Number of relations that are neither Reflexive nor Irreflexive on a Set | Python program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 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 res ; Divide y by...
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT 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 % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DE...
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment th...
def minOperations ( N ) : NEW_LINE INDENT arr = [ 0 ] * N NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr [ i ] = ( 2 * i ) + 1 NEW_LINE sum = sum + arr [ i ] NEW_LINE DEDENT mid = 0 NEW_LINE if N % 2 == 0 : NEW_LINE INDENT mid = sum / N NEW_LINE DEDENT else : NEW_LINE INDENT mid = arr [ int ( N / ...
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver Code
def minOperation ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT return ( N / 2 ) * ( N / 2 ) NEW_LINE DEDENT k = ( N - 1 ) / 2 NEW_LINE return ( k * ( k + 1 ) ) NEW_LINE DEDENT N = 6 NEW_LINE print ( int ( minOperation ( N ) ) ) NEW_LINE
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] ...
def findXORS ( arr1 , arr2 , N , M ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT temp = arr1 [ i ] & arr2 [ j ] NEW_LINE res ^= temp NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 3...
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ;...
def findXORS ( arr1 , arr2 , N , M ) : NEW_LINE INDENT XORS1 = 0 NEW_LINE XORS2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT XORS1 ^= arr1 [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT XORS2 ^= arr2 [ i ] NEW_LINE DEDENT return XORS1 and XORS2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_...
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Function to check if a number can be expressed as a power of 2 ; if N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of...
def isPowerof2 ( n ) : NEW_LINE INDENT return ( ( n & ( n - 1 ) ) and n ) NEW_LINE DEDENT def countNum ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT flag = isPowerof2 ( i ) NEW_LINE if ( not flag ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE D...
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Python 3 program for the above approach ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
import math NEW_LINE def countNum ( N ) : NEW_LINE INDENT ans = int ( math . log2 ( N ) ) + 1 NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE countNum ( N ) NEW_LINE DEDENT
Replace array elements that contains K as a digit with the nearest power of K | Python3 program for the above approach ; Function to calculate the power of base nearest to x ; Stores logX to the base K ; Function to replace array elements with nearest power of K ; Traverse the array ; Convert integer into a string ; If...
import math NEW_LINE def nearestPow ( x , base ) : NEW_LINE INDENT k = int ( math . log ( x , base ) ) NEW_LINE if abs ( base ** k - x ) < abs ( base ** ( k + 1 ) - x ) : NEW_LINE INDENT return base ** k NEW_LINE DEDENT else : NEW_LINE INDENT return base ** ( k + 1 ) NEW_LINE DEDENT DEDENT def replaceWithNearestPowerOf...
Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | Python3 program for the above approach ; Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements...
from math import ceil NEW_LINE def ceilDifference ( arr , n , x ) : NEW_LINE INDENT totalSum = 0 NEW_LINE perElementSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE perElementSum += ceil ( arr [ i ] / x ) NEW_LINE DEDENT totalCeilSum = ceil ( totalSum / x ) NEW_LINE return abs ( pe...
Remove trailing zeros from the sum of two numbers ( Using Stack ) | Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0 ; Pop the top e...
def removeTailing ( A , B ) : NEW_LINE INDENT N = A + B NEW_LINE s = [ ] NEW_LINE strsum = str ( N ) NEW_LINE for i in range ( len ( strsum ) ) : NEW_LINE INDENT s . append ( strsum [ i ] ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT while ( s [ - 1 ] == '0' ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT res = " " NEW_LI...
Count number of triplets with product not exceeding a given number | Function to count the number of triplets whose product is at most N ; Stores the count of triplets ; Iterate over the range [ 0 , N ] ; Iterate over the range [ 0 , N ] ; If the product of pairs exceeds N ; Increment the count of possible triplets ; R...
def countTriplets ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i * j > N ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += N // ( i * j ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_...
Sum of an Infinite Geometric Progression ( GP ) | Function to calculate the sum of an infinite Geometric Progression ; Case for Infinite Sum ; Store the sum of GP Series ; Print the value of sum ; Driver Code
def findSumOfGP ( a , r ) : NEW_LINE INDENT if ( abs ( r ) >= 1 ) : NEW_LINE INDENT print ( " Infinite " ) NEW_LINE return NEW_LINE DEDENT sum = a / ( 1 - r ) NEW_LINE print ( int ( sum ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , R = 1 , 0.5 NEW_LINE findSumOfGP ( A , R ) NEW_LINE DEDENT
Number of Relations that are both Irreflexive and Antisymmetric on a Set | Python 3 program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the...
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE 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 numberOfRelations ( N ) : ...
Count of numbers up to N having at least one prime factor common with N | Python 3 program for the above approach ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the count of numbers having more than 1 factor with N ; Iterate over the range [ 1 , N ] ; If gcd...
import math NEW_LINE def countNumbers ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( math . gcd ( i , N ) != 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE countNumbe...
Count of numbers up to N having at least one prime factor common with N | Function to calculate the value of Euler 's totient function ; Initialize result with N ; Find all prime factors of N and subtract their multiples ; Check if p is a prime factor ; If found to be true , then update N and result ; If N has a prime ...
def phi ( N ) : NEW_LINE INDENT result = N NEW_LINE for p in range ( 2 , int ( pow ( N , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( N % p == 0 ) : NEW_LINE INDENT while ( N % p == 0 ) : NEW_LINE INDENT N = N / p NEW_LINE DEDENT result -= result // p NEW_LINE DEDENT DEDENT if ( N > 1 ) : NEW_LINE INDENT result -= result // ...
Queries to update array by adding or multiplying array elements and print the element present at specified index | Function to modify the array by performing given queries ; Stores the multiplication of all integers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ...
def Query ( arr , N , Q ) : NEW_LINE INDENT mul = 1 NEW_LINE add = 0 NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT add = add + Q [ i ] [ 1 ] NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT mul = mul * Q [ i ] [ 1 ] NEW_LINE add = add * Q [ i ] [ 1 ] NE...
Minimize cost of converting all array elements to Fibonacci Numbers | Python program for the above approach ; Function to find the N - th Fibonacci Number ; Find the value of a , b , and r ; Find the N - th Fibonacci ; Return the result ; Function to find the Fibonacci number which is nearest to X ; Calculate the value...
import math NEW_LINE def nthFibo ( n ) : NEW_LINE INDENT a = ( 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE b = ( - 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE r = 5 ** ( 1 / 2 ) NEW_LINE ans = ( a ** n - b ** n ) / r NEW_LINE return int ( ans ) NEW_LINE DEDENT def nearFibo ( X ) : NEW_LINE INDENT a = ( 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE n ...
Maximum sum of pairs that are at least K distance apart in an array | Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find th...
def getMaxPairSum ( arr , N , K ) : NEW_LINE INDENT preMax = [ 0 ] * N NEW_LINE preMax [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preMax [ i ] = max ( preMax [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT res = - 10 ** 8 NEW_LINE for i in range ( K , N ) : NEW_LINE INDENT res = max ( res , arr [ i ]...
Program to calculate sum of an Infinite Arithmetic | Function to find the sum of the infinite AGP ; Stores the sum of infinite AGP ; Print the required sum ; Driver Code
def sumOfInfiniteAGP ( a , d , r ) : NEW_LINE INDENT ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; NEW_LINE print ( round ( ans , 6 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , d , r = 0 , 1 , 0.5 NEW_LINE sumOfInfiniteAGP ( a , d , r ) NEW_LINE DEDENT
Count pairs from an array having GCD equal to the minimum element in the pair | Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] %...
def countPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ ==...
Count pairs from an array having GCD equal to the minimum element in the pair | Python3 program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Stores the frequency of each array element ; Traverse the array arr [ ] ; Iterate...
from math import sqrt NEW_LINE def CountPairs ( arr , N ) : NEW_LINE INDENT res = 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 DEDENT for key , value in mp . ...
Product of count of set bits present in binary representations of elements in an array | Function to count the set bits in an integer ; Stores the count of set bits ; Iterate while N is not equal to 0 ; Increment count by 1 ; Divide N by 2 ; Return the total count obtained ; Function to find the product of count of set...
def countbits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT n = n // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT def BitProduct ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT bits = co...
Queries to calculate sum of array elements consisting of odd number of divisors | Python3 program for the above approach ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Initialize the dp [ ] array ; Traverse the array , arr [ ] ; If a [ i ] is a perfect squa...
from math import sqrt NEW_LINE def OddDivisorsSum ( n , q , a , Query ) : NEW_LINE INDENT DP = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = sqrt ( a [ i ] ) NEW_LINE if ( x * x == a [ i ] ) : NEW_LINE INDENT DP [ i ] = a [ i ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT DP [ i ] =...
Smallest subset of maximum sum possible by splitting array into two subsets | Python 3 program for the above approach ; Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array...
from collections import defaultdict NEW_LINE def findSubset ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE mp = defaultdict ( int ) NEW_LINE totSum = 0 NEW_LINE s = 0 NEW_LINE flag = 0 NEW_LINE ans = [ ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT totSum += arr [ i ] NEW_LINE mp [ arr [ i ] ] = mp [ a...
Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K | Function to find minimum number of Bitwise XOR of array elements with 1 required to make sum of the array at least K ; Stores the count of even array elements ; Stores sum of the array ; Traverse the array arr [ ] ; Increment sum ;...
def minStepK ( arr , N , K ) : NEW_LINE INDENT E = 0 NEW_LINE S = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT E += 1 NEW_LINE DEDENT DEDENT if ( S >= K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( S + E < K ) : NEW_LINE INDENT return - 1 N...
Maximize difference between odd and even | Function to find maximum and minimum value of a number that can be obtained by rotating bits ; Stores the value of N ; Stores the maximum value ; Stores the minimum value ; If temp is odd ; Update the maximum and the minimum value ; If flag is 1 , then return the maximum value...
def Rotate ( n , f ) : NEW_LINE INDENT temp = n NEW_LINE maxi = n NEW_LINE mini = n NEW_LINE for idx in range ( 7 ) : NEW_LINE INDENT if temp & 1 : NEW_LINE INDENT temp >>= 1 NEW_LINE temp += 2 ** 7 NEW_LINE DEDENT else : NEW_LINE INDENT temp >>= 1 NEW_LINE DEDENT mini = min ( mini , temp ) NEW_LINE maxi = max ( maxi ,...
Array element with minimum sum of absolute differences | Set 2 | Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update ...
def minimumDiff ( arr , N ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE DEDENT min_diff = sum1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( sum1 - ( arr [ i ] * N ) ) < min_diff ) : NEW_LINE INDENT min_diff = abs ( sum1 - (...
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Stores the array elements ; Traverse...
def Sum ( arr , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT flag = False NEW_LINE i = 0 NEW_LINE lenn = len ( v ) NEW_LINE while ( i < lenn ) : NEW_LINE INDENT if ( flag == False and ( v [ i ] % K == 0 ) ) : NEW_LINE INDENT ...
Count array elements whose count of divisors is a prime number | Function to count the array elements whose count of divisors is prime ; Stores the maximum element ; Find the maximum element ; Store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; Base Case ; If i is a prime number ; Mark all multiples of i as n...
def primeDivisors ( arr , N ) : NEW_LINE INDENT K = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT K = max ( K , arr [ i ] ) NEW_LINE DEDENT prime = [ 0 ] * ( K + 1 ) NEW_LINE prime [ 0 ] = 1 NEW_LINE prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , K + 1 ) : NEW_LINE INDENT if ( not prime [ i ] ) : NEW_LIN...
Count prime numbers up to N that can be represented as a sum of two prime numbers | Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of...
def SieveOfEratosthenes ( n , prime ) : NEW_LINE INDENT prime [ 0 ] = 0 NEW_LINE prime [ 1 ] = 0 NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDEN...
Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | Function to find the minimum sum of the array such that Bitwise AND of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a ...
def findMinSum ( mat , N ) : NEW_LINE INDENT sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT res |= mat [ i ] [ j ] NEW_LINE DEDENT DEDENT sum1 += res NEW_LINE DEDENT return sum1 NEW_LINE DEDENT if __name__ == ' _ _ main _ ...
LCM of unique elements present in an array | Python3 program for the above approach ; Function to find GCD of two numbers ; Base Case ; Recursively find the GCD ; Function to find LCM of two numbers ; Function to find LCM of unique elements present in the array ; Stores the frequency of each number of the array ; Store...
from collections import defaultdict NEW_LINE def findGCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return findGCD ( b , a % b ) NEW_LINE DEDENT def findLCM ( a , b ) : NEW_LINE INDENT return ( a * b ) // findGCD ( a , b ) NEW_LINE DEDENT def uniqueElementsLCM ( arr , N ) : NEW...
Minimize maximum difference between adjacent elements possible by removing a single array element | Python3 program to implement the above approach ; Function to find maximum difference between adjacent array elements ; Store the maximum difference ; Traverse the array ; Update maximum difference ; Function to calculat...
import sys NEW_LINE def maxAdjacentDifference ( A ) : NEW_LINE INDENT diff = 0 NEW_LINE for i in range ( 1 , len ( A ) , 1 ) : NEW_LINE INDENT diff = max ( diff , A [ i ] - A [ i - 1 ] ) NEW_LINE DEDENT return diff NEW_LINE DEDENT def MinimumValue ( arr , N ) : NEW_LINE INDENT MinValue = sys . maxsize NEW_LINE for i in...
Number of Antisymmetric Relations on a set of N elements | Python3 program for the above approach ; Function to calculate the value of x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the res...
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE 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 antisymmetricRelation ( N ...
Number of Asymmetric Relations on a set of N elements | Python3 program for the above approach ; Function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; 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 ; Re...
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT 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 DEDEN...
Check if two vectors are collinear or not | Function to calculate cross product of two vectors ; Update cross_P [ 0 ] ; Update cross_P [ 1 ] ; Update cross_P [ 2 ] ; Function to check if two given vectors are collinear or not ; Store the first and second vectors ; Store their cross product ; Calculate their cross produ...
def crossProduct ( vect_A , vect_B , cross_P ) : NEW_LINE INDENT cross_P [ 0 ] = ( vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ) NEW_LINE cross_P [ 1 ] = ( vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ) NEW_LINE cross_P [ 2 ] = ( vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ) NEW...
Program to calculate Kinetic Energy and Potential Energy | Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code
def kineticEnergy ( M , V ) : NEW_LINE INDENT KineticEnergy = 0.5 * M * V * V NEW_LINE return KineticEnergy NEW_LINE DEDENT def potentialEnergy ( M , H ) : NEW_LINE INDENT PotentialEnergy = M * 9.8 * H NEW_LINE return PotentialEnergy NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = 5.5 NEW_LINE H =...
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | Function to count the number of pairs required to be removed from the array and replaced by their Bitwise OR values ; Stores the count of even array elements ; Traverse the given array ; Increment the count of even array elements ; ...
def countPairs ( arr , N ) : NEW_LINE INDENT even = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( N - even >= 1 ) : NEW_LINE INDENT print ( even ) NEW_LINE return NEW_LINE DEDENT print ( 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _...
Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | Function to find distribution of M objects among all array elements ; Stores the distribution of M objects ; Stores the indices of distribution ; Stores the remaining objects ; Iterate until rem is positive ; If the number...
def distribute ( N , K , M , arr ) : NEW_LINE INDENT distribution = [ 0 ] * N NEW_LINE ptr = K - 1 NEW_LINE rem = M NEW_LINE while ( rem > 0 ) : NEW_LINE INDENT if ( rem >= arr [ ptr ] ) : NEW_LINE INDENT distribution [ ptr ] += arr [ ptr ] NEW_LINE rem -= arr [ ptr ] NEW_LINE DEDENT else : NEW_LINE INDENT distribution...
Sum of squares of differences between all pairs of an array | Function to calculate sum of squares of differences of all possible pairs ; Stores the final sum ; Stores temporary values ; Traverse the array ; Final sum ; Prthe answer ; Driver Code ; Given array ; Size of the array ; Function call to find sum of square o...
def sumOfSquaredDifferences ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE sumA , sumB = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sumA += ( arr [ i ] * arr [ i ] ) NEW_LINE sumB += arr [ i ] NEW_LINE DEDENT sumA = N * sumA NEW_LINE sumB = ( sumB * sumB ) NEW_LINE ans = sumA - sumB NEW_LINE print ( ans ) NE...
Count ways to remove objects such that exactly M equidistant objects remain | Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iter...
def waysToRemove ( n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT print ( n ) NEW_LINE return NEW_LINE DEDENT d = 0 NEW_LINE while d >= 0 : NEW_LINE INDENT length = m + ( m - 1 ) * d NEW_LINE if ( length > n ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += ( n - length ) + 1 NEW_LINE d += 1 ...
Count unique stairs that can be reached by moving given number of steps forward or backward | Python3 program for the above approach ; Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; Pu...
from collections import deque NEW_LINE def countStairs ( n , x , a , b ) : NEW_LINE INDENT vis = [ 0 ] * ( n + 1 ) NEW_LINE moves = [ + a , - a , + b , - b ] NEW_LINE q = deque ( ) NEW_LINE q . append ( x ) NEW_LINE vis [ x ] = 1 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT currentStair = q . popleft ( ) NEW_LINE...
Count smaller elements present in the array for each array element | Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver ...
def smallerNumbers ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LI...
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | Function to calculate GCD of two numbers ; Base Case ; Calculate GCD recursively ; Function to count the minimum number of divisions required to make X and Y equal ; Calculate GCD of X and Y ; Divide X and Y by their GCD ; Stores the number ...
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 minimumOperations ( X , Y ) : NEW_LINE INDENT GCD = gcd ( X , Y ) NEW_LINE X = X // GCD NEW_LINE Y = Y // GCD NEW_LINE count = 0 NEW_LINE while ( X != Y ) : NEW_LINE INDENT if ( Y > ...
Minimum increments or decrements required to convert a sorted array into a power sequence | Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ...
def minOperations ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += a [ i ] NEW_LINE DEDENT ans -= n NEW_LINE x = 1 NEW_LINE while ( 1 ) : NEW_LINE INDENT curPow = 1 NEW_LINE curCost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT curCost += abs ( a [ i ] - curPow ) NEW_LINE...
Find the absolute difference between the nearest powers of two given integers for every array element | Python3 program for the above approach ; Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ...
import math NEW_LINE def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def nearestPowerDiff ( arr , N , a , b ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT log_a = int ( math . log ( arr [ i ] ) / math . log ( a ) ) ...
Count subarrays having even Bitwise OR | Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of...
def bitOr ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE length = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( length != 0 ) : NEW_LINE INDENT count += ( ( length ) * ( length + 1 ) ) // 2 NEW_LINE DEDENT length =...
Generate an array having sum of Bitwise OR of same | Function to print the resultant array ; Stores the sum of the array ; Calculate sum of the array ; If sum > K ; Not possible to construct the array ; Update K ; Stores the resultant array ; Traverse the array ; Stores the current element ; If jth bit is not set and v...
def constructArr ( A , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT if ( sum > K ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT K -= sum NEW_LINE B = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr = A [ i ] NEW_LINE j = 32 ...
Find all pairs raised to power K differs by exactly N | Function to prpairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ;...
def ValidPairs ( X , K ) : NEW_LINE INDENT count = 0 NEW_LINE for A in range ( - 1000 , 1001 , 1 ) : NEW_LINE INDENT for B in range ( - 1000 , 1001 , 1 ) : NEW_LINE INDENT if ( pow ( A , K ) - pow ( B , K ) == X ) : NEW_LINE INDENT count += 1 NEW_LINE print ( A , B ) NEW_LINE DEDENT DEDENT DEDENT if ( count == 0 ) : NE...
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ;...
def maximumAND ( arr , n , m ) : NEW_LINE INDENT tot = 1 << n NEW_LINE mx = 0 NEW_LINE for bm in range ( tot ) : NEW_LINE INDENT andans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( bm >> i ) & 1 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT andans = arr [ i ] NEW_LINE DEDENT e...
Calculate sum of the array generated by given operations | Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the st...
def findTotalSum ( ops ) : NEW_LINE INDENT if ( len ( ops ) == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT pts = [ ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ( ops ) ) : NEW_LINE INDENT if ( ops [ i ] == " C " ) : NEW_LINE INDENT ans -= pts [ - 1 ] NEW_LINE pts . pop ( ) NEW_LINE DEDENT elif...
XOR of major diagonal elements of a 3D Matrix | Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
def findXOR ( mat , N ) : NEW_LINE INDENT XOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT for k in range ( N ) : NEW_LINE INDENT if ( ( i == j and j == k ) ) : NEW_LINE INDENT XOR ^= mat [ i ] [ j ] [ k ] NEW_LINE XOR ^= mat [ i ] [ j ] [ N - k - 1 ] NEW_LINE DEDENT DEDENT...
XOR of major diagonal elements of a 3D Matrix | Function to find the Bitwise XOR of both diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
def findXOR ( mat , N ) : NEW_LINE INDENT XOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT XOR ^= mat [ i ] [ i ] [ i ] NEW_LINE XOR ^= mat [ i ] [ i ] [ N - i - 1 ] NEW_LINE DEDENT print ( XOR ) NEW_LINE DEDENT mat = [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] NEW_LINE N = len ( mat ) NEW_LINE fin...
Count of subtrees possible from an N | Python3 program of the above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the roo...
MAX = 300004 NEW_LINE graph = [ [ ] for i in range ( MAX ) ] NEW_LINE mod = 10 ** 9 + 7 NEW_LINE ans = 0 NEW_LINE def countSubtreesUtil ( cur , par ) : NEW_LINE INDENT global mod , ans NEW_LINE res = 1 NEW_LINE for i in range ( len ( graph [ cur ] ) ) : NEW_LINE INDENT v = graph [ cur ] [ i ] NEW_LINE if ( v == par ) :...
Find the player who wins the game of placing alternate + and | Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
def checkWinner ( arr , N ) : NEW_LINE INDENT diff = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT diff -= arr [ i ] NEW_LINE DEDENT if ( diff % 2 == 0 ) : NEW_LINE INDENT print ( " A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ...
Generate an array having sum of Euler Totient Function of all elements equal to N | Python3 program for the above approach ; Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; ...
from math import sqrt NEW_LINE def constructArray ( N ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( 1 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE if ( N != ( i * i ) ) : NEW_LINE INDENT ans . append ( N / i ) NEW_LINE DEDENT DEDENT DEDENT fo...
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row
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 maximumRowValue ( n , m ) : NEW_LINE INDENT return gcd ( n , m ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 2 NEW_LINE print ( maximumRowVal...
Program to generate an array having convolution of two given arrays | Python3 program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c ...
MOD = 998244353 NEW_LINE def findConvolution ( a , b ) : NEW_LINE INDENT global MOD NEW_LINE n , m = len ( a ) , len ( b ) NEW_LINE c = [ 0 ] * ( n + m - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD NEW_LINE DEDENT DEDENT for k in ...
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | Python3 program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat...
P = 51 NEW_LINE def count1s ( start_i , start_j , end_i , end_j , mat ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( start_i , end_i ) : NEW_LINE INDENT for y in range ( start_j , end_j ) : NEW_LINE INDENT if ( mat [ x ] [ y ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_L...
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 ; Iterate till x is N ; If x is even then return true ; Increment x ; If N is not a power of 2 ; Driver Code
def checkEvenPower ( n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( x < n ) : NEW_LINE INDENT value = pow ( 2 , x ) NEW_LINE if ( value == n ) : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x += 1 NEW_LINE DEDENT return False NE...
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 ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver code
def checkEvenPower ( n ) : NEW_LINE INDENT low , high = 0 , n NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 NEW_LINE value = pow ( 2 , mid ) NEW_LINE if ( value == n ) : NEW_LINE INDENT if ( mid % 2 == 1 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " ...
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 ...
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 ...
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 ; S...
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 [ ...
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 ; Calcula...
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 ...
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 ...
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_LIN...
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 ; Retu...
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 ) % ...
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 ...
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 ) ...
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 ;...
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 INDEN...
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 ...
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 DED...
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 wi...
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...
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 p...
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 // ...
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 ; Sto...
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 isPron...
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 previo...
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 -...
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 -...
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 ...
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 ; Driv...
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 ...
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...
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 ...