codeparrot/xlcost-text-to-code huggingface dataset:
Minimum size of Array possible with given sum and product values | Function to find the minimum size of array with sum S and product P ; Base Case ; Iterate through all values of S and check the mentioned condition ; Otherwise , print " - 1" ; Driver Code
def minimumSizeArray ( S , P ) : NEW_LINE INDENT if ( S == P ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( 2 , S + 1 ) : NEW_LINE INDENT d = i NEW_LINE if ( ( S / d ) >= pow ( P , 1.0 / d ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = 5 NEW_LINE P = 6 NEW_LINE print ( minimumSizeArray ( S , P ) ) NEW_LINE DEDENT
Smallest possible integer to be added to N | Function to find the smallest positive integer X such that X is added to every element of A except one element to give array B ; Stores the unique elements of array A ; Insert A [ i ] into the set s ; Sort array A [ ] ; Sort array B [ ] ; Assume X value as B [ 0 ] - A [ 1 ] ; Check if X value assumed is negative or 0 ; Update the X value to B [ 0 ] - A [ 0 ] ; If assumed value is wrong ; Update X value ; Driver Code
def findVal ( A , B , N ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT s . add ( A [ i ] ) NEW_LINE DEDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE X = B [ 0 ] - A [ 1 ] NEW_LINE if ( X <= 0 ) : NEW_LINE INDENT X = B [ 0 ] - A [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT if ( B [ i ] - X not in s ) : NEW_LINE INDENT X = B [ 0 ] - A [ 0 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( X ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 3 , 8 ] NEW_LINE B = [ 15 , 8 , 11 ] NEW_LINE N = len ( A ) NEW_LINE findVal ( A , B , N ) NEW_LINE DEDENT
Count of integral points that lie at a distance D from origin | python 3 program for the above approach ; Function to find the total valid integer coordinates at a distance D from origin ; Stores the count of valid points ; Iterate over possibel x coordinates ; Find the respective y coordinate with the pythagoras theorem ; Adding 4 to compensate the coordinates present on x and y axes . ; Return the answer ; Driver Code
from math import sqrt NEW_LINE def countPoints ( D ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( 1 , int ( sqrt ( D * D ) ) , 1 ) : NEW_LINE INDENT y = int ( sqrt ( ( D * D - x * x ) ) ) NEW_LINE if ( x * x + y * y == D * D ) : NEW_LINE INDENT count += 4 NEW_LINE DEDENT DEDENT count += 4 NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT D = 5 NEW_LINE print ( countPoints ( D ) ) NEW_LINE DEDENT
Sum of the shortest distance between all 0 s to 1 in given binary string | Function to find the total sum of the shortest distance between every 0 to 1 in a given binary string ; Stores the prefix distance and suffix distance from 0 to 1 ; Stores the current distance from 1 to 0 ; Marks the 1 ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign prefixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update prefixDistance [ i ] ; Assign prefixDistance [ i ] as INT_MAX ; Assign haveOne as false ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign the suffixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update suffixDistance [ i ] as cnt ; Assign suffixDistance [ i ] as INT_MAX ; Stores the total sum of distances between 0 to nearest 1 ; If current character is 0 ; Update the value of sum ; Print the value of the sum ; Driver Code
INT_MAX = 2147483647 NEW_LINE def findTotalDistance ( S , N ) : NEW_LINE INDENT prefixDistance = [ 0 for _ in range ( N ) ] NEW_LINE suffixDistance = [ 0 for _ in range ( N ) ] NEW_LINE cnt = 0 NEW_LINE haveOne = False NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT haveOne = True NEW_LINE cnt = 0 NEW_LINE prefixDistance [ i ] = 0 NEW_LINE DEDENT elif ( haveOne ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE prefixDistance [ i ] = cnt NEW_LINE DEDENT else : NEW_LINE INDENT prefixDistance [ i ] = INT_MAX NEW_LINE DEDENT DEDENT haveOne = False NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT haveOne = True NEW_LINE cnt = 0 NEW_LINE suffixDistance [ i ] = 0 NEW_LINE DEDENT elif ( haveOne ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE suffixDistance [ i ] = cnt NEW_LINE DEDENT else : NEW_LINE INDENT suffixDistance [ i ] = INT_MAX NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT sum += min ( prefixDistance [ i ] , suffixDistance [ i ] ) NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "100100" NEW_LINE N = len ( S ) NEW_LINE findTotalDistance ( S , N ) NEW_LINE DEDENT
Minimize value of | A | Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code
def minimizeCost ( A , B , C ) : NEW_LINE INDENT ans = A + B + C NEW_LINE for i in range ( 1 , 2 * C + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( i * j <= 2 * C ) : NEW_LINE INDENT ans = min ( ans , abs ( A - i ) + abs ( B - j ) + abs ( i * j - C ) ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = 19 NEW_LINE B = 28 NEW_LINE C = 522 NEW_LINE print ( minimizeCost ( A , B , C ) ) NEW_LINE
Average value of set bit count in given Binary string after performing all possible choices of K operations | Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array arr [ ] and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code
def averageSetBits ( N , K , arr ) : NEW_LINE INDENT p = N NEW_LINE q = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT _p = p NEW_LINE _q = q NEW_LINE p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N NEW_LINE q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N NEW_LINE DEDENT return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE arr = [ 1 , 2 , 3 ] NEW_LINE K = len ( arr ) NEW_LINE print ( " % .2f " % averageSetBits ( N , K , arr ) ) NEW_LINE DEDENT
Find maximum sum of subsequence after flipping signs of at most K elements in given Array | Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Driver Code ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence
def maxSubseq ( arr , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT arr [ i ] = - arr [ i ] NEW_LINE K -= 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , - 10 , - 1 , 0 , - 4 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( maxSubseq ( arr , N , K ) ) NEW_LINE DEDENT
Minimum number of intervals to cover the target interval | Function to find the minimum number of intervals in the array A [ ] to cover the entire target interval ; Sort the array A [ ] in increasing order of starting point ; Insert a pair of INT_MAX to prevent going out of bounds ; Stores start of current interval ; Stores end of current interval ; Stores the count of intervals ; Iterate over all the intervals ; If starting point of current index <= start ; Update the value of start ; Increment the value of count ; If the target interval is already covered or it is not possible to move then break the loop ; If the entire target interval is not covered ; Return Answer ; Driver Code
def minimizeSegment ( A , X ) : NEW_LINE INDENT A . sort ( ) NEW_LINE INT_MAX = 2147483647 NEW_LINE A . append ( [ INT_MAX , INT_MAX ] ) NEW_LINE start = X [ 0 ] NEW_LINE end = X [ 0 ] - 1 NEW_LINE cnt = 0 NEW_LINE for i in range ( 0 , len ( A ) ) : NEW_LINE INDENT if ( A [ i ] [ 0 ] <= start ) : NEW_LINE INDENT end = max ( A [ i ] [ 1 ] , end ) NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = end NEW_LINE cnt = cnt + 1 NEW_LINE if ( A [ i ] [ 0 ] > end or end >= X [ 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( end < X [ 1 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 10 ] , [ 2 , 3 ] , [ 1 , 1 ] ] NEW_LINE X = [ 1 , 10 ] NEW_LINE print ( minimizeSegment ( A , X ) ) NEW_LINE DEDENT
K | Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
def KthSolution ( X , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , 64 ) : NEW_LINE INDENT if ( not ( X & ( 1 << i ) ) ) : NEW_LINE INDENT if ( K & 1 ) : NEW_LINE INDENT ans |= ( 1 << i ) NEW_LINE DEDENT K >>= 1 NEW_LINE if ( not K ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 5 NEW_LINE K = 5 NEW_LINE print ( KthSolution ( X , K ) ) NEW_LINE DEDENT
Rearrange sorted array such that all odd indices elements comes before all even indices element | Function to print the array ; Function to rearrange the array such that odd indexed elements come before even indexed elements ; Reduces the size of array by one because last element does not need to be changed in case N = odd ; Initialize the variables ; Iterate over the range ; Add the modified element at the odd position ; Iterate over the range ; Add the modified element at the even position ; Iterate over the range ; Divide by the maximum element ; Driver Code
def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def rearrange ( arr , N ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT N -= 1 NEW_LINE DEDENT odd_idx = 1 NEW_LINE even_idx = 0 NEW_LINE max_elem = arr [ N - 1 ] + 1 NEW_LINE for i in range ( N // 2 ) : NEW_LINE INDENT arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem NEW_LINE odd_idx += 2 NEW_LINE DEDENT for i in range ( N // 2 , N ) : NEW_LINE INDENT arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem NEW_LINE even_idx += 2 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // max_elem NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 ] NEW_LINE N = len ( arr ) NEW_LINE rearrange ( arr , N ) NEW_LINE printArray ( arr , N ) ; NEW_LINE DEDENT
Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | Python 3 program for the above approach ; Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver Code
from math import ceil , floor NEW_LINE import sys NEW_LINE def findRange ( arr , N , K ) : NEW_LINE INDENT L = - sys . maxsize - 1 NEW_LINE R = sys . maxsize NEW_LINE for i in range ( N ) : NEW_LINE INDENT l = ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) NEW_LINE r = ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) - 1 ) NEW_LINE L = max ( L , l ) NEW_LINE R = min ( R , r ) NEW_LINE DEDENT print ( L , R ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 9 , 11 ] NEW_LINE K = 10 NEW_LINE N = len ( arr ) NEW_LINE findRange ( arr , N , K ) NEW_LINE DEDENT
Find an anagram of given String having different characters at corresponding indices | Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code
def findAnagram ( s ) : NEW_LINE INDENT check = s NEW_LINE st = list ( s ) NEW_LINE i = 0 NEW_LINE j = len ( st ) - 1 NEW_LINE while ( i < len ( st ) and j >= 0 ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] and check [ i ] != st [ j ] and check [ j ] != st [ i ] ) : NEW_LINE INDENT st [ i ] , st [ j ] = st [ j ] , st [ i ] NEW_LINE i += 1 NEW_LINE j = len ( st ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT if ( len ( st ) % 2 != 0 ) : NEW_LINE INDENT mid = len ( st ) / 2 NEW_LINE if ( check [ mid ] == st [ mid ] ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( check [ i ] != st [ mid ] and st [ i ] != st [ mid ] ) : NEW_LINE INDENT st [ i ] , st [ mid ] = st [ mid ] , st [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT ok = True NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( check [ i ] == st [ i ] ) : NEW_LINE INDENT ok = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok ) : NEW_LINE INDENT print ( " " . join ( st ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geek " NEW_LINE findAnagram ( S ) NEW_LINE DEDENT
Lexicographically smallest permutation of [ 1 , N ] based on given Binary string | Function to generate the lexicographically smallest permutation according to the given criteria ; Stores the resultant permutation ; Initialize the first elements to 1 ; Traverse the given string S ; Number greater than last number ; Number equal to the last number ; Correct all numbers to the left of the current index ; Printing the permutation ; Driver Code
def constructPermutation ( S , N ) : NEW_LINE INDENT ans = [ 0 ] * N NEW_LINE ans [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( S [ i - 1 ] == '0' ) : NEW_LINE INDENT ans [ i ] = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans [ i ] = ans [ i - 1 ] NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT if ( ans [ j ] >= ans [ i ] ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE if ( i != N - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT S = "100101" NEW_LINE constructPermutation ( S , len ( S ) + 1 ) NEW_LINE
Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X | Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
def KthSolution ( X , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 64 ) : NEW_LINE INDENT if not ( X & ( 1 << i ) ) : NEW_LINE INDENT if ( K & 1 ) : NEW_LINE INDENT ans |= ( 1 << i ) NEW_LINE DEDENT K >>= 1 NEW_LINE if not K : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT X = 10 NEW_LINE K = 5 NEW_LINE print ( KthSolution ( X , K ) ) NEW_LINE
Count of distinct N | Function to find the count of distinct arrays of size n having elements in range [ 1 , k ] and all adjacent elements ( P , Q ) follows ( P <= Q ) or ( P % Q > 0 ) ; Stores the divisors of all integers in the range [ 1 , k ] ; Calculate the divisors of all integers using the Sieve ; Stores the dp states such that dp [ i ] [ j ] with i elements having j as the last element of array ; Initialize the dp array ; Calculate the dp states using the derived relation ; Calculate the sum for len - 1 ; Subtract dp [ len - 1 ] [ j ] for each factor of j from [ 1 , K ] ; Calculate the final result ; Return the resultant sum ; Driver Code
def countArrays ( n , k ) : NEW_LINE INDENT divisors = [ [ ] for i in range ( k + 1 ) ] NEW_LINE for i in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT for j in range ( 2 * i , k + 1 , i ) : NEW_LINE INDENT divisors [ j ] . append ( i ) NEW_LINE DEDENT DEDENT dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for j in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT dp [ 1 ] [ j ] = 1 NEW_LINE DEDENT for x in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT sum += dp [ x - 1 ] [ j ] NEW_LINE DEDENT for y in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT dp [ x ] [ y ] = sum NEW_LINE for d in divisors [ y ] : NEW_LINE INDENT dp [ x ] [ y ] = ( dp [ x ] [ y ] - dp [ x - 1 ] [ d ] ) NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for j in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT sum += dp [ n ] [ j ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE K = 3 NEW_LINE print ( countArrays ( N , K ) ) NEW_LINE DEDENT
Minimize increment | Function to calculate the minimum number of operations to convert array A to array B by incrementing and decrementing adjacent elements ; Stores the final count ; Stores the sum of array A and B respectivelly ; Check of the sums are unequall ; Pointer to iterate through array ; Case 1 where A [ i ] > B [ i ] ; Stores the extra values for the current index ; Iterate the array from [ i - 1 , 0 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Iterate the array in right direction id A [ i ] - B [ i ] > 0 ; Iterate the array from [ i + 1 , n - 1 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Return Answer ; Driver Code ; Function Call
def minimumMoves ( A , B , N ) : NEW_LINE INDENT ans = 0 NEW_LINE sum_A = 0 NEW_LINE sum_B = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum_A += A [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT sum_B += B [ i ] NEW_LINE DEDENT if ( sum_A != sum_B ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT if ( A [ i ] > B [ i ] ) : NEW_LINE INDENT temp = A [ i ] - B [ i ] NEW_LINE j = i - 1 NEW_LINE while ( j >= 0 and temp > 0 ) : NEW_LINE INDENT if ( B [ j ] > A [ j ] ) : NEW_LINE INDENT cnt = min ( temp , ( B [ j ] - A [ j ] ) ) NEW_LINE A [ j ] += cnt NEW_LINE temp -= cnt NEW_LINE ans += ( cnt * abs ( j - i ) ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT if ( temp > 0 ) : NEW_LINE INDENT j = i + 1 NEW_LINE while ( j < N and temp > 0 ) : NEW_LINE INDENT if ( B [ j ] > A [ j ] ) : NEW_LINE INDENT cnt = min ( temp , ( B [ j ] - A [ j ] ) ) NEW_LINE A [ j ] += cnt NEW_LINE temp -= cnt NEW_LINE ans += ( cnt * abs ( j - i ) ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 5 , 7 ] NEW_LINE B = [ 13 , 0 , 0 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumMoves ( A , B , N ) ) NEW_LINE DEDENT
Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | Function to check the above problem condition ; Check the base case . ; check if X - T is a divisor of D - 1 ; Driver code
def possibleReachingSequence ( X , D , T ) : NEW_LINE INDENT if X < T : NEW_LINE INDENT return " NO " NEW_LINE DEDENT if T * D < X : NEW_LINE INDENT return " NO " NEW_LINE DEDENT if ( X - T ) % ( D - 1 ) == 0 : NEW_LINE INDENT return " YES " NEW_LINE DEDENT return " NO " NEW_LINE DEDENT X = 10 NEW_LINE D = 3 NEW_LINE T = 6 NEW_LINE print ( possibleReachingSequence ( X , D , T ) ) NEW_LINE
Maximum number of times Array can be reduced in half when its all elements are even | Python 3 code implementation for the above approach ; Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code
import sys NEW_LINE def arrayDivisionByTwo ( arr , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // 2 NEW_LINE cnt += 1 NEW_LINE DEDENT ans = min ( ans , cnt ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 12 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE print ( arrayDivisionByTwo ( arr , n ) ) NEW_LINE DEDENT
Find smallest number with given digits and sum of digits | Function to print minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and Q ; If P and Q are both zero or if Q is zero and N is not divisible by P then there is no possible integer which satisfies the given conditions ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver Code ; Function Call
def printMinInteger ( P , Q , N ) : NEW_LINE INDENT if ( Q > P ) : NEW_LINE INDENT t = P NEW_LINE P = Q NEW_LINE Q = t NEW_LINE DEDENT if ( Q == 0 and ( P == 0 or N % P != 0 ) ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT count_P = 0 NEW_LINE count_Q = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % P == 0 ) : NEW_LINE INDENT count_P += N / P NEW_LINE N = 0 NEW_LINE DEDENT else : NEW_LINE INDENT N = N - Q NEW_LINE count_Q += 1 NEW_LINE DEDENT DEDENT if ( N == 0 ) : NEW_LINE INDENT for i in range ( count_Q ) : NEW_LINE INDENT print ( Q , end = " " ) NEW_LINE DEDENT for i in range ( int ( count_P ) ) : NEW_LINE INDENT print ( P , end = " " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT N = 32 NEW_LINE P = 7 NEW_LINE Q = 4 NEW_LINE printMinInteger ( P , Q , N ) NEW_LINE
Minimum number of sum and modulo operations using given numbers to reach target | Function to find the minimum moves to reach K from N ; Initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; Traversing through the buttons ; Iterating through all the positions ; If not visited ; Next status of lock ; Advance to next state ; Return the final dp [ target ] ; Given Input ; Function Call
def minPushes ( N , K , arr ) : NEW_LINE INDENT dp = [ - 1 ] * 100000 NEW_LINE dp [ N ] = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT for xx in range ( 100000 ) : NEW_LINE INDENT x = xx NEW_LINE if ( dp [ x ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT next = ( x + arr [ i ] ) % 100000 NEW_LINE while ( dp [ next ] == - 1 or dp [ next ] > dp [ x ] + 1 ) : NEW_LINE INDENT dp [ next ] = dp [ x ] + 1 NEW_LINE x = next NEW_LINE next = ( next + arr [ i ] ) % 100000 NEW_LINE DEDENT DEDENT DEDENT return dp [ K ] NEW_LINE DEDENT N = 99880 NEW_LINE K = 89 NEW_LINE arr = [ 100 , 3 ] NEW_LINE print ( minPushes ( N , K , arr ) ) NEW_LINE