codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subarray which upon repetition gives the original array | Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
def printArray ( brr ) : NEW_LINE INDENT for it in brr : NEW_LINE INDENT print ( it , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def RepeatingSubarray ( arr , N ) : NEW_LINE INDENT if ( N < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT brr = [ ] NEW_LINE brr . append ( arr [ 0 ] ) NEW_LINE brr . append ( arr [ 1 ] ) NEW_LINE for i in range ( 2 , N // 2 + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT a = False NEW_LINE n = len ( brr ) NEW_LINE j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT K = j % i NEW_LINE if ( arr [ j ] == brr [ K ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT a = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not a and j == N ) : NEW_LINE INDENT printArray ( brr ) NEW_LINE return NEW_LINE DEDENT DEDENT brr . append ( arr [ i ] ) NEW_LINE DEDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE RepeatingSubarray ( arr , N ) NEW_LINE DEDENT
Find all missing numbers from a given sorted array | Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Given array arr [ ] ; Function call
def printMissingElements ( arr , N ) : NEW_LINE INDENT diff = arr [ 0 ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] - i != diff ) : NEW_LINE INDENT while ( diff < arr [ i ] - i ) : NEW_LINE INDENT print ( i + diff , end = " ▁ " ) NEW_LINE diff += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 6 , 7 , 10 , 11 , 13 ] NEW_LINE N = len ( arr ) NEW_LINE printMissingElements ( arr , N ) NEW_LINE
Find all missing numbers from a given sorted array | Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Given array arr [ ] ; Function call
def printMissingElements ( arr , N ) : NEW_LINE INDENT b = [ 0 ] * ( arr [ N - 1 ] + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT b [ arr [ i ] ] = 1 NEW_LINE DEDENT for i in range ( arr [ 0 ] , arr [ N - 1 ] + 1 ) : NEW_LINE INDENT if ( b [ i ] == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 6 , 7 , 10 , 11 , 13 ] NEW_LINE N = len ( arr ) NEW_LINE printMissingElements ( arr , N ) NEW_LINE
Least common element in given two Arithmetic sequences | Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield True in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
def smallestCommon ( a , b , c , d ) : NEW_LINE INDENT if ( a == c ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT if ( a > c ) : NEW_LINE INDENT swap ( a , c ) ; NEW_LINE swap ( b , d ) ; NEW_LINE DEDENT first_term_diff = ( c - a ) ; NEW_LINE possible_y = 0 ; NEW_LINE for possible_y in range ( b ) : NEW_LINE INDENT if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( possible_y != b ) : NEW_LINE INDENT return c + possible_y * d ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT def swap ( x , y ) : NEW_LINE INDENT temp = x ; NEW_LINE x = y ; NEW_LINE y = temp ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 ; B = 20 ; C = 19 ; D = 9 ; NEW_LINE print ( smallestCommon ( A , B , C , D ) ) ; NEW_LINE DEDENT
Minimum number of flips with rotation to make binary string alternating | Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Given String ; Length of given string ; Function call
def MinimumFlips ( s , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = 1 if s [ i ] == '1' else 0 NEW_LINE DEDENT oddone = [ 0 ] * ( n + 1 ) NEW_LINE evenone = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT oddone [ i + 1 ] = oddone [ i ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT oddone [ i + 1 ] = oddone [ i ] + 0 NEW_LINE DEDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT evenone [ i + 1 ] = evenone [ i ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT evenone [ i + 1 ] = evenone [ i ] + 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT oddone [ i + 1 ] = oddone [ i ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT oddone [ i + 1 ] = oddone [ i ] + 0 NEW_LINE DEDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT evenone [ i + 1 ] = evenone [ i ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT evenone [ i + 1 ] = evenone [ i ] + 0 NEW_LINE DEDENT DEDENT DEDENT minimum = min ( oddone [ n ] , evenone [ n ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT minimum = min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) NEW_LINE minimum = min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) NEW_LINE DEDENT DEDENT return minimum NEW_LINE DEDENT S = "000001100" NEW_LINE n = len ( S ) NEW_LINE print ( MinimumFlips ( S , n ) ) NEW_LINE
Create a Graph by connecting divisors from N to M and find shortest path | Python3 program for the above approach ; Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To avoid duplicate printing of same element ; Print the factors from n ; Driver Code ; Given N and M ; Function call
import math NEW_LINE def isprm ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def shortestpath ( m , n ) : NEW_LINE INDENT mfactor = [ ] NEW_LINE nfactor = [ ] NEW_LINE fre = dict . fromkeys ( range ( n + 1 ) , 0 ) NEW_LINE mfactor . append ( m ) NEW_LINE fre [ m ] = 1 NEW_LINE while ( m != 1 ) : NEW_LINE INDENT if ( isprm ( m ) ) : NEW_LINE INDENT mfactor . append ( 1 ) NEW_LINE fre [ 1 ] = 1 NEW_LINE m = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sqt = ( int ) ( math . sqrt ( m ) ) NEW_LINE for i in range ( 2 , sqt + 1 ) : NEW_LINE INDENT if ( m % i == 0 ) : NEW_LINE INDENT mfactor . append ( m // i ) NEW_LINE fre [ m // i ] = 1 NEW_LINE m = ( m // i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT nfactor . append ( n ) NEW_LINE while ( fre [ n ] != 1 ) : NEW_LINE INDENT if ( isprm ( n ) ) : NEW_LINE INDENT nfactor . append ( 1 ) NEW_LINE n = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sqt = ( int ) ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , sqt + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT nfactor . append ( n // i ) NEW_LINE n = ( n // i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( len ( mfactor ) ) : NEW_LINE INDENT if ( mfactor [ i ] == n ) : NEW_LINE INDENT break NEW_LINE DEDENT print ( mfactor [ i ] , end = " ▁ < - - > ▁ " ) NEW_LINE DEDENT for i in range ( len ( nfactor ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT print ( nfactor [ i ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( nfactor [ i ] , end = " ▁ < - - > ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 18 NEW_LINE n = 19 NEW_LINE shortestpath ( m , n ) NEW_LINE DEDENT
Possible number of Trees having N vertex | Python3 program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of Nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the final count of trees ; Driver Code ; Given array arr ; Function call
mod = int ( 1e9 + 7 ) NEW_LINE def NumberOfTrees ( arr , N ) : NEW_LINE INDENT maxElement = max ( arr ) ; NEW_LINE level = [ 0 ] * ( maxElement + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT level [ arr [ i ] ] += 1 ; NEW_LINE DEDENT if ( arr [ 0 ] != 0 or level [ 0 ] != 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE for i in range ( maxElement ) : NEW_LINE INDENT for j in range ( level [ i + 1 ] ) : NEW_LINE INDENT ans = ( ans * level [ i ] ) % mod ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 ; NEW_LINE arr = [ 0 , 3 , 2 , 1 , 2 , 2 , 1 ] ; NEW_LINE print ( NumberOfTrees ( arr , N ) ) ; NEW_LINE DEDENT
Possible number of Trees having N vertex | Python3 program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the final count ; Driver Code ; Given Queries ; Function call
mod = int ( 1e9 + 7 ) NEW_LINE def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( x , y // 2 ) % mod NEW_LINE p = ( p * p ) % mod NEW_LINE if ( y & 1 ) : NEW_LINE INDENT p = ( x * p ) % mod NEW_LINE DEDENT return p NEW_LINE DEDENT def NumberOfTrees ( arr , N ) : NEW_LINE INDENT maxElement = max ( arr ) NEW_LINE level = [ 0 ] * ( maxElement + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT level [ arr [ i ] ] += 1 NEW_LINE DEDENT if ( arr [ 0 ] != 0 or level [ 0 ] != 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( maxElement ) : NEW_LINE INDENT ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 7 NEW_LINE arr = [ 0 , 3 , 2 , 1 , 2 , 2 , 1 ] NEW_LINE print ( NumberOfTrees ( arr , N ) ) NEW_LINE
A Binary String Game | Function to return the result of the game ; Length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1 ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Given string S ; Function call
def gameMax ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE list = [ ] NEW_LINE one = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( one != 0 ) : NEW_LINE INDENT list . append ( one ) NEW_LINE DEDENT one = 0 NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( one != 0 ) : NEW_LINE INDENT list . append ( one ) NEW_LINE DEDENT list . sort ( reverse = True ) NEW_LINE score_1 = 0 NEW_LINE score_2 = 0 NEW_LINE for i in range ( len ( list ) ) : NEW_LINE INDENT if ( list [ i ] % 2 == 1 ) : NEW_LINE INDENT score_1 += list [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT score_2 += list [ i ] NEW_LINE DEDENT DEDENT if ( score_1 == score_2 ) : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT if ( score_1 > score_2 ) : NEW_LINE INDENT return " Player ▁ 1" NEW_LINE DEDENT else : NEW_LINE INDENT return " Player ▁ 2" NEW_LINE DEDENT DEDENT S = "11111101" NEW_LINE print ( gameMax ( S ) ) NEW_LINE
Count of carry operations on adding two Binary numbers | Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to final count ; Return the final count ; Given two numbers ; Function call
def carryCount ( num1 , num2 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num2 != 0 ) : NEW_LINE INDENT carry = num1 & num2 NEW_LINE num1 = num1 ^ num2 NEW_LINE num2 = carry << 1 NEW_LINE count += bin ( num2 ) . count ( '1' ) NEW_LINE DEDENT return count NEW_LINE DEDENT A = 15 NEW_LINE B = 10 NEW_LINE print ( carryCount ( A , B ) ) NEW_LINE
Count of all possible ways to reach a target by a Knight | Python3 program to implement above approach ; Function to return X ^ Y % Mod ; Base case ; Function to return the inverse of factorial of N ; Base case ; Function to return factorial of n % Mod ; Base case ; Function to return the value of n ! / ( ( n - k ) ! * k ! ) ; Function to return the count of ways to reach ( n , m ) from ( 0 , 0 ) ; If ( N + M ) % 3 != 0 ; No possible way exists ; Calculate X and Y from the equations X + 2 Y = N and 2 X + Y == M ; Driver code
Mod = int ( 1e9 + 7 ) NEW_LINE def power ( X , Y , Mod ) : NEW_LINE INDENT if Y == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( X , Y // 2 , Mod ) % Mod NEW_LINE p = ( p * p ) % Mod NEW_LINE if Y & 1 : NEW_LINE INDENT p = ( X * p ) % Mod NEW_LINE DEDENT return p NEW_LINE DEDENT def Inversefactorial ( N ) : NEW_LINE INDENT if N <= 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT fact = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = ( fact * i ) % Mod NEW_LINE DEDENT return power ( fact , Mod - 2 , Mod ) NEW_LINE DEDENT def factorial ( N ) : NEW_LINE INDENT if N <= 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT fact = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = ( fact * i ) % Mod NEW_LINE DEDENT return fact NEW_LINE DEDENT def nck ( N , K ) : NEW_LINE INDENT factN = factorial ( N ) NEW_LINE inv = Inversefactorial ( K ) NEW_LINE invFact = Inversefactorial ( N - K ) NEW_LINE return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod NEW_LINE DEDENT def TotalWays ( N , M ) : NEW_LINE INDENT if ( N + M ) % 3 != 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT X = N - ( N + M ) // 3 NEW_LINE Y = M - ( N + M ) // 3 NEW_LINE if X < 0 or Y < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return nck ( X + Y , Y ) NEW_LINE DEDENT N , M = 3 , 3 NEW_LINE print ( TotalWays ( N , M ) ) NEW_LINE
Lead a life problem | Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Driver Code ; Given days ; Given earnings ; Given cost ; Given energy e ; Function call
def calculateProfit ( n , earnings , cost , e ) : NEW_LINE INDENT profit = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT earning_per_day = 0 ; NEW_LINE daily_spent_food = 0 ; NEW_LINE if ( i == ( n - 1 ) ) : NEW_LINE INDENT earning_per_day = earnings [ i ] * e ; NEW_LINE profit = profit + earning_per_day ; NEW_LINE break ; NEW_LINE DEDENT if ( cost [ i ] < earnings [ i ] ) : NEW_LINE INDENT earning_per_day = earnings [ i ] * e ; NEW_LINE daily_spent_food = cost [ i ] * e ; NEW_LINE profit = ( profit + earning_per_day - daily_spent_food ) ; NEW_LINE DEDENT DEDENT print ( profit ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE earnings = [ 1 , 8 , 6 , 7 ] ; NEW_LINE cost = [ 1 , 3 , 4 , 1 ] ; NEW_LINE e = 5 ; NEW_LINE calculateProfit ( n , earnings , cost , e ) ; NEW_LINE DEDENT
Check if N numbers with Even Sum can be selected from a given Array | Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
def checkEvenSum ( arr , N , size ) : NEW_LINE INDENT even_freq , odd_freq = 0 , 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd_freq += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even_freq += 1 NEW_LINE DEDENT DEDENT if ( even_freq >= N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT if ( odd_freq & 1 ) : NEW_LINE INDENT taken = odd_freq - 1 NEW_LINE req = N - taken NEW_LINE if ( even_freq < req ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT taken = odd_freq NEW_LINE req = N - taken NEW_LINE if ( even_freq < req ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 ] NEW_LINE size = len ( arr ) NEW_LINE N = 5 NEW_LINE if ( checkEvenSum ( arr , N , size ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum count of digits required to obtain given Sum | Function to print the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code
def mindigits ( n ) : NEW_LINE INDENT if ( n % 9 == 0 ) : NEW_LINE INDENT print ( n // 9 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( n // 9 ) + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n1 = 24 ; NEW_LINE n2 = 18 ; NEW_LINE mindigits ( n1 ) ; NEW_LINE mindigits ( n2 ) ; NEW_LINE DEDENT
Count of all possible numbers not exceeding M having suffix N | Function to count the no . of digits of N ; Function to count all possible numbers having Suffix as N ; Difference of the A . P ; Count of the number of terms ; Driver code
def digitsOf ( num ) : NEW_LINE INDENT return len ( str ( num ) ) ; NEW_LINE DEDENT def count ( a , tn ) : NEW_LINE INDENT diff = int ( pow ( 10 , digitsOf ( a ) ) ) ; NEW_LINE return ( ( tn - a ) / diff ) + 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 ; m = 4500 ; NEW_LINE print ( int ( count ( n , m ) ) ) ; NEW_LINE DEDENT
Minimum increment / decrement operations required on Array to satisfy given conditions | Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Driver Code ; Given array arr [ ] ; Function call
def minOperations ( a , N ) : NEW_LINE INDENT num_of_ops1 = num_of_ops2 = sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( i % 2 == 0 and sum >= 0 ) : NEW_LINE INDENT num_of_ops1 += ( 1 + abs ( sum ) ) NEW_LINE sum = - 1 NEW_LINE DEDENT elif ( i % 2 == 1 and sum <= 0 ) : NEW_LINE INDENT num_of_ops1 += ( 1 + abs ( sum ) ) NEW_LINE sum = 1 NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( i % 2 == 1 and sum >= 0 ) : NEW_LINE INDENT num_of_ops2 += ( 1 + abs ( sum ) ) NEW_LINE sum = - 1 NEW_LINE DEDENT elif ( i % 2 == 0 and sum <= 0 ) : NEW_LINE INDENT num_of_ops2 += ( 1 + abs ( sum ) ) NEW_LINE sum = 1 NEW_LINE DEDENT DEDENT return min ( num_of_ops1 , num_of_ops2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , - 4 , 5 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minOperations ( arr , N ) ) NEW_LINE DEDENT
Check if all elements of a Circular Array can be made equal by increments of adjacent pairs | Function to check if all array elements can be made equal ; Stores the sum of even and odd array elements ; If index is odd ; Driver Code
def checkEquall ( arr , N ) : NEW_LINE INDENT sumEven , sumOdd = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT sumOdd += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sumEven += arr [ i ] NEW_LINE DEDENT DEDENT if ( sumEven == sumOdd ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 7 , 3 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE if ( checkEquall ( arr , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check if a number can be expressed as sum of two Perfect powers | Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum boolean array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum into perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Given Number n ; Function call
def isSumOfPower ( n ) : NEW_LINE INDENT isSum = [ 0 ] * ( n + 1 ) NEW_LINE perfectPowers = [ ] NEW_LINE perfectPowers . append ( 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT isSum [ i ] = False NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( isSum [ i ] == True ) : NEW_LINE INDENT perfectPowers . append ( i ) NEW_LINE continue NEW_LINE DEDENT j = i * i NEW_LINE while ( j > 0 and j < ( n + 1 ) ) : NEW_LINE INDENT isSum [ j ] = True NEW_LINE j *= i NEW_LINE DEDENT DEDENT for i in range ( len ( perfectPowers ) ) : NEW_LINE INDENT isSum [ perfectPowers [ i ] ] = False NEW_LINE DEDENT for i in range ( len ( perfectPowers ) ) : NEW_LINE INDENT for j in range ( len ( perfectPowers ) ) : NEW_LINE INDENT sum = ( perfectPowers [ i ] + perfectPowers [ j ] ) NEW_LINE if ( sum < ( n + 1 ) ) : NEW_LINE INDENT isSum [ sum ] = True NEW_LINE DEDENT DEDENT DEDENT return isSum [ n ] NEW_LINE DEDENT n = 9 NEW_LINE if ( isSumOfPower ( n ) ) : NEW_LINE INDENT print ( " true " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " false " ) NEW_LINE DEDENT