text
stringlengths
1
636
code
stringlengths
8
1.89k
Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array
def closetAND ( arr , n , k ) : NEW_LINE INDENT ans = sys . maxsize ; NEW_LINE DEDENT
Check all possible sub - arrays
for i in range ( n ) : NEW_LINE INDENT X = arr [ i ] ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT X &= arr [ j ] ; NEW_LINE DEDENT DEDENT
Find the overall minimum
ans = min ( ans , abs ( k - X ) ) ; NEW_LINE
No need to perform more AND operations as | k - X | will increase
if ( X <= k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT return ans ; NEW_LINE
Driver code
arr = [ 4 , 7 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( closetAND ( arr , n , k ) ) ; NEW_LINE
Function to return the gcd of a and b
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
Function to return the count of quadruplets having gcd = k
def countQuadruplets ( l , r , k ) : NEW_LINE INDENT frequency = [ 0 ] * ( r + 1 ) ; NEW_LINE DEDENT
Count the frequency of every possible gcd value in the range
for i in range ( l , r + 1 ) : NEW_LINE INDENT for j in range ( l , r + 1 ) : NEW_LINE INDENT frequency [ gcd ( i , j ) ] += 1 ; NEW_LINE DEDENT DEDENT
To store the required count
answer = 0 ; NEW_LINE
Calculate the answer using frequency values
for i in range ( l , r + 1 ) : NEW_LINE INDENT for j in range ( l , r + 1 ) : NEW_LINE INDENT if ( gcd ( i , j ) == k ) : NEW_LINE INDENT answer += ( frequency [ i ] * frequency [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT
Return the required count
return answer ; NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l , r , k = 1 , 10 , 2 ; NEW_LINE print ( countQuadruplets ( l , r , k ) ) ; NEW_LINE DEDENT
Function to print the re - arranged array
def solve ( a , n ) : NEW_LINE INDENT ones , twos = 0 , 0 NEW_LINE DEDENT
Count the number of ones and twos in a [ ]
for i in range ( n ) : NEW_LINE
If the array element is 1
if ( a [ i ] == 1 ) : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT
Array element is 2
else : NEW_LINE INDENT twos += 1 NEW_LINE DEDENT ind = 0 NEW_LINE
If it has at least one 2 Fill up first 2
if ( twos ) : NEW_LINE INDENT a [ ind ] = 2 NEW_LINE ind += 1 NEW_LINE DEDENT
Decrease the cnt of ones if even
if ones % 2 == 0 : NEW_LINE INDENT evenOnes = True NEW_LINE DEDENT else : NEW_LINE INDENT evenOnes = False NEW_LINE DEDENT if ( evenOnes ) : NEW_LINE INDENT ones -= 1 NEW_LINE DEDENT
Fill up with odd count of ones
for i in range ( ones ) : NEW_LINE INDENT a [ ind ] = 1 NEW_LINE ind += 1 NEW_LINE DEDENT
Fill up with remaining twos
for i in range ( twos - 1 ) : NEW_LINE INDENT a [ ind ] = 2 NEW_LINE ind += 1 NEW_LINE DEDENT
If even ones , then fill last position
if ( evenOnes ) : NEW_LINE INDENT a [ ind ] = 1 NEW_LINE ind += 1 NEW_LINE DEDENT
Print the rearranged array
for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Driver code
a = [ 1 , 2 , 1 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE solve ( a , n ) NEW_LINE
Function to generate and print the required array
def CreateArray ( N , even , odd ) : NEW_LINE INDENT temp = - 1 NEW_LINE DEDENT
Find the number of odd prefix sums
for i in range ( N + 2 ) : NEW_LINE INDENT if ( i * ( ( N + 1 ) - i ) == odd ) : NEW_LINE INDENT temp = 0 NEW_LINE OddPreSums = i NEW_LINE break NEW_LINE DEDENT DEDENT
If no odd prefix sum found
if ( temp == - 1 ) : NEW_LINE INDENT print ( temp ) NEW_LINE DEDENT else : NEW_LINE
Calculating the number of even prefix sums
EvenPreSums = ( N + 1 ) - OddPreSums NEW_LINE e = 1 NEW_LINE o = 0 NEW_LINE
Stores the current prefix sum
CurrSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE
If current prefix sum is even
if ( CurrSum % 2 == 0 ) : NEW_LINE
Print 0 until e = EvenPreSums - 1
if ( e < EvenPreSums ) : NEW_LINE INDENT e += 1 NEW_LINE print ( "0 ▁ " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT o += 1 NEW_LINE DEDENT
Print 1 when e = EvenPreSums
print ( "1 ▁ " , end = " " ) NEW_LINE CurrSum += 1 NEW_LINE else : NEW_LINE if ( e < EvenPreSums ) : NEW_LINE e += 1 NEW_LINE print ( "1 ▁ " ) NEW_LINE CurrSum += 1 NEW_LINE else : NEW_LINE o += 1 NEW_LINE
Print 0 for rest of the values
print ( "0 ▁ " , end = " " ) NEW_LINE print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 NEW_LINE even = 60 NEW_LINE odd = 60 NEW_LINE CreateArray ( N , even , odd ) NEW_LINE DEDENT
Python3 implementation of the approach
import math NEW_LINE import sys NEW_LINE
Function to return the minimum number of operations required
def changeTheArray ( arr , n ) : NEW_LINE
Minimum and maximum elements from the array
minEle = min ( arr ) NEW_LINE maxEle = max ( arr ) NEW_LINE
To store the minimum number of operations required
minOperations = sys . maxsize NEW_LINE for num in range ( minEle , maxEle + 1 ) : NEW_LINE
To store the number of operations required to change every element to either ( num - 1 ) , num or ( num + 1 )
operations = 0 NEW_LINE for i in range ( n ) : NEW_LINE
If current element is not already num
if arr [ i ] != num : NEW_LINE
Add the count of operations required to change arr [ i ]
operations += ( abs ( num - arr [ i ] ) - 1 ) NEW_LINE
Update the minimum operations so far
minOperations = min ( minOperations , operations ) NEW_LINE return minOperations NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( changeTheArray ( arr , n ) ) NEW_LINE DEDENT
Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized
def findX ( A , B ) : NEW_LINE INDENT j = 0 NEW_LINE x = 0 NEW_LINE DEDENT
While either A or B is non - zero
while ( A or B ) : NEW_LINE
Position at which both A and B have a set bit
if ( ( A & 1 ) and ( B & 1 ) ) : NEW_LINE
Inserting a set bit in x
x += ( 1 << j ) NEW_LINE
Right shifting both numbers to traverse all the bits
A >>= 1 NEW_LINE B >>= 1 NEW_LINE j += 1 NEW_LINE return x NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 NEW_LINE B = 3 NEW_LINE X = findX ( A , B ) NEW_LINE print ( " X ▁ = " , X , " , ▁ Sum ▁ = " , ( A ^ X ) + ( B ^ X ) ) NEW_LINE DEDENT
finding X
def findX ( A , B ) : NEW_LINE INDENT return A & B NEW_LINE DEDENT
finding Sum
def findSum ( A , B ) : NEW_LINE INDENT return A ^ B NEW_LINE DEDENT
Driver code
A , B = 2 , 3 NEW_LINE print ( " X ▁ = " , findX ( A , B ) , " , ▁ Sum ▁ = " , findSum ( A , B ) ) NEW_LINE
Function that returns true if sum of first n - 1 elements of the array is equal to the last element
def isSumEqual ( ar , n ) : NEW_LINE INDENT sum = 0 NEW_LINE DEDENT
Find the sum of first n - 1 elements of the array
for i in range ( n - 1 ) : NEW_LINE INDENT sum += ar [ i ] NEW_LINE DEDENT
If sum equals to the last element
if ( sum == ar [ n - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isSumEqual ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Python3 implementation of the above approach
from math import sqrt , ceil , floor ; NEW_LINE
Function to count number of perfect squares
def perfectSquares ( a , b ) : NEW_LINE
Counting number of perfect squares between a and b
return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; NEW_LINE
Function to count number of 1 s in array after N moves
def countOnes ( arr , n ) : NEW_LINE INDENT return perfectSquares ( 1 , n ) ; NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Initialize array size
N = 10 ; NEW_LINE
Initialize all elements to 0
arr = [ 0 ] * 10 ; NEW_LINE print ( countOnes ( arr , N ) ) ; NEW_LINE
Python3 implementation of the approach
import bisect NEW_LINE
Function to print the position of each boxes where a ball has to be placed
def printPosition ( A , B , sizeOfA , sizeOfB ) : NEW_LINE
Find the cumulative sum of array A [ ]
for i in range ( 1 , sizeOfA ) : NEW_LINE INDENT A [ i ] += A [ i - 1 ] NEW_LINE DEDENT
Find the position of box for each ball
for i in range ( sizeOfB ) : NEW_LINE
Row number
row = bisect . bisect_left ( A , B [ i ] ) NEW_LINE
Column ( position of box in particular row )
if row >= 1 : NEW_LINE INDENT boxNumber = B [ i ] - A [ row - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT boxNumber = B [ i ] NEW_LINE DEDENT
Row + 1 denotes row if indexing of array start from 1
print ( row + 1 , " , " , boxNumber ) NEW_LINE
Driver code
A = [ 2 , 2 , 2 , 2 ] NEW_LINE B = [ 1 , 2 , 3 , 4 ] NEW_LINE sizeOfA = len ( A ) NEW_LINE sizeOfB = len ( B ) NEW_LINE printPosition ( A , B , sizeOfA , sizeOfB ) NEW_LINE
Python program to implement the above approach
import math NEW_LINE
Function to get the prime factors and its count of times it divides
def primeFactors ( n , freq ) : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT
Count the number of 2 s that divide n
while n % 2 == 0 : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE n = int ( n // 2 ) NEW_LINE DEDENT freq [ 2 ] = cnt NEW_LINE
n must be odd at this point . So we can skip one element ( Note i = i + 2 )
i = 3 NEW_LINE while i <= math . sqrt ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT
While i divides n , count i and divide n
while ( n % i == 0 ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE n = int ( n // i ) NEW_LINE DEDENT freq [ int ( i ) ] = cnt NEW_LINE i = i + 2 NEW_LINE
This condition is to handle the case when n is a prime number greater than 2
if ( n > 2 ) : NEW_LINE INDENT freq [ int ( n ) ] = 1 NEW_LINE DEDENT
Function to return the highest power
def getMaximumPower ( n , m ) : NEW_LINE
Initialize two arrays
freq1 = [ 0 ] * ( n + 1 ) NEW_LINE freq2 = [ 0 ] * ( m + 1 ) NEW_LINE
Get the prime factors of n and m
primeFactors ( n , freq1 ) NEW_LINE primeFactors ( m , freq2 ) NEW_LINE maxi = 0 NEW_LINE
Iterate and find the maximum power
i = 2 NEW_LINE while i <= m : NEW_LINE
If i not a prime factor of n and m
if ( freq1 [ i ] == 0 and freq2 [ i ] == 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE continue NEW_LINE DEDENT
If i is a prime factor of n and m If count of i dividing m is more than i dividing n , then power will be 0
if ( freq2 [ i ] > freq1 [ i ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
If i is a prime factor of M
if ( freq2 [ i ] ) : NEW_LINE
get the maximum power
maxi = max ( maxi , int ( freq1 [ i ] // freq2 [ i ] ) ) NEW_LINE i = i + 1 NEW_LINE return maxi NEW_LINE
Drivers code
n = 48 NEW_LINE m = 4 NEW_LINE print ( getMaximumPower ( n , m ) ) NEW_LINE
Function to find the number of divisors of all numbers in the range [ 1 , n ]
def findDivisors ( n ) : NEW_LINE
List to store the count of divisors
div = [ 0 for i in range ( n + 1 ) ] NEW_LINE
For every number from 1 to n
for i in range ( 1 , n + 1 ) : NEW_LINE
Increase divisors count for every number divisible by i
for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if j * i <= n : NEW_LINE INDENT div [ i * j ] += 1 NEW_LINE DEDENT DEDENT
Print the divisors
for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( div [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE findDivisors ( n ) NEW_LINE DEDENT
Function to decide the winner
def decideWinner ( a , n ) : NEW_LINE INDENT count0 = 0 NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE count3 = 0 NEW_LINE DEDENT
Iterate for all numbers in the array
for i in range ( n ) : NEW_LINE
If mod gives 0
if ( a [ i ] % 4 == 0 ) : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT
If mod gives 1
elif ( a [ i ] % 4 == 1 ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT
If mod gives 2
elif ( a [ i ] % 4 == 2 ) : NEW_LINE INDENT count2 += 1 NEW_LINE DEDENT
If mod gives 3
elif ( a [ i ] % 4 == 3 ) : NEW_LINE INDENT count3 += 1 NEW_LINE DEDENT
Check the winning condition for X
if ( count0 % 2 == 0 and count1 % 2 == 0 and count2 % 2 == 0 and count3 == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT
Driver code
a = [ 4 , 8 , 5 , 9 ] NEW_LINE n = len ( a ) NEW_LINE if ( decideWinner ( a , n ) == 1 ) : NEW_LINE INDENT print ( " X ▁ wins " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Y ▁ wins " ) NEW_LINE DEDENT
Function to return the count of total binary prefix which are divisible by x
def CntDivbyX ( arr , n , x ) : NEW_LINE