codeparrot/xlcost-text-to-code huggingface dataset:
Find K positive integers not exceeding N and having sum S | Function to represent S as the sum of K positive integers less than or equal to N ; If S can cannot be represented as sum of K integers ; If sum of first i natural numbers exceeds S ; Insert i into nums [ ] ; Insert first K - 1 positive numbers into answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Printing the K numbers ; Driver Code
def solve ( S , K , N ) : NEW_LINE INDENT if ( K > N ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT max_sum , min_sum = 0 , 0 NEW_LINE for i in range ( K + 1 ) : NEW_LINE INDENT min_sum += i NEW_LINE max_sum += N - i + 1 NEW_LINE DEDENT if ( S < min_sum or S > max_sum ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT s1 = 0 NEW_LINE nums = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( s1 > S ) : NEW_LINE INDENT break NEW_LINE DEDENT s1 += i NEW_LINE nums . append ( i ) NEW_LINE DEDENT answer = [ ] NEW_LINE s2 = 0 NEW_LINE for i in range ( K - 1 ) : NEW_LINE INDENT answer . append ( nums [ i ] ) NEW_LINE s2 += nums [ i ] NEW_LINE DEDENT answer . append ( S - s2 ) NEW_LINE Max = N NEW_LINE for i in range ( len ( answer ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( answer [ i ] > Max ) : NEW_LINE INDENT extra = answer [ i ] - Max NEW_LINE if ( i - 1 >= 0 ) : NEW_LINE INDENT answer [ i - 1 ] += extra NEW_LINE DEDENT answer [ i ] = Max NEW_LINE Max -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for x in answer : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , K , N = 15 , 4 , 8 NEW_LINE solve ( S , K , N ) NEW_LINE DEDENT
Generate an N | Python3 program for the above approach ; Function to minimize the maximum element present in an N - length array having sum of elements divisible by K ; Return the ceil value of ( K / N ) ; Driver Code
import math NEW_LINE def minimumValue ( N , K ) : NEW_LINE INDENT return math . ceil ( K / N ) NEW_LINE DEDENT N = 4 NEW_LINE K = 50 NEW_LINE print ( minimumValue ( N , K ) ) NEW_LINE
Minimum removal of elements from end of an array required to obtain sum K | Function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver Code ; Given Array ; Given target sum
def minSizeArr ( A , N , K ) : NEW_LINE INDENT leftTaken = N NEW_LINE rightTaken = N NEW_LINE leftSum = 0 NEW_LINE rightSum = 0 NEW_LINE for left in range ( - 1 , N ) : NEW_LINE INDENT if ( left != - 1 ) : NEW_LINE INDENT leftSum += A [ left ] NEW_LINE DEDENT rightSum = 0 NEW_LINE for right in range ( N - 1 , left , - 1 ) : NEW_LINE INDENT rightSum += A [ right ] NEW_LINE if ( leftSum + rightSum == K ) : NEW_LINE INDENT if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) : NEW_LINE INDENT leftTaken = left + 1 NEW_LINE rightTaken = N - right NEW_LINE DEDENT break NEW_LINE DEDENT if ( leftSum + rightSum > K ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( leftTaken + rightTaken <= N ) : NEW_LINE INDENT for i in range ( leftTaken ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( rightTaken ) : NEW_LINE INDENT print ( A [ N - i - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ] NEW_LINE K = 10 NEW_LINE minSizeArr ( A , N , K ) NEW_LINE DEDENT
Minimum removal of elements from end of an array required to obtain sum K | Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver Code ; Given Array ; Given target sum
def minSizeArr ( A , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT if ( K > sum ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE return NEW_LINE DEDENT if ( K == sum ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT tar = sum - K NEW_LINE um = { } NEW_LINE um [ 0 ] = - 1 NEW_LINE left = 0 NEW_LINE right = 0 NEW_LINE cur = 0 NEW_LINE maxi = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cur += A [ i ] NEW_LINE if ( ( cur - tar ) in um and ( i - um [ cur - tar ] ) > maxi ) : NEW_LINE INDENT maxi = i - um [ cur - tar ] NEW_LINE right = i NEW_LINE left = um [ cur - tar ] NEW_LINE DEDENT if ( cur not in um ) : NEW_LINE INDENT um [ cur ] = i NEW_LINE DEDENT DEDENT if ( maxi == - 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( left + 1 ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( right ) : NEW_LINE INDENT print ( A [ N - i - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ] NEW_LINE K = 10 NEW_LINE minSizeArr ( A , N , K ) NEW_LINE DEDENT
Minimum product modulo N possible for any pair from a given range | Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Print the minimum value of remainder ; If R - L >= N ; Driver Code
def minModulo ( L , R , N ) : NEW_LINE INDENT if ( R - L < N ) : NEW_LINE INDENT ans = 10 ** 9 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT for j in range ( L , R + 1 ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT ans = min ( ans , ( i * j ) % N ) NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R , N = 6 , 10 , 2019 NEW_LINE minModulo ( L , R , N ) NEW_LINE DEDENT
Count numbers having GCD with N equal to the number itself | Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
def countNumbers ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( N % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( N // i != i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE print ( countNumbers ( N ) ) NEW_LINE DEDENT
Maximum length of all possible K equal length ropes generated by cutting N ropes | Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array arr [ ] ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
def maximumSize ( a , k ) : NEW_LINE INDENT low = 1 NEW_LINE high = max ( a ) NEW_LINE ans = - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE count = 0 NEW_LINE for c in range ( len ( a ) ) : NEW_LINE INDENT count += a // mid NEW_LINE DEDENT if ( count >= k ) : NEW_LINE INDENT ans = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 9 ] NEW_LINE K = 6 NEW_LINE print ( maximumSize ( arr , K ) ) NEW_LINE DEDENT
Quadratic equation whose roots are K times the roots of given equation | Function to find the quadratic equation whose roots are K times the roots of the given equation ; Prquadratic equation ; Driver Code
def findEquation ( A , B , C , K ) : NEW_LINE INDENT print ( A , K * B , K * K * C ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B , C , K = 1 , 2 , 1 , 2 NEW_LINE findEquation ( A , B , C , K ) NEW_LINE DEDENT
Maximum the value of a given expression for any pair of coordinates on a 2D plane | Function to find the maximum value of the given expression possible for any pair of co - ordinates ; Stores the differences between pairs ; Stores the maximum value ; Traverse the array arr [ ] [ ] ; While pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > K ; Removes the top element ; If pq is not empty ; Update the value res ; Push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; Prthe result ; Driver Code
def findMaxValueOfEquation ( arr , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE res = - 10 ** 8 NEW_LINE for point in arr : NEW_LINE INDENT while ( len ( pq ) > 0 and point [ 0 ] - pq [ - 1 ] [ 1 ] > K ) : NEW_LINE INDENT del pq [ - 1 ] NEW_LINE DEDENT if ( len ( pq ) > 0 ) : NEW_LINE INDENT res = max ( res , pq [ - 1 ] [ 0 ] + point [ 0 ] + point [ 1 ] ) NEW_LINE DEDENT pq . append ( [ point [ 1 ] - point [ 0 ] , point [ 0 ] ] ) NEW_LINE pq = sorted ( pq ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 3 ] , [ 2 , 0 ] , [ 5 , 10 ] , [ 6 , - 10 ] ] NEW_LINE K = 1 NEW_LINE findMaxValueOfEquation ( arr , K ) NEW_LINE DEDENT
Minimum increments required to make absolute difference of all pairwise adjacent array elements even | Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver Code
def minOperations ( arr , n ) : NEW_LINE INDENT oddcount , evencount = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT oddcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT evencount += 1 NEW_LINE DEDENT DEDENT return min ( oddcount , evencount ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 1 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minOperations ( arr , N ) ) NEW_LINE DEDENT
Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver code
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 countDistinctNumbers ( A , B , C ) : NEW_LINE INDENT g = gcd ( A , B ) NEW_LINE count = C // g NEW_LINE print ( count ) NEW_LINE DEDENT A = 2 NEW_LINE B = 3 NEW_LINE C = 5 NEW_LINE countDistinctNumbers ( A , B , C ) NEW_LINE
Find the last element after repeatedly removing every second element from either end alternately | Function to find the last element remaining in the array after performing the given operations ; Checks if traversal is from left to right or vice versa ; Store the elements currently present in the array ; Store the distance between 2 consecutive array elements ; Store the first element of the remaining array ; Iterate while elements are greater than 1 ; If left to right turn ; Update head ; Otherwise , check if the remaining elements are odd ; If true , update head ; Eleminate half of the array elements ; Double the steps after each turn ; Alter the turn ; Print the remaining element ; Driver Code
def printLastElement ( arr , N ) : NEW_LINE INDENT leftTurn = True NEW_LINE remainElements = N NEW_LINE step = 1 NEW_LINE head = 1 NEW_LINE while ( remainElements > 1 ) : NEW_LINE INDENT if ( leftTurn ) : NEW_LINE INDENT head = head + step NEW_LINE DEDENT else : NEW_LINE INDENT if ( remainElements % 2 == 1 ) : NEW_LINE INDENT head = head + step NEW_LINE DEDENT DEDENT remainElements = remainElements // 2 NEW_LINE step = step * 2 NEW_LINE leftTurn = not leftTurn NEW_LINE DEDENT print ( arr [ head - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE printLastElement ( arr , N ) NEW_LINE DEDENT
Distributed C candies among N boys such that difference between maximum and minimum candies received is K | Function to calculate the maximum and minimum number of candies a boy can possess ; All candies will be given to one boy ; All the candies will be given to 1 boy ; Give K candies to 1 st boy initially ; Count remaining candies ; If the last candy of remaining candies is given to the last boy , i . e Nth boy ; Increase minimum count ; Driver code
def max_min ( N , C , K ) : NEW_LINE INDENT maximum = 0 NEW_LINE minimum = 0 NEW_LINE if ( N == 1 ) : NEW_LINE INDENT maximum = minimum = C NEW_LINE DEDENT elif ( K >= C ) : NEW_LINE INDENT maximum = C NEW_LINE minimum = 0 NEW_LINE DEDENT else : NEW_LINE INDENT maximum = K NEW_LINE minimum = 0 NEW_LINE remain_candy = C - K NEW_LINE maximum += remain_candy // N NEW_LINE minimum = remain_candy // N NEW_LINE DEDENT if ( remain_candy % N == N - 1 ) : NEW_LINE INDENT minimum += 1 NEW_LINE DEDENT print ( " Maximum ▁ = ▁ { } " . format ( maximum ) ) NEW_LINE print ( " Minimum ▁ = ▁ { } " . format ( minimum ) ) NEW_LINE DEDENT N = 4 NEW_LINE C = 12 NEW_LINE K = 3 NEW_LINE max_min ( N , C , K ) NEW_LINE
Length of smallest subarray required to be removed to make remaining elements consecutive | Function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; Store the ending index of the longest prefix consecutive array ; Traverse the array to find the longest prefix consecutive sequence ; A [ 0. . . left_index ] is the prefix consecutive sequence ; Store the starting index of the longest suffix consecutive sequence ; Traverse the array to find the longest suffix consecutive sequence ; A [ right_index ... N - 1 ] is the consecutive sequence ; Store the smallest subarray required to be removed ; Check if subarray from the middle can be removed ; Update the right index s . t . A [ 0 , N - 1 ] is consecutive ; If updated_right < N , then update the minimumLength ; Print the required result ; Driver Code
def shortestSubarray ( A , N ) : NEW_LINE INDENT i = 0 NEW_LINE left_index = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( A [ i ] + 1 != A [ i + 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT left_index = i NEW_LINE right_index = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT if ( A [ i ] != A [ i - 1 ] + 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT right_index = i NEW_LINE updated_right = 0 NEW_LINE minLength = min ( N - left_index - 1 , right_index ) NEW_LINE if ( A [ right_index ] <= A [ left_index ] + 1 ) : NEW_LINE INDENT updated_right = ( right_index + A [ left_index ] - A [ right_index ] + 1 ) NEW_LINE if ( updated_right < N ) : NEW_LINE INDENT minLength = min ( minLength , updated_right - left_index - 1 ) NEW_LINE DEDENT DEDENT print ( minLength ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 7 , 4 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE shortestSubarray ( arr , N ) NEW_LINE DEDENT
Check if a string can be split into 3 substrings such that one of them is a substring of the other two | Function to check if string S contains any character with frequency >= 3 or not ; Stores frequency of characters ; Iterate over the string ; Update the frequency of current character ; Iterate over the hash array ; If any character has frequency >= 3 ; Otherwise ; Driver Code
def freqCheck ( S , N ) : NEW_LINE INDENT hash = [ 0 ] * 26 NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( hash [ i ] > 2 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geekseekforgeeks " NEW_LINE N = len ( S ) NEW_LINE print ( freqCheck ( S , N ) ) NEW_LINE DEDENT
Minimize length of a string by removing suffixes and prefixes of same characters | Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code
def minLength ( s ) : NEW_LINE INDENT i = 0 ; j = len ( s ) - 1 NEW_LINE while ( i < j and s [ i ] == s [ j ] ) : NEW_LINE INDENT d = s [ i ] NEW_LINE while ( i <= j and s [ i ] == d ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( i <= j and s [ j ] == d ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT return j - i + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aacbcca " NEW_LINE print ( minLength ( S ) ) NEW_LINE DEDENT
Number of Binary Search Trees of height H consisting of H + 1 nodes | Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver Code
def power ( x , y ) : NEW_LINE INDENT mod = 1000000007 NEW_LINE res = 1 NEW_LINE x = x % mod NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def CountBST ( H ) : NEW_LINE INDENT return power ( 2 , H ) NEW_LINE DEDENT H = 2 NEW_LINE print ( CountBST ( H ) ) NEW_LINE
Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1 ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
def rotateString ( n , m , s ) : NEW_LINE INDENT v = [ ] NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( len ( v ) >= 2 and ( n - ( v [ - 1 ] - v [ 0 ] ) - 1 ) > m ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( cnt <= 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "101001" NEW_LINE M = 1 NEW_LINE N = len ( S ) NEW_LINE rotateString ( N , M , S ) NEW_LINE DEDENT
Number obtained by reducing sum of digits of 2 N into a single digit | Function to find the number obtained by reducing sum of digits of 2 ^ N into a single digit ; Stores answers for different values of N ; Driver Code
def findNumber ( N ) : NEW_LINE INDENT ans = [ 1 , 2 , 4 , 8 , 7 , 5 ] NEW_LINE return ans [ N % 6 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( findNumber ( N ) ) NEW_LINE DEDENT
Check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other | Function to check if two given piles can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other ; If maximum of A & B exceeds the twice of minimum of A & B ; Not possible to empty the piles ; If sum of both the coins is divisible by 3 , then print Yes ; Otherwise , print " No " ; Driver Code
def canBeEmptied ( A , B ) : NEW_LINE INDENT if ( max ( A , B ) > 2 * min ( A , B ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT if ( ( A + B ) % 3 == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 2 NEW_LINE canBeEmptied ( A , B ) NEW_LINE DEDENT
Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Driver Code ; Given value of N & B ; Function call to find maximum index the pointer can reach
def maximumIndex ( N , B ) : NEW_LINE INDENT i , j = 0 , 1 NEW_LINE cnt = 0 NEW_LINE sum = N * ( N + 1 ) // 2 NEW_LINE flag = False NEW_LINE while ( cnt < N ) : NEW_LINE INDENT i += j NEW_LINE j += 1 NEW_LINE cnt += 1 NEW_LINE if ( i == B ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not flag ) : NEW_LINE INDENT print ( sum ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( sum - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , B = 4 , 6 NEW_LINE maximumIndex ( N , B ) NEW_LINE DEDENT
Generate longest possible array with product K such that each array element is divisible by its previous adjacent element | Function to construct longest array with product K such that each element is divisible by its previous element ; Stores the prime factors of K ; Stores the power to which primefactor i is raised ; Sort prime factors in descending order ; Stores the final array ; Multiply the last element by K ; Print the constructed array ; Driver Code
def findLongestArray ( K ) : NEW_LINE INDENT primefactors = [ ] NEW_LINE K_temp = K NEW_LINE i = 2 NEW_LINE while i * i <= K : NEW_LINE INDENT count = 0 NEW_LINE while ( K_temp % i == 0 ) : NEW_LINE INDENT K_temp //= i NEW_LINE count += 1 NEW_LINE DEDENT if ( count > 0 ) : NEW_LINE INDENT primefactors . append ( [ count , i ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( K_temp != 1 ) : NEW_LINE INDENT primefactors . append ( [ 1 , K_temp ] ) NEW_LINE DEDENT primefactors . sort ( ) NEW_LINE answer = [ primefactors [ 0 ] [ 0 ] , primefactors [ 0 ] [ 1 ] ] NEW_LINE answer [ - 1 ] *= K NEW_LINE for i in range ( primefactors [ 0 ] [ 0 ] ) : NEW_LINE INDENT answer [ - 1 ] //= primefactors [ 0 ] [ 1 ] NEW_LINE DEDENT print ( " { " , end = " " ) NEW_LINE for i in range ( len ( answer ) ) : NEW_LINE INDENT if ( i == len ( answer ) - 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " } " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( answer [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 4 NEW_LINE findLongestArray ( K ) NEW_LINE DEDENT