codeparrot/xlcost-text-to-code huggingface dataset:
Check if digit cube limit of an integer arrives at fixed point or a limit cycle | Define the limit ; Function to get the sum of cube of digits of a number ; Convert to string to get sum of the cubes of its digits ; Return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call
LIMIT = 1000000000 NEW_LINE def F ( N : int ) -> int : NEW_LINE INDENT string = str ( N ) NEW_LINE sum = 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT val = int ( ord ( string [ i ] ) - ord ( '0' ) ) NEW_LINE sum += val * val * val NEW_LINE DEDENT return sum NEW_LINE DEDENT def findDestination ( N : int ) -> int : NEW_LINE INDENT s = set ( ) NEW_LINE prev = N NEW_LINE next = 0 NEW_LINE s . add ( N ) NEW_LINE while ( N <= LIMIT ) : NEW_LINE INDENT next = F ( N ) NEW_LINE if next in s : NEW_LINE INDENT return next NEW_LINE DEDENT prev = next NEW_LINE s . add ( prev ) NEW_LINE N = next NEW_LINE DEDENT return next NEW_LINE DEDENT def digitCubeLimit ( N : int ) -> int : NEW_LINE INDENT if ( N < 0 ) : NEW_LINE INDENT print ( " N ▁ cannot ▁ be ▁ negative " ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = findDestination ( N ) NEW_LINE if ( ans > LIMIT ) : NEW_LINE INDENT print ( " Limit ▁ exceeded " ) NEW_LINE DEDENT elif ( ans == F ( ans ) ) : NEW_LINE INDENT print ( " { } ▁ reaches ▁ to ▁ a ▁ fixed ▁ point : ▁ { } " . format ( N , ans ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " { } ▁ reaches ▁ to ▁ a ▁ limit ▁ cycle : ▁ { } " . format ( N , ans ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE digitCubeLimit ( N ) NEW_LINE DEDENT
Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array arr [ ] ; Function call
def countTuples ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE val = 0 NEW_LINE freq = { } NEW_LINE for j in range ( N - 2 ) : NEW_LINE INDENT val = 0 NEW_LINE for l in range ( j + 1 , N ) : NEW_LINE INDENT if ( arr [ j ] == arr [ l ] ) : NEW_LINE INDENT ans += val NEW_LINE DEDENT if arr [ l ] in freq : NEW_LINE INDENT val += freq [ arr [ l ] ] NEW_LINE DEDENT DEDENT freq [ arr [ j ] ] = freq . get ( arr [ j ] , 0 ) + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 2 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countTuples ( arr , N ) ) NEW_LINE DEDENT
Minimize subarray increments / decrements required to reduce all array elements to 0 | Python3 program of the above approach ; Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum position ; Return minOp ; Driver code
import sys NEW_LINE def minOperation ( arr ) : NEW_LINE INDENT minOp = sys . maxsize NEW_LINE minNeg = 0 NEW_LINE maxPos = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT if ( arr [ i ] < minNeg ) : NEW_LINE INDENT minNeg = arr [ i ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr [ i ] > maxPos : NEW_LINE INDENT maxPos = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return abs ( minNeg ) + maxPos NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 4 , 1 ] NEW_LINE print ( minOperation ( arr ) ) NEW_LINE DEDENT
Pair of integers having least GCD among all given pairs having GCD exceeding K | Function to calculate the GCD of two numbers ; Function to print the pair having a gcd value just greater than the given integer ; Initialize variables ; Iterate until low less than equal to high ; Calculate mid ; Reducing the search space ; Print the answer ; Driver Code ; Given array and K ; Function call
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 GcdPair ( arr , k ) : NEW_LINE INDENT lo = 0 NEW_LINE hi = len ( arr ) - 1 NEW_LINE ans = [ - 1 , 0 ] NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = lo + ( hi - lo ) // 2 NEW_LINE if ( GCD ( arr [ mid ] [ 0 ] , arr [ mid ] [ 1 ] ) > k ) : NEW_LINE INDENT ans = arr [ mid ] NEW_LINE hi = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT if ( len ( ans ) == - 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ( " , ans [ 0 ] , " , " , ans [ 1 ] , " ) " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 3 , 6 ] , [ 15 , 30 ] , [ 25 , 75 ] , [ 30 , 120 ] ] NEW_LINE K = 16 NEW_LINE GcdPair ( arr , K ) NEW_LINE DEDENT
Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | Python3 implementation of the above approach ; Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of arr to array B ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Driver Code ; Given array ; Function Call
import sys NEW_LINE def isPossible ( arr , N ) : NEW_LINE INDENT mn = sys . maxsize ; NEW_LINE B = [ 0 ] * N ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT mn = min ( mn , arr [ i ] ) ; NEW_LINE B [ i ] = arr [ i ] ; NEW_LINE DEDENT arr . sort ( ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] != B [ i ] ) : NEW_LINE INDENT if ( B [ i ] % mn != 0 ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE arr = [ 4 , 3 , 6 , 6 , 2 , 9 ] ; NEW_LINE isPossible ( arr , N ) ; NEW_LINE DEDENT
Check if every vertex triplet in graph contains two vertices connected to third vertex | Function to add edge into the graph ; Finding the maximum and minimum values in each component ; Function for checking whether the given graph is valid or not ; Checking for intersecting intervals ; If intersection is found ; Graph is not valid ; Function for the DFS Traversal ; Traversing for every vertex ; Storing maximum and minimum values of each component ; Driver code
def addEdge ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE return adj NEW_LINE DEDENT def DFSUtil ( u , adj , visited , componentMin , componentMax ) : NEW_LINE INDENT visited [ u ] = True NEW_LINE componentMax = max ( componentMax , u ) NEW_LINE componentMin = min ( componentMin , u ) NEW_LINE for i in range ( len ( adj [ u ] ) ) : NEW_LINE INDENT if ( visited [ adj [ u ] [ i ] ] == False ) : NEW_LINE INDENT visited , componentMax , componentMin = DFSUtil ( adj [ u ] [ i ] , adj , visited , componentMin , componentMax ) NEW_LINE DEDENT DEDENT return visited , componentMax , componentMin NEW_LINE DEDENT def isValid ( v ) : NEW_LINE INDENT MAX = - 1 NEW_LINE ans = False NEW_LINE for i in v : NEW_LINE INDENT if len ( i ) != 2 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( i [ 0 ] <= MAX ) : NEW_LINE INDENT ans = True NEW_LINE DEDENT MAX = max ( MAX , i [ 1 ] ) NEW_LINE DEDENT return ( True if ans == False else False ) NEW_LINE DEDENT def DFS ( adj , V ) : NEW_LINE INDENT v = [ [ ] ] NEW_LINE visited = [ False for i in range ( V + 1 ) ] NEW_LINE for u in range ( 1 , V + 1 ) : NEW_LINE INDENT if ( visited [ u ] == False ) : NEW_LINE INDENT componentMax = u NEW_LINE componentMin = u NEW_LINE visited , componentMax , componentMin = DFSUtil ( u , adj , visited , componentMin , componentMax ) NEW_LINE v . append ( [ componentMin , componentMax ] ) NEW_LINE DEDENT DEDENT check = isValid ( v ) NEW_LINE if ( check ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE K = 3 NEW_LINE adj = [ [ ] for i in range ( N + 1 ) ] NEW_LINE adj = addEdge ( adj , 1 , 2 ) NEW_LINE adj = addEdge ( adj , 2 , 3 ) NEW_LINE adj = addEdge ( adj , 3 , 4 ) NEW_LINE DFS ( adj , N ) NEW_LINE DEDENT
Check if all array elements are present in a given stack or not | Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver Code
def checkArrInStack ( s , arr ) : NEW_LINE INDENT freq = { } NEW_LINE for ele in arr : NEW_LINE INDENT freq [ ele ] = freq . get ( ele , 0 ) + 1 NEW_LINE DEDENT while s : NEW_LINE INDENT poppedEle = s . pop ( ) NEW_LINE if poppedEle in freq : NEW_LINE freq [ poppedEle ] -= 1 NEW_LINE if not freq [ poppedEle ] : NEW_LINE INDENT del freq [ poppedEle ] NEW_LINE DEDENT DEDENT if not freq : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ 10 , 20 , 30 , 40 , 50 ] NEW_LINE arr = [ 20 , 30 ] NEW_LINE if checkArrInStack ( s , arr ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Length of longest substring to be deleted to make a string equal to another string | Function to prthe length of longest substring to be deleted ; Stores the length of string ; Store the position of previous matched character of str1 ; Store the position of first occurrence of str2 in str1 ; Find the position of the first occurrence of str2 ; Store the index of str1 ; If both characters not matched ; Store the length of the longest deleted substring ; Store the position of last occurrence of str2 in str1 ; If both characters not matched ; Update res ; Update res . ; Given string ; Function call
def longDelSub ( str1 , str2 ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE M = len ( str2 ) NEW_LINE prev_pos = 0 NEW_LINE pos = [ 0 ] * M NEW_LINE for i in range ( M ) : NEW_LINE INDENT index = prev_pos NEW_LINE while ( index < N and str1 [ index ] != str2 [ i ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT pos [ i ] = index NEW_LINE prev_pos = index + 1 NEW_LINE DEDENT res = N - prev_pos NEW_LINE prev_pos = N - 1 NEW_LINE for i in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT index = prev_pos NEW_LINE while ( index >= 0 and str1 [ index ] != str2 [ i ] ) : NEW_LINE INDENT index -= 1 NEW_LINE DEDENT if ( i != 0 ) : NEW_LINE INDENT res = max ( res , index - pos [ i - 1 ] - 1 ) NEW_LINE DEDENT prev_pos = index - 1 NEW_LINE DEDENT res = max ( res , prev_pos + 1 ) NEW_LINE return res NEW_LINE DEDENT str1 = " GeeksforGeeks " NEW_LINE str2 = " forks " NEW_LINE print ( longDelSub ( str1 , str2 ) ) NEW_LINE
Maximized partitions of a string such that each character of the string appears in one substring | Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s ; Find the last position of each character in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; If the current pos of character equals the min pos then the end of partition ; Add the respective character to the string ; Store the partition 's  len and reset variables ; Update the minp and str ; Input string ; Function call
def print_substring ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE str = " " NEW_LINE ans = [ ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_pos = [ - 1 ] * 26 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] = i NEW_LINE DEDENT DEDENT minp = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lp = last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] NEW_LINE minp = max ( minp , lp ) NEW_LINE if ( i == minp ) : NEW_LINE INDENT str += s [ i ] NEW_LINE print ( str , end = " ▁ " ) NEW_LINE minp = - 1 NEW_LINE str = " " NEW_LINE DEDENT else : NEW_LINE INDENT str += s [ i ] NEW_LINE DEDENT DEDENT DEDENT S = " ababcbacadefegdehijhklij " NEW_LINE print_substring ( S ) NEW_LINE
Lengths of maximized partitions of a string such that each character of the string appears in one substring | Function to find the length of all partitions oof a string such that each characters occurs in a single substring ; Stores last index of string s ; Find the last position of each letter in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Given string str ; Function call
def partitionString ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = [ ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_pos = [ - 1 ] * 26 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] = i NEW_LINE DEDENT DEDENT minp = - 1 NEW_LINE plen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lp = last_pos [ ord ( s [ i ] ) - ord ( ' a ' ) ] NEW_LINE minp = max ( minp , lp ) NEW_LINE plen += 1 NEW_LINE if ( i == minp ) : NEW_LINE INDENT ans . append ( plen ) NEW_LINE minp = - 1 NEW_LINE plen = 0 NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT str = " acbbcc " NEW_LINE partitionString ( str ) NEW_LINE
Find the maximum occurring character after performing the given operations | Function to find the maximum occurring character ; Initialize count of zero and one ; Iterate over the given string ; Count the zeros ; Count the ones ; Iterate over the given string ; Check if character is * then continue ; Check if first character after * is X ; Add all * to the frequency of X ; Set prev to the i - th character ; Check if first character after * is Y ; Set prev to the i - th character ; Check if prev character is 1 and current character is 0 ; Half of the * will be converted to 0 ; Half of the * will be converted to 1 ; Check if prev and current are 1 ; All * will get converted to 1 ; No * can be replaced by either 0 or 1 ; Prev becomes the ith character ; Check if prev and current are 0 ; All * will get converted to 0 ; If frequency of 0 is more ; If frequency of 1 is more ; Given string ; Function call
def solve ( S ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE prev = - 1 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT elif ( S [ i ] == '1' ) : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT DEDENT for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == ' * ' ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( S [ i ] == '0' and prev == - 1 ) : NEW_LINE INDENT count_0 = count_0 + i NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ i ] == '1' and prev == - 1 ) : NEW_LINE INDENT prev = i NEW_LINE DEDENT elif ( S [ prev ] == '1' and S [ i ] == '0' ) : NEW_LINE INDENT count_0 = count_0 + ( i - prev - 1 ) / 2 NEW_LINE count_1 = count_1 + ( i - prev - 1 ) // 2 NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ prev ] == '1' and S [ i ] == '1' ) : NEW_LINE INDENT count_1 = count_1 + ( i - prev - 1 ) NEW_LINE prev = i NEW_LINE DEDENT elif ( S [ prev ] == '0' and S [ i ] == '1' ) : NEW_LINE INDENT prev = i NEW_LINE DEDENT elif ( S [ prev ] == '0' and S [ i ] == '0' ) : NEW_LINE INDENT count_0 = count_0 + ( i - prev - 1 ) NEW_LINE prev = i NEW_LINE DEDENT DEDENT if ( count_0 > count_1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( count_1 > count_0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT str = " * *0 * * 1 * * *0" NEW_LINE solve ( str ) NEW_LINE
Arithmetic Progression containing X and Y with least possible first term | Python3 program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function call
import sys NEW_LINE def minFirstTerm ( X , diff , N ) : NEW_LINE INDENT first_term_1 = sys . maxsize NEW_LINE low = 0 NEW_LINE high = N NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( X - mid * diff > 0 ) : NEW_LINE INDENT first_term_1 = X - mid * diff NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return first_term_1 NEW_LINE DEDENT def printAP ( N , X , Y ) : NEW_LINE INDENT if ( X > Y ) : NEW_LINE INDENT X = X + Y NEW_LINE Y = X - Y NEW_LINE X = X - Y NEW_LINE DEDENT maxDiff = Y - X NEW_LINE first_term = sys . maxsize NEW_LINE diff = 0 NEW_LINE for i in range ( 1 , maxDiff + 1 ) : NEW_LINE INDENT if i * i > maxDiff : NEW_LINE INDENT break NEW_LINE DEDENT if ( maxDiff % i == 0 ) : NEW_LINE INDENT diff1 = i NEW_LINE diff2 = maxDiff // diff1 NEW_LINE terms1 = diff2 + 1 NEW_LINE terms2 = diff1 + 1 NEW_LINE first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) NEW_LINE first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) NEW_LINE if ( first_term1 < first_term ) : NEW_LINE INDENT first_term = first_term1 NEW_LINE diff = diff1 NEW_LINE DEDENT if ( first_term2 < first_term ) : NEW_LINE INDENT first_term = first_term2 NEW_LINE diff = diff2 NEW_LINE DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( first_term , end = " ▁ " ) NEW_LINE first_term += diff NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE X = 10 NEW_LINE Y = 15 NEW_LINE printAP ( N , X , Y ) NEW_LINE DEDENT
Check if a number can be represented as sum of two consecutive perfect cubes | Function to check if a number can be expressed as the sum of cubes of two consecutive numbers ; Driver Code
def isCubeSum ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( pow ( n , 1 / 3 ) ) + 1 ) : NEW_LINE INDENT if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 35 ; NEW_LINE if ( isCubeSum ( 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 represented as sum of two consecutive perfect cubes | Function to check that a number is the sum of cubes of 2 consecutive numbers or not ; Condition to check if a number is the sum of cubes of 2 consecutive numbers or not ; Driver Code ; Function call
def isSumCube ( N ) : NEW_LINE INDENT a = int ( pow ( N , 1 / 3 ) ) NEW_LINE b = a - 1 NEW_LINE ans = ( ( a * a * a + b * b * b ) == N ) NEW_LINE return ans NEW_LINE DEDENT i = 35 NEW_LINE if ( isSumCube ( i ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Maximize sum of remaining elements after every removal of the array half with greater sum | Function to find the maximum sum ; Base case len of array is 1 ; Stores the final result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the final ans ; Function to print maximum sum ; Dicitionary to store prefix sums ; Traversing the array ; Add prefix sum of the array ; Drivers Code ; Function Call
def maxweight ( s , e , pre ) : NEW_LINE INDENT if s == e : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( s , e ) : NEW_LINE INDENT left = pre [ i ] - pre [ s - 1 ] NEW_LINE right = pre [ e ] - pre [ i ] NEW_LINE if left < right : NEW_LINE INDENT ans = max ( ans , left + maxweight ( s , i , pre ) ) NEW_LINE DEDENT if left == right : NEW_LINE INDENT ans = max ( ans , left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) NEW_LINE DEDENT if left > right : NEW_LINE INDENT ans = max ( ans , right + maxweight ( i + 1 , e , pre ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def maxSum ( arr ) : NEW_LINE INDENT pre = { - 1 : 0 , 0 : arr [ 0 ] } NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + arr [ i ] NEW_LINE DEDENT print ( maxweight ( 0 , len ( arr ) - 1 , pre ) ) NEW_LINE DEDENT arr = [ 6 , 2 , 3 , 4 , 5 , 5 ] NEW_LINE maxSum ( arr ) NEW_LINE
Maximum time in HH : MM : SS format that can be represented by given six digits | Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code
def largestTimeFromDigits ( A ) : NEW_LINE INDENT mp1 = { } NEW_LINE mp2 = { } NEW_LINE for x in A : NEW_LINE INDENT mp1 [ x ] = mp1 . get ( x , 0 ) + 1 NEW_LINE DEDENT mp2 = mp1 . copy ( ) NEW_LINE hr = 23 NEW_LINE m = 59 NEW_LINE s = 59 NEW_LINE while ( hr >= 0 ) : NEW_LINE INDENT h0 = hr // 10 NEW_LINE h1 = hr % 10 NEW_LINE m0 = m // 10 NEW_LINE m1 = m % 10 NEW_LINE s0 = s // 10 NEW_LINE s1 = s % 10 NEW_LINE p = 0 NEW_LINE arr = [ h0 , h1 , m0 , m1 , s0 , s1 ] NEW_LINE for it in arr : NEW_LINE INDENT if ( it in mp1 and mp1 . get ( it ) > 0 ) : NEW_LINE INDENT mp1 [ it ] = mp1 . get ( it ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT p = 1 NEW_LINE DEDENT DEDENT if ( p == 0 ) : NEW_LINE INDENT s = " " NEW_LINE s = ( str ( h0 ) + str ( h1 ) ) NEW_LINE s += ( ' : ' + str ( m0 ) + str ( m1 ) ) NEW_LINE s += ( ' : ' + str ( s0 ) + str ( s1 ) ) NEW_LINE return s NEW_LINE DEDENT mp1 = mp2 . copy ( ) NEW_LINE if ( s == 0 ) : NEW_LINE INDENT s = 59 NEW_LINE m -= 1 NEW_LINE DEDENT elif ( m < 0 ) : NEW_LINE INDENT m = 59 NEW_LINE hr -= 1 NEW_LINE DEDENT if ( s > 0 ) : NEW_LINE INDENT s -= 1 NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT v = [ 0 , 2 , 1 , 9 , 3 , 2 ] NEW_LINE print ( largestTimeFromDigits ( v ) ) NEW_LINE DEDENT