codeparrot/xlcost-text-to-code huggingface dataset:
Largest number not exceeding N that does not contain any of the digits of S | Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver code
def greatestReducedNumber ( num , s ) : NEW_LINE INDENT vis_s = [ False ] * 10 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE vis_s [ ( ord ) ( s [ i ] ) - 48 ] = True NEW_LINE n = len ( num ) NEW_LINE In = - 1 NEW_LINE for i in range ( n ) : NEW_LINE if ( vis_s [ ord ( num [ i ] ) - ord ( '0' ) ] ) : NEW_LINE INDENT In = i NEW_LINE break NEW_LINE DEDENT if ( In == - 1 ) : NEW_LINE return num NEW_LINE for dig in range ( ord ( num [ In ] ) , ord ( '0' ) - 1 , - 1 ) : NEW_LINE if ( vis_s [ dig - ord ( '0' ) ] == False ) : NEW_LINE INDENT num = num [ 0 : In ] + chr ( dig ) + num [ In + 1 : n - In - 1 ] NEW_LINE break NEW_LINE DEDENT LargestDig = '0' NEW_LINE for dig in range ( ord ( '9' ) , ord ( '0' ) - 1 , - 1 ) : NEW_LINE if ( vis_s [ dig - ord ( '0' ) ] == False ) : NEW_LINE INDENT LargestDig = dig NEW_LINE break NEW_LINE DEDENT for i in range ( In + 1 , n ) : NEW_LINE num = num [ 0 : i ] + chr ( LargestDig ) NEW_LINE Count = 0 NEW_LINE for i in range ( n ) : NEW_LINE if ( num [ i ] == '0' ) : NEW_LINE INDENT Count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT num = num [ Count : n ] NEW_LINE if ( int ( len ( num ) ) == 0 ) : NEW_LINE return "0" NEW_LINE return num NEW_LINE DEDENT N = "12345" NEW_LINE S = "23" NEW_LINE print ( greatestReducedNumber ( N , S ) ) NEW_LINE
Generate an array of minimum sum whose XOR of same | Function to generate an array whose XOR with same - indexed elements of the given array is always a prime ; Traverse the array ; If current array element is 2 ; Print its XOR with 3 ; Otherwise ; Print its XOR with 2 ; Driver Code ; Given array ; Size of the array ; Prints the required array
def minXOR ( Arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( Arr [ i ] == 2 ) : NEW_LINE INDENT print ( Arr [ i ] ^ 3 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( Arr [ i ] ^ 2 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Arr = [ 5 , 4 , 7 , 6 ] NEW_LINE N = len ( Arr ) NEW_LINE minXOR ( Arr , N ) NEW_LINE DEDENT
Find the largest element in an array generated using the given conditions | Function to generate the required array ; Stores the array ; Base case ; Iterate over the indices ; If current index is even ; Otherwise ; Function to find and return the maximum array element ; If n is 0 ; If n is 1 ; Generates the required array ; Return maximum element of Arr ; Driver Code
def findArray ( n ) : NEW_LINE INDENT Arr = [ 0 ] * ( n + 1 ) NEW_LINE Arr [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT Arr [ i ] = Arr [ i // 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT Arr [ i ] = ( Arr [ ( i - 1 ) // 2 ] + Arr [ ( i - 1 ) // 2 + 1 ] ) NEW_LINE DEDENT DEDENT return Arr NEW_LINE DEDENT def maxElement ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT Arr = findArray ( n ) NEW_LINE return max ( Arr ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE print ( maxElement ( N ) ) NEW_LINE DEDENT
Construct a graph using N vertices whose shortest distance between K pair of vertices is 2 | Function to construct the simple and connected graph such that the distance between exactly K pairs of vertices is 2 ; Stores maximum possible count of edges in a graph ; Base case ; Stores edges of a graph ; Connect all vertices of pairs ( i , j ) ; Print first ( ( N - 1 ) + Max - K ) elements of edges [ ] ; Driver code
def constGraphWithCon ( N , K ) : NEW_LINE INDENT Max = ( ( N - 1 ) * ( N - 2 ) ) // 2 NEW_LINE if ( K > Max ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT ans = [ ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N + 1 ) : NEW_LINE INDENT ans . append ( [ i , j ] ) NEW_LINE DEDENT DEDENT for i in range ( 0 , ( N - 1 ) + Max - K ) : NEW_LINE INDENT print ( ans [ i ] [ 0 ] , ans [ i ] [ 1 ] , sep = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE constGraphWithCon ( N , K ) NEW_LINE DEDENT
Minimum steps required to visit all corners of an N * M grid | Python3 program to implement the above approach ; Function to find the minimum count of steps required to visit all the corners of the grid ; Stores corner of the grid ; Stores minimum count of steps required to visit the first corner of the grid ; Checking for leftmost upper corner ; Checking for leftmost down corner ; Checking for rightmost upper corner ; Checking for rightmost down corner ; Stores minimum count of steps required to visit remaining three corners of the grid ; Driver Code
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def min_steps_required ( n , m , r , c ) : NEW_LINE INDENT i = 0 ; j = 0 ; NEW_LINE corner_steps_req = INT_MAX ; NEW_LINE i = 1 ; NEW_LINE j = 1 ; NEW_LINE corner_steps_req = min ( corner_steps_req , abs ( r - i ) + abs ( j - c ) ) ; NEW_LINE i = n ; NEW_LINE j = 1 ; NEW_LINE corner_steps_req = min ( corner_steps_req , abs ( r - i ) + abs ( j - c ) ) ; NEW_LINE i = 1 ; NEW_LINE j = m ; NEW_LINE corner_steps_req = min ( corner_steps_req , abs ( r - i ) + abs ( j - c ) ) ; NEW_LINE i = n ; NEW_LINE j = m ; NEW_LINE corner_steps_req = min ( corner_steps_req , abs ( r - i ) + abs ( j - c ) ) ; NEW_LINE minimum_steps = min ( 2 * ( n - 1 ) + m - 1 , 2 * ( m - 1 ) + n - 1 ) ; NEW_LINE return minimum_steps + corner_steps_req ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE m = 2 ; NEW_LINE r = 1 ; NEW_LINE c = 1 ; NEW_LINE print ( min_steps_required ( n , m , r , c ) ) ; NEW_LINE DEDENT
Minimum replacements required to have at most K distinct elements in the array | Function to find minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Store the frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Store frequency of each distinct element of the array ; Traverse the map ; Stores key of the map ; Insert mp [ i ] into Freq [ ] ; Sort Freq [ ] in descending order ; Stores size of Freq [ ] ; If len is less than or equal to K ; Stores minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Iterate over the range [ K , len ] ; Update cntMin ; Driver code
def min_elements ( arr , N , K ) : NEW_LINE INDENT 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 Freq = [ ] NEW_LINE for it in mp : NEW_LINE INDENT i = it NEW_LINE Freq . append ( mp [ i ] ) NEW_LINE DEDENT Freq . sort ( ) NEW_LINE Freq . reverse ( ) NEW_LINE Len = len ( Freq ) NEW_LINE if ( Len <= K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT cntMin = 0 NEW_LINE for i in range ( K , Len ) : NEW_LINE INDENT cntMin += Freq [ i ] NEW_LINE DEDENT return cntMin NEW_LINE DEDENT arr = [ 5 , 1 , 3 , 2 , 4 , 1 , 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 ; NEW_LINE print ( min_elements ( arr , N , K ) ) NEW_LINE
Check if sum of the given array can be reduced to 0 by reducing array elements by K | Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array arr [ ]
def sumzero ( arr , N , K ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT elif ( sum > 0 ) : NEW_LINE INDENT if ( sum % K == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , - 6 , 2 , 2 ] ; NEW_LINE K = 1 ; NEW_LINE N = len ( arr1 ) ; NEW_LINE sumzero ( arr1 , N , K ) ; NEW_LINE DEDENT
Counts 1 s that can be obtained in an Array by performing given operations | Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code
def cntOnesArrWithGivenOp ( arr , N ) : NEW_LINE INDENT cntOnes = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( i - 1 , N , i ) : NEW_LINE INDENT arr [ j ] = 1 if arr [ j ] == 0 else 0 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cntOnes += 1 NEW_LINE DEDENT DEDENT return cntOnes NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntOnesArrWithGivenOp ( arr , N ) ) NEW_LINE DEDENT
Counts 1 s that can be obtained in an Array by performing given operations | Function to count total number of 1 s in array by performing the given operations ; Stores count of 1 s in the array by performing the operations ; Update cntOnes ; Driver Code
def cntOnesArrWithGivenOp ( arr , N ) : NEW_LINE INDENT cntOnes = 0 ; NEW_LINE cntOnes = int ( N ** ( 1 / 2 ) ) ; NEW_LINE return cntOnes ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 , 0 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( cntOnesArrWithGivenOp ( arr , N ) ) ; NEW_LINE DEDENT
Count Knights that can attack a given pawn in an N * N board | Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver code ; Stores total count of knights
def cntKnightsAttackPawn ( knights , pawn , M ) : NEW_LINE INDENT cntKnights = 0 ; NEW_LINE for i in range ( M ) : NEW_LINE INDENT X = abs ( knights [ i ] [ 0 ] - pawn [ 0 ] ) ; NEW_LINE Y = abs ( knights [ i ] [ 1 ] - pawn [ 1 ] ) ; NEW_LINE if ( ( X == 1 and Y == 2 ) or ( X == 2 and Y == 1 ) ) : NEW_LINE INDENT cntKnights += 1 ; NEW_LINE DEDENT DEDENT return cntKnights ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT knights = [ [ 0 , 4 ] , [ 4 , 5 ] , [ 1 , 4 ] , [ 3 , 1 ] ] ; NEW_LINE pawn = [ 2 , 3 ] ; NEW_LINE M = len ( knights ) ; NEW_LINE print ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; NEW_LINE DEDENT
Find the winner of game of removing array elements having GCD equal to 1 | Function to find the winner of the game by removing array elements whose GCD is 1 ; mp [ i ] : Stores count of array elements whose prime factor is i ; Traverse the array ; Calculate the prime factors of arr [ i ] ; If arr [ i ] is divisible by j ; Update mp [ j ] ; Update arr [ i ] ; If arr [ i ] exceeds 1 ; Stores maximum value present in the Map ; Traverse the map ; Update maxCnt ; If n is an even number ; If maxCnt is greater than or equal to n - 1 ; Player 1 wins ; Player 2 wins ; If maxCnt equal to n ; Player 1 wins ; Player 2 wins ; Driver Code
def findWinnerGameRemoveGCD ( arr , n ) : NEW_LINE INDENT mp = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 2 , int ( arr [ i ] * ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( arr [ i ] % j == 0 ) : NEW_LINE INDENT mp [ j ] += 1 ; NEW_LINE while ( arr [ i ] % j == 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // j ; NEW_LINE DEDENT DEDENT DEDENT if ( arr [ i ] > 1 ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 ; NEW_LINE DEDENT DEDENT maxCnt = 0 ; NEW_LINE for i in mp : NEW_LINE INDENT maxCnt = max ( maxCnt , mp [ i ] ) ; NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT if ( maxCnt >= n - 1 ) : NEW_LINE INDENT print ( " Player ▁ 1" , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Player ▁ 2" , end = " " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( maxCnt == n ) : NEW_LINE INDENT print ( " Player ▁ 1" , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Player ▁ 2" , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findWinnerGameRemoveGCD ( arr , N ) ; NEW_LINE DEDENT
Maximum possible sum of K even multiples of 5 in a given range | Function to find the maximum sum of K even multiples of 5 in the range [ L , R ] ; Store the total number of even multiples of 5 in the range [ L , R ] ; Check if K > N ; If true , print - 1 and return ; Otherwise , divide R by 10 ; Store the sum using the formula ; Print the sum ; Driver Code ; Given L , R and K ; Function Call
def maxksum ( L , R , K ) : NEW_LINE INDENT N = ( R // 10 - L // 10 ) + 1 ; NEW_LINE if ( K > N ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE return ; NEW_LINE DEDENT R = R // 10 ; NEW_LINE X = R - K ; NEW_LINE sum = 10 * ( ( R * ( R + 1 ) ) // 2 - ( X * ( X + 1 ) ) // 2 ) ; NEW_LINE print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 16 ; R = 60 ; K = 4 ; NEW_LINE maxksum ( L , R , K ) ; NEW_LINE DEDENT
Minimum swaps of same | Function to count the minimum swaps of same - indexed elements from arrays arr1 and arr2 required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , pr0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , pr - 1 ; Driver code ; Function Call
def minimumSwaps ( arr1 , arr2 , n ) : NEW_LINE INDENT sumArr1 = 0 ; sumArr2 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sumArr1 += arr1 [ i ] ; NEW_LINE sumArr2 += arr2 [ i ] ; NEW_LINE DEDENT if ( sumArr1 % 2 == 0 and sumArr2 % 2 == 0 ) : NEW_LINE INDENT print ( 0 ) ; NEW_LINE return ; NEW_LINE DEDENT if ( sumArr1 % 2 != 0 and sumArr2 % 2 != 0 ) : NEW_LINE INDENT flag = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT print ( flag ) ; NEW_LINE return ; NEW_LINE DEDENT print ( - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 11 , 14 , 20 , 2 ] ; NEW_LINE arr2 = [ 5 , 9 , 6 , 3 ] ; NEW_LINE N = len ( arr1 ) ; NEW_LINE minimumSwaps ( arr1 , arr2 , N ) ; NEW_LINE DEDENT
Count of seats booked on each of the given N flights | Function to find the total of the seats booked in each of the flights ; Stores the resultant sequence ; Traverse the array ; Store the first booked flight ; Store the last booked flight ; Store the total number of seats booked in flights [ l , r ] ; Add K to the flight L ; Subtract K from flight number R + 1 ; Find the prefix sum of the array ; Print total number of seats booked in each flight ; Driver Code ; Given list of bookings ; Function Call
def corpFlightBookings ( Bookings , N ) : NEW_LINE INDENT res = [ 0 ] * N NEW_LINE for i in range ( len ( Bookings ) ) : NEW_LINE INDENT l = Bookings [ i ] [ 0 ] NEW_LINE r = Bookings [ i ] [ 1 ] NEW_LINE K = Bookings [ i ] [ 2 ] NEW_LINE res [ l - 1 ] = res [ l - 1 ] + K NEW_LINE if ( r <= len ( res ) - 1 ) : NEW_LINE INDENT res [ r ] = ( - K ) + res [ r ] NEW_LINE DEDENT DEDENT for i in range ( 1 , len ( res ) ) : NEW_LINE INDENT res [ i ] = res [ i ] + res [ i - 1 ] NEW_LINE DEDENT for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bookings = [ [ 1 , 3 , 100 ] , [ 2 , 6 , 100 ] , [ 3 , 4 , 100 ] ] NEW_LINE N = 6 NEW_LINE corpFlightBookings ( bookings , N ) NEW_LINE DEDENT
Bitwise XOR of all odd numbers from a given range | Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Prthe answer ; If number is odd ; Prthe answer ; Driver Code ; Function Call
def findXOR ( n ) : NEW_LINE INDENT if ( n % 4 == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT elif ( n % 4 == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( n % 4 == 2 ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT elif ( n % 4 == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def findOddXOR ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n // 2 ) ) ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) // 2 ) ) ) ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 ; NEW_LINE findOddXOR ( N ) ; NEW_LINE DEDENT
Smallest number greater than or equal to N which is divisible by its non | Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver Code ; Function Call
def findSmallestNumber ( n ) : NEW_LINE INDENT for i in range ( n , n + 2521 ) : NEW_LINE INDENT possible = 1 NEW_LINE temp = i NEW_LINE while ( temp ) : NEW_LINE INDENT if ( temp % 10 != 0 ) : NEW_LINE INDENT digit = temp % 10 NEW_LINE if ( i % digit != 0 ) : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT DEDENT temp //= 10 NEW_LINE DEDENT if ( possible == 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 31 NEW_LINE findSmallestNumber ( N ) NEW_LINE DEDENT
Print path from a node to root of given Complete Binary Tree | Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver Code
def path_to_root ( node ) : NEW_LINE INDENT while ( node >= 1 ) : NEW_LINE INDENT print ( node , end = " ▁ " ) NEW_LINE node //= 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE path_to_root ( N ) NEW_LINE DEDENT
Smallest number whose sum of digits is N and every digit occurring at most K times | Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call
def findSmallestNumberPossible ( N , K ) : NEW_LINE INDENT if ( N > 45 * K ) : NEW_LINE INDENT print ( " - 1" , endl = " " ) ; NEW_LINE return ; NEW_LINE DEDENT res = " " ; NEW_LINE count = 0 ; NEW_LINE i = 9 ; NEW_LINE while i >= 1 : NEW_LINE INDENT if ( count == K ) : NEW_LINE INDENT i -= 1 ; NEW_LINE count = 0 ; NEW_LINE DEDENT if ( N > i ) : NEW_LINE INDENT N -= i ; NEW_LINE res += chr ( 48 + i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res += chr ( N + 48 ) ; NEW_LINE N = 0 ; NEW_LINE break ; NEW_LINE DEDENT count += 1 ; NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE print ( res , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 25 ; K = 3 ; NEW_LINE findSmallestNumberPossible ( N , K ) ; NEW_LINE DEDENT
Minimum sum of values subtracted from array elements to make all array elements equal | Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
def minValue ( arr , n ) : NEW_LINE INDENT minimum = min ( arr ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( arr [ i ] - minimum ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minValue ( arr , N ) ) NEW_LINE DEDENT
Smallest positive number made up of non | Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call
def result ( n ) : NEW_LINE INDENT if ( n > 45 ) : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE return NEW_LINE DEDENT res = " " NEW_LINE digit = 9 NEW_LINE while ( n > digit ) : NEW_LINE INDENT res = str ( digit ) + res NEW_LINE n -= digit NEW_LINE digit -= 1 NEW_LINE DEDENT if ( n > 0 ) : NEW_LINE INDENT res = str ( n ) + res NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 19 NEW_LINE result ( N ) NEW_LINE DEDENT
Count pair of integers having even sum | Python3 program for the above approach ; Function to count even pairs ; Stores count of pairs having even sum ; Stores count of even numbers up to N ; Stores count of odd numbers up to N ; Stores count of even numbers up to M ; Stores count of odd numbers up to M ; Return the count ; Driver Code
import math NEW_LINE def countEvenPairs ( N , M ) : NEW_LINE INDENT count = 0 ; NEW_LINE nEven = int ( math . floor ( N / 2 ) ) ; NEW_LINE nOdd = int ( math . ceil ( N / 2 ) ) ; NEW_LINE mEven = int ( math . floor ( M / 2 ) ) ; NEW_LINE mOdd = int ( math . ceil ( M / 2 ) ) ; NEW_LINE count = nEven * mEven + nOdd * mOdd ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE M = 6 ; NEW_LINE print ( countEvenPairs ( N , M ) ) ; NEW_LINE DEDENT
Generate an N | Function to generate a string of length N having longest palindromic substring of length K ; Fill first K characters with 'a ; Stores a non - palindromic sequence to be repeated for N - k slots ; PrN - k remaining characters ; Driver Code ; Given N and K
def string_palindrome ( N , K ) : NEW_LINE ' NEW_LINE INDENT for i in range ( K ) : NEW_LINE INDENT print ( " a " , end = " " ) NEW_LINE DEDENT s = " bcd " NEW_LINE for i in range ( N - K ) : NEW_LINE INDENT print ( s [ i % 3 ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , K = 5 , 3 NEW_LINE string_palindrome ( N , K ) NEW_LINE DEDENT