codeparrot/xlcost-text-to-code huggingface dataset:
Minimum time required to print given string from a circular container based on given conditions | Function to calculate minimum time to print all characters in the string ; Current element where the pointer is pointing ; Find index of that element ; Calculate absolute difference between pointer index and character index as clockwise distance ; Subtract clockwise time from 26 to get anti - clockwise time ; Add minimum of both times to the answer ; Add one unit time to print the character ; Print the final answer ; Driver code ; Given string word ; Function call
def minTime ( word ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE for i in range ( len ( word ) ) : NEW_LINE INDENT k = ord ( word [ i ] ) - 97 NEW_LINE a = abs ( curr - k ) NEW_LINE b = 26 - abs ( curr - k ) NEW_LINE ans += min ( a , b ) NEW_LINE ans += 1 NEW_LINE curr = ord ( word [ i ] ) - 97 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " zjpc " NEW_LINE minTime ( str ) NEW_LINE DEDENT
Minimum decrements or division by a proper divisor required to reduce N to 1 | Function to find the minimum number of steps required to reduce N to 1 ; Stores the number of steps required ; If the value of N is equal to 2 or N is odd ; Decrement N by 1 ; Increment cnt by 1 ; If N is even ; Update N ; Increment cnt by 1 ; Return the number of steps obtained ; Driver Code
def reduceToOne ( N ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( N != 1 ) : NEW_LINE INDENT if ( N == 2 or ( N % 2 == 1 ) ) : NEW_LINE INDENT N = N - 1 NEW_LINE cnt += 1 NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT N = N / ( N / 2 ) NEW_LINE cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 35 NEW_LINE print ( reduceToOne ( N ) ) NEW_LINE DEDENT
Maximum number of diamonds that can be gained in K minutes | Function to find the maximum number of diamonds that can be gained in exactly K minutes ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Pop it from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
def maxDiamonds ( A , N , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( A [ i ] ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE ans = 0 NEW_LINE while ( len ( pq ) > 0 and K > 0 ) : NEW_LINE INDENT pq . sort ( ) NEW_LINE top = pq [ len ( pq ) - 1 ] NEW_LINE pq = pq [ 0 : len ( pq ) - 1 ] NEW_LINE ans += top NEW_LINE top = top // 2 ; NEW_LINE pq . append ( top ) NEW_LINE K -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 1 , 7 , 4 , 2 ] NEW_LINE K = 3 NEW_LINE N = len ( A ) NEW_LINE maxDiamonds ( A , N , K ) NEW_LINE DEDENT
Minimize cost of increments or decrements such that same indexed elements become multiple of each other | Function to find the minimum cost to make A [ i ] multiple of B [ i ] or vice - versa for every array element ; Stores the minimum cost ; Traverse the array ; Case 1 : Update A [ i ] ; Case 2 : Update B [ i ] ; Add the minimum of the above two cases ; Return the resultant cost ; Driver Code
def MinimumCost ( A , B , N ) : NEW_LINE INDENT totalCost = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mod_A = B [ i ] % A [ i ] NEW_LINE totalCost_A = min ( mod_A , A [ i ] - mod_A ) NEW_LINE mod_B = A [ i ] % B [ i ] NEW_LINE totalCost_B = min ( mod_B , B [ i ] - mod_B ) NEW_LINE totalCost += min ( totalCost_A , totalCost_B ) NEW_LINE DEDENT return totalCost NEW_LINE DEDENT A = [ 3 , 6 , 3 ] NEW_LINE B = [ 4 , 8 , 13 ] NEW_LINE N = len ( A ) NEW_LINE print ( MinimumCost ( A , B , N ) ) NEW_LINE
Largest number divisible by 50 that can be formed from a given set of N digits consisting of 0 s and 7 s only | Print the largest number divisible by 50 ; Counting number of 0 s and 7 s ; If count of 7 is divisible by 50 ; If count of 7 is less than 5 ; If count of 7 is not divisible by 50 ; Count of groups of 5 in which count of 7 s can be grouped ; Driver Code ; Given array ; Size of the array
def printLargestDivisible ( arr , N ) : NEW_LINE INDENT count0 = 0 ; count7 = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count0 += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count7 += 1 ; NEW_LINE DEDENT DEDENT if ( count7 % 50 == 0 ) : NEW_LINE INDENT while ( count7 ) : NEW_LINE INDENT count7 -= 1 ; NEW_LINE print ( 7 , end = " " ) ; NEW_LINE DEDENT while ( count0 ) : NEW_LINE INDENT count0 -= 1 ; NEW_LINE print ( count0 , end = " " ) ; NEW_LINE DEDENT DEDENT elif ( count7 < 5 ) : NEW_LINE INDENT if ( count0 == 0 ) : NEW_LINE INDENT print ( " No " , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT count7 = count7 - count7 % 5 ; NEW_LINE while ( count7 ) : NEW_LINE INDENT count7 -= 1 ; NEW_LINE print ( 7 , end = " " ) ; NEW_LINE DEDENT while ( count0 ) : NEW_LINE INDENT count0 -= 1 ; NEW_LINE print ( 0 , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 7 , 0 , 7 , 7 , 7 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE printLargestDivisible ( arr , N ) ; NEW_LINE DEDENT
Rearrange array to maximize sum of GCD of array elements with their respective indices | Function to find the maximum sum of GCD ( arr [ i ] , i ) by rearranging the array ; Sort the array in ascending order ; Stores maximum sum of GCD ( arr [ i ] , i ) by rearranging the array elements ; Generate all possible permutations of the array ; Stores sum of GCD ( arr [ i ] , i ) ; Traverse the array ; Update sum ; Update res ; Driver code
def findMaxValByRearrArr ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE res = 0 NEW_LINE while ( True ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT Sum += __gcd ( i + 1 , arr [ i ] ) NEW_LINE DEDENT res = max ( res , Sum ) NEW_LINE if ( not next_permutation ( arr ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def next_permutation ( p ) : NEW_LINE INDENT for a in range ( len ( p ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( p [ a ] < p [ a + 1 ] ) : NEW_LINE INDENT b = len ( p ) - 1 NEW_LINE while True : NEW_LINE INDENT if ( p [ b ] > p [ a ] ) : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b = len ( p ) - 1 NEW_LINE while a < b : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT b -= 1 NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaxValByRearrArr ( arr , N ) ) NEW_LINE
Minimum removals required to make frequency of each array element equal to its value | Function to find the minimum count of elements required to be removed such that frequency of arr [ i ] equal to arr [ i ] ; Stores frequency of each element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores minimum count of removals ; Traverse the map ; Stores key value of the map ; If frequency of i is less than i ; Update cntMinRem ; If frequency of i is greater than i ; Update cntMinRem ; Driver Code
def min_elements ( arr , N ) : 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 cntMinRem = 0 ; NEW_LINE for it in mp : NEW_LINE INDENT i = it ; NEW_LINE if ( mp [ i ] < i ) : NEW_LINE INDENT cntMinRem += mp [ i ] ; NEW_LINE DEDENT elif ( mp [ i ] > i ) : NEW_LINE INDENT cntMinRem += ( mp [ i ] - i ) ; NEW_LINE DEDENT DEDENT return cntMinRem ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( min_elements ( arr , N ) ) ; NEW_LINE DEDENT
Minimum increments to make all array elements equal with sum same as the given array after exactly one removal | Function to check if an array of equal elements with sum equal to the given array can be obtained or not ; Base case ; Stores sum of array elements ; Stores second largest array element ; Stores the largest array element ; Traverse the array ; Update secMax ; Update Max ; Update secMax ; Update totalSum ; If totalSum is less than secMax * ( N - 1 ) ) ; If totalSum is not divisible by ( N - 1 ) ; Driver Code
def CheckAllarrayEqual ( arr , N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT totalSum = arr [ 0 ] NEW_LINE secMax = - 10 ** 19 NEW_LINE Max = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] >= Max ) : NEW_LINE INDENT secMax = Max NEW_LINE Max = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > secMax ) : NEW_LINE INDENT secMax = arr [ i ] NEW_LINE DEDENT totalSum += arr [ i ] NEW_LINE DEDENT if ( ( secMax * ( N - 1 ) ) > totalSum ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( totalSum % ( N - 1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 2 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE if ( CheckAllarrayEqual ( arr , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element | Function to count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element ; Stores xor of odd and even indexed elements from the end ; Stores xor of odd and even indexed elements from the start ; Stores the required count ; Traverse the array in reverse ; If i is odd ; If i is even ; Traverse the array ; If i is odd ; If i is even ; Removing arr [ i ] , post_even stores XOR of odd indexed elements ; Removing arr [ i ] , post_odd stores XOR of even indexed elements ; Check if they are equal ; If i is odd , xor it with curr_odd ; If i is even , xor it with curr_even ; Finally print res ; Drivers Code ; Given array ; Given size ; Function call
def Remove_one_element ( arr , n ) : NEW_LINE INDENT post_odd = 0 NEW_LINE post_even = 0 NEW_LINE curr_odd = 0 NEW_LINE curr_even = 0 NEW_LINE res = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT post_odd ^= arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT post_even ^= arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT post_odd ^= arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT post_even ^= arr [ i ] NEW_LINE DEDENT X = curr_odd ^ post_even NEW_LINE Y = curr_even ^ post_odd NEW_LINE if ( X == Y ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( i % 2 ) : NEW_LINE INDENT curr_odd ^= arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT curr_even ^= arr [ i ] NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE Remove_one_element ( arr , N ) NEW_LINE DEDENT
Count ways to make sum of odd and even indexed elements equal by removing an array element | Function to count array indices whose removal makes sum of odd and even indexed elements equal ; If size of the array is 1 ; If size of the array is 2 ; Stores sum of even - indexed elements of the given array ; Stores sum of odd - indexed elements of the given array ; Traverse the array ; If i is an even number ; Update sumEven ; If i is an odd number ; Update sumOdd ; Stores sum of even - indexed array elements till i - th index ; Stores sum of odd - indexed array elements till i - th index ; Stores count of indices whose removal makes sum of odd and even indexed elements equal ; Stores sum of even - indexed elements after removing the i - th element ; Stores sum of odd - indexed elements after removing the i - th element ; Traverse the array ; If i is an odd number ; Update currOdd ; Update newEvenSum ; Update newOddSum ; If i is an even number ; Update currEven ; Update newOddSum ; Update newEvenSum ; If newEvenSum is equal to newOddSum ; Increase the count ; If sum of even - indexed and odd - indexed elements is equal by removing the first element ; Increase the count ; If length of the array is an odd number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; If length of the array is an even number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; Driver Code
def cntIndexesToMakeBalance ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sumEven = 0 NEW_LINE sumOdd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT sumEven += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sumOdd += arr [ i ] NEW_LINE DEDENT DEDENT currOdd = 0 NEW_LINE currEven = arr [ 0 ] NEW_LINE res = 0 NEW_LINE newEvenSum = 0 NEW_LINE newOddSum = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT currOdd += arr [ i ] NEW_LINE newEvenSum = ( currEven + sumOdd - currOdd ) NEW_LINE newOddSum = ( currOdd + sumEven - currEven - arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT currEven += arr [ i ] NEW_LINE newOddSum = ( currOdd + sumEven - currEven ) NEW_LINE newEvenSum = ( currEven + sumOdd - currOdd - arr [ i ] ) NEW_LINE DEDENT if ( newEvenSum == newOddSum ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT if ( sumOdd == sumEven - arr [ 0 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT if ( sumOdd == sumEven - arr [ n - 1 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( sumEven == sumOdd - arr [ n - 1 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( cntIndexesToMakeBalance ( arr , n ) ) NEW_LINE DEDENT
Find two numbers from their sum and XOR | Set 2 | Function to find the value of A and B whose sum is X and xor is Y ; Initialize the two numbers ; Case 1 : X < Y ; Case 2 : X - Y is odd ; Case 3 : If both Sum and XOR are equal ; Case 4 : If above cases fails ; Update the value of A ; Check if A & Y value is 0 ; If True , update B ; Otherwise assign - 1 to A , - 1 to B ; Prthe numbers A and B ; Driver Code ; Given Sum and XOR of 2 numbers ; Function Call
def findNums ( X , Y ) : NEW_LINE INDENT A = 0 ; NEW_LINE B = 0 ; NEW_LINE if ( X < Y ) : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT elif ( ( ( abs ( X - Y ) ) & 1 ) != 0 ) : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT elif ( X == Y ) : NEW_LINE INDENT A = 0 ; NEW_LINE B = Y ; NEW_LINE DEDENT else : NEW_LINE INDENT A = ( X - Y ) // 2 ; NEW_LINE if ( ( A & Y ) == 0 ) : NEW_LINE INDENT B = ( A + Y ) ; NEW_LINE DEDENT else : NEW_LINE INDENT A = - 1 ; NEW_LINE B = - 1 ; NEW_LINE DEDENT DEDENT print A ; NEW_LINE print B ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 17 ; NEW_LINE Y = 13 ; NEW_LINE findNums ( X , Y ) ; NEW_LINE DEDENT
Queries to check if count of increasing and decreasing subarrays is same in given range | Function to check if given range have equal number of increasing as well as decreasing subarrays ; Traverse each query ; For 0 - based indexing ; Condition for same count of increasing & decreasing subarray ; Driver Code
def checkCount ( A , Q , q ) : NEW_LINE INDENT for i in range ( q ) : NEW_LINE INDENT L = Q [ i ] [ 0 ] NEW_LINE R = Q [ i ] [ 1 ] NEW_LINE L -= 1 NEW_LINE R -= 1 NEW_LINE if ( ( A [ L ] < A [ L + 1 ] ) != ( A [ R - 1 ] < A [ R ] ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 13 , 12 , 14 ] NEW_LINE Q = [ [ 1 , 4 ] , [ 2 , 4 ] ] NEW_LINE q = len ( Q ) NEW_LINE checkCount ( arr , Q , q ) NEW_LINE DEDENT
Mean of array generated by products of all pairs of the given array | Function to find the mean of pair product array of arr ; Store product of pairs ; Generate all unordered pairs ; Store product of pairs ; Size of pairArray ; Store sum of pairArray ; Stores the mean of pairArray ; Find mean of pairArray ; Return the resultant mean ; Driver Code ; Given array arr ; Function Call
def pairProductMean ( arr , N ) : NEW_LINE INDENT pairArray = [ ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT pairProduct = arr [ i ] * arr [ j ] ; NEW_LINE pairArray . append ( pairProduct ) ; NEW_LINE DEDENT DEDENT length = len ( pairArray ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT sum += pairArray [ i ] ; NEW_LINE DEDENT mean = 0 ; NEW_LINE if ( length != 0 ) : NEW_LINE INDENT mean = sum / length ; NEW_LINE DEDENT else : NEW_LINE INDENT mean = 0 ; NEW_LINE DEDENT return mean ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( " { 0 : . 2f } " . format ( pairProductMean ( arr , N ) ) ) NEW_LINE DEDENT
Smallest number exceeding N whose Kth bit is set | Function to find the number greater than n whose Kth bit is set ; Iterate from N + 1 ; Check if Kth bit is set or not ; Increment M for next number ; Return the minimum value ; Driver Code ; Given N and K ; Function Call
def find_next ( n , k ) : NEW_LINE INDENT M = n + 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( ( M & ( 1 << k ) ) > 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT M += 1 ; NEW_LINE DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 ; K = 2 ; NEW_LINE print ( find_next ( N , K ) ) ; NEW_LINE DEDENT
Smallest number exceeding N whose Kth bit is set | Function to find the number greater than n whose Kth bit is set ; Stores the resultant number ; If Kth bit is not set ; cur will be the sum of all powers of 2 < k ; If the current bit is set ; Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set ; If the kth bit is set ; First unset bit position ; Sum of bits that are set ; Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set ; If Kth bit became unset then set it again ; Return the resultant number ; Driver code ; Print ans
def find_next ( n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE if ( ( n & ( 1 << k ) ) == 0 ) : NEW_LINE INDENT cur = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( n & ( 1 << i ) ) : NEW_LINE INDENT cur += 1 << i NEW_LINE DEDENT DEDENT ans = n - cur + ( 1 << k ) NEW_LINE DEDENT else : NEW_LINE INDENT first_unset_bit , cur = - 1 , 0 NEW_LINE for i in range ( 64 ) : NEW_LINE INDENT if ( ( n & ( 1 << i ) ) == 0 ) : NEW_LINE INDENT first_unset_bit = i NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT cur += ( 1 << i ) NEW_LINE DEDENT DEDENT ans = n - cur + ( 1 << first_unset_bit ) NEW_LINE if ( ( ans & ( 1 << k ) ) == 0 ) : NEW_LINE INDENT ans += ( 1 << k ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT N , K = 15 , 2 NEW_LINE print ( find_next ( N , K ) ) NEW_LINE
Lexicographically largest possible String after removal of K characters | Python3 program to implement the above approach ; Final result string ; If the current char exceeds the character at the top of the stack ; Remove from the end of the string ; Decrease k for the removal ; Insert current character ; Perform remaining K deletions from the end of the string ; Return the string ; Driver code
def largestString ( num , k ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT while ( len ( ans ) and ans [ - 1 ] < num [ i ] and k > 0 ) : NEW_LINE INDENT ans . pop ( ) NEW_LINE k -= 1 NEW_LINE DEDENT ans . append ( num [ i ] ) NEW_LINE DEDENT while ( len ( ans ) and k ) : NEW_LINE INDENT k -= 1 NEW_LINE ans . pop ( ) NEW_LINE DEDENT return ans NEW_LINE DEDENT str = " zyxedcba " NEW_LINE k = 1 NEW_LINE print ( * largestString ( str , k ) , sep = " " ) NEW_LINE
Maximum length of subarray consisting of same type of element on both halves of sub | Function that finds the maximum length of the sub - array that contains equal element on both halves of sub - array ; To store continuous occurence of the element ; To store continuous forward occurence ; To store continuous backward occurence ; To store the maximum length ; Find maximum length ; Print the result ; Given array ; Size of the array ; Function call
def maxLengthSubArray ( A , N ) : NEW_LINE INDENT forward = [ 0 ] * N NEW_LINE backward = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if i == 0 or A [ i ] != A [ i - 1 ] : NEW_LINE INDENT forward [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT forward [ i ] = forward [ i - 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if i == N - 1 or A [ i ] != A [ i + 1 ] : NEW_LINE INDENT backward [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT backward [ i ] = backward [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( A [ i ] != A [ i + 1 ] ) : NEW_LINE INDENT ans = max ( ans , min ( forward [ i ] , backward [ i + 1 ] ) * 2 ) ; NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 6 , 6 , 6 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE maxLengthSubArray ( arr , N ) NEW_LINE
Smallest N digit number divisible by all possible prime digits | Python3 implementation of the above approach ; Function to find the minimum number of n digits divisible by all prime digits . ; Driver Code
from math import * NEW_LINE def minNum ( n ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 210 * ( 10 ** ( n - 1 ) // 210 + 1 ) ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE minNum ( n ) NEW_LINE