codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increment operations to make K elements equal | Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code
def minOperations ( ar , k ) : NEW_LINE INDENT ar = sorted ( ar ) NEW_LINE opsNeeded = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT opsNeeded += ar [ k - 1 ] - ar [ i ] NEW_LINE DEDENT ans = opsNeeded NEW_LINE for i in range ( k , len ( ar ) ) : NEW_LINE INDENT opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) NEW_LINE opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) NEW_LINE ans = min ( ans , opsNeeded ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 1 , 9 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( minOperations ( arr , k ) ) NEW_LINE
Lexicographical ordering using Heap Sort | Used for index in heap ; Predefining the heap array ; Defining formation of the heap ; Iterative heapiFy ; Just swapping if the element is smaller than already stored element ; Swapping the current index with its child ; Moving upward in the heap ; Defining heap sort ; Taking output of the minimum element ; Set first element as a last one ; Decrement of the size of the string ; Initializing the left and right index ; Process of heap sort If root element is minimum than its both of the child then break ; Otherwise checking that the child which one is smaller , swap them with parent element ; Swapping ; Changing the left index and right index ; Utility function ; To heapiFy ; Calling heap sort function ; Driver Code
x = - 1 ; NEW_LINE heap = [ 0 ] * 1000 ; NEW_LINE def heapForm ( k ) : NEW_LINE INDENT global x ; NEW_LINE x += 1 ; NEW_LINE heap [ x ] = k ; NEW_LINE child = x ; NEW_LINE index = x // 2 ; NEW_LINE while ( index >= 0 ) : NEW_LINE INDENT if ( heap [ index ] > heap [ child ] ) : NEW_LINE INDENT tmp = heap [ index ] ; NEW_LINE heap [ index ] = heap [ child ] ; NEW_LINE heap [ child ] = tmp ; NEW_LINE child = index ; NEW_LINE index = index // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT def heapSort ( ) : NEW_LINE INDENT global x ; NEW_LINE while ( x >= 0 ) : NEW_LINE INDENT k = heap [ 0 ] ; NEW_LINE print ( k , end = " ▁ " ) ; NEW_LINE heap [ 0 ] = heap [ x ] ; NEW_LINE x = x - 1 ; NEW_LINE tmp = - 1 ; NEW_LINE index = 0 ; NEW_LINE length = x ; NEW_LINE left1 = 1 ; NEW_LINE right1 = left1 + 1 ; NEW_LINE while ( left1 <= length ) : NEW_LINE INDENT if ( heap [ index ] <= heap [ left1 ] and heap [ index ] <= heap [ right1 ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( heap [ left1 ] < heap [ right1 ] ) : NEW_LINE INDENT tmp = heap [ index ] ; NEW_LINE heap [ index ] = heap [ left1 ] ; NEW_LINE heap [ left1 ] = tmp ; NEW_LINE index = left1 ; NEW_LINE DEDENT else : NEW_LINE INDENT tmp = heap [ index ] ; NEW_LINE heap [ index ] = heap [ right1 ] ; NEW_LINE heap [ right1 ] = tmp ; NEW_LINE index = right1 ; NEW_LINE DEDENT DEDENT left1 = 2 * left1 ; NEW_LINE right1 = left1 + 1 ; NEW_LINE DEDENT DEDENT DEDENT def sort ( k , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT heapForm ( k [ i ] ) ; NEW_LINE DEDENT heapSort ( ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sort ( arr , n ) ; NEW_LINE DEDENT
Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
def getNumber ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * n ; NEW_LINE i = 0 ; NEW_LINE odd = 1 ; NEW_LINE while ( odd <= n ) : NEW_LINE INDENT arr [ i ] = odd ; NEW_LINE i += 1 ; NEW_LINE odd += 2 ; NEW_LINE DEDENT even = 2 ; NEW_LINE while ( even <= n ) : NEW_LINE INDENT arr [ i ] = even ; NEW_LINE i += 1 ; NEW_LINE even += 2 ; NEW_LINE DEDENT return arr [ k - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE k = 5 ; NEW_LINE print ( getNumber ( n , k ) ) ; NEW_LINE DEDENT
Swap Alternate Boundary Pairs | Utility function to print the contents of an array ; Function to update the array ; Initialize the pointers ; While there are elements to swap ; Update the pointers ; Print the updated array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def UpdateArr ( arr , n ) : NEW_LINE INDENT i = 0 ; NEW_LINE j = n - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT temp = arr [ i ] ; NEW_LINE arr [ i ] = arr [ j ] ; NEW_LINE arr [ j ] = temp ; NEW_LINE i += 2 ; NEW_LINE j -= 2 ; NEW_LINE DEDENT printArr ( arr , n ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE UpdateArr ( arr , n ) ; NEW_LINE DEDENT
Count permutation such that sequence is non decreasing | Python3 implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver code ; Pre - calculating factorials
N = 20 NEW_LINE fact = [ 0 ] * N ; NEW_LINE def pre ( ) : NEW_LINE INDENT fact [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] ; NEW_LINE DEDENT DEDENT def CountPermutation ( a , n ) : NEW_LINE INDENT ways = 1 ; NEW_LINE a . sort ( ) ; NEW_LINE size = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] == a [ i - 1 ] ) : NEW_LINE INDENT size += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ways *= fact [ size ] ; NEW_LINE size = 1 ; NEW_LINE DEDENT DEDENT ways *= fact [ size ] ; NEW_LINE return ways ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE pre ( ) ; NEW_LINE print ( CountPermutation ( a , n ) ) ; NEW_LINE DEDENT
Smallest element greater than X not present in the array | Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
def Next_greater ( a , n , x ) : NEW_LINE INDENT a = sorted ( a ) NEW_LINE low , high , ans = 0 , n - 1 , x + 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( a [ mid ] <= ans ) : NEW_LINE INDENT if ( a [ mid ] == ans ) : NEW_LINE INDENT ans += 1 NEW_LINE high = n - 1 NEW_LINE DEDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 1 , 5 , 10 , 4 , 7 ] NEW_LINE x = 4 NEW_LINE n = len ( a ) NEW_LINE print ( Next_greater ( a , n , x ) ) NEW_LINE
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
def printArrangement ( a , n ) : NEW_LINE INDENT a = sorted ( a ) NEW_LINE b = [ 0 for i in range ( n ) ] NEW_LINE low = 0 NEW_LINE high = n - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT b [ low ] = a [ i ] NEW_LINE low += 1 NEW_LINE DEDENT else : NEW_LINE INDENT b [ high ] = a [ i ] NEW_LINE high -= 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT elif ( i == ( n - 1 ) ) : NEW_LINE INDENT if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT a = [ 1 , 4 , 4 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE printArrangement ( a , n ) NEW_LINE
Greatest contiguous sub | Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
def findSubarray ( a , k , n ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( i , i + k ) : NEW_LINE INDENT temp . append ( a [ j ] ) NEW_LINE DEDENT vec . append ( temp ) NEW_LINE DEDENT vec = sorted ( vec ) NEW_LINE return vec [ len ( vec ) - 1 ] NEW_LINE DEDENT a = [ 1 , 4 , 3 , 2 , 5 ] NEW_LINE k = 4 NEW_LINE n = len ( a ) NEW_LINE ans = findSubarray ( a , k , n ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT
Unbounded Fractional Knapsack | Python implementation of the approach ; Function to return the maximum required value ; maxratio will store the maximum value to weight ratio we can have for any item and maxindex will store the index of that element ; Find the maximum ratio ; The item with the maximum value to weight ratio will be put into the knapsack repeatedly until full ; Driver code
import sys NEW_LINE def knapSack ( W , wt , val , n ) : NEW_LINE INDENT maxratio = - sys . maxsize - 1 ; NEW_LINE maxindex = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( val [ i ] / wt [ i ] ) > maxratio ) : NEW_LINE INDENT maxratio = ( val [ i ] / wt [ i ] ) ; NEW_LINE maxindex = i ; NEW_LINE DEDENT DEDENT return ( W * maxratio ) ; NEW_LINE DEDENT val = [ 14 , 27 , 44 , 19 ] ; NEW_LINE wt = [ 6 , 7 , 9 , 8 ] ; NEW_LINE n = len ( val ) ; NEW_LINE W = 50 ; NEW_LINE print ( knapSack ( W , wt , val , n ) ) ; NEW_LINE
Sort an array without changing position of negative numbers | Function to sort the array such that negative values do not get affected ; Store all non - negative values ; Sort non - negative values ; If current element is non - negative then update it such that all the non - negative values are sorted ; Print the sorted array ; Driver code
def sortArray ( a , n ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT ans . append ( a [ i ] ) NEW_LINE DEDENT DEDENT ans = sorted ( ans ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT a [ i ] = ans [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , - 6 , - 3 , 8 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sortArray ( arr , n ) NEW_LINE
Find a triplet in an array whose sum is closest to a given number | Python3 implementation of the approach ; Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code
import sys NEW_LINE def solution ( arr , x ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE closestSum = sys . maxsize ; NEW_LINE for i in range ( len ( arr ) - 2 ) : NEW_LINE INDENT ptr1 = i + 1 ; ptr2 = len ( arr ) - 1 ; NEW_LINE while ( ptr1 < ptr2 ) : NEW_LINE INDENT sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; NEW_LINE if ( abs ( x - sum ) < abs ( x - closestSum ) ) : NEW_LINE INDENT closestSum = sum ; NEW_LINE DEDENT if ( sum > x ) : NEW_LINE INDENT ptr2 -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 += 1 ; NEW_LINE DEDENT DEDENT DEDENT return closestSum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , 2 , 1 , - 4 ] ; NEW_LINE x = 1 ; NEW_LINE print ( solution ( arr , x ) ) ; NEW_LINE DEDENT
Print elements of an array according to the order defined by another array | set 2 | Function to print an array according to the order defined by another array ; Declaring map and iterator ; Store the frequency of each number of a1 [ ] int the map ; Traverse through a2 [ ] ; Check whether number is present in map or not ; Print that number that many times of its frequency ; Print those numbers that are not present in a2 [ ] ; Driver code
def print_in_order ( a1 , a2 , n , m ) : NEW_LINE INDENT mp = dict . fromkeys ( a1 , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ a1 [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if a2 [ i ] in mp . keys ( ) : NEW_LINE INDENT for j in range ( mp [ a2 [ i ] ] ) : NEW_LINE INDENT print ( a2 [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT del ( mp [ a2 [ i ] ] ) ; NEW_LINE DEDENT DEDENT for key , value in mp . items ( ) : NEW_LINE INDENT for j in range ( value ) : NEW_LINE INDENT print ( key , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a1 = [ 2 , 1 , 2 , 5 , 7 , 1 , 9 , 3 , 6 , 8 , 8 ] ; NEW_LINE a2 = [ 2 , 1 , 8 , 3 ] ; NEW_LINE n = len ( a1 ) ; NEW_LINE m = len ( a2 ) ; NEW_LINE print_in_order ( a1 , a2 , n , m ) ; NEW_LINE DEDENT
Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code
def IsSorted ( A , B , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT x = max ( A [ i ] , B [ i ] ) ; NEW_LINE y = min ( A [ i ] , B [ i ] ) ; NEW_LINE A [ i ] = x ; NEW_LINE B [ i ] = y ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( A [ i ] <= A [ i - 1 ] or B [ i ] <= B [ i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 4 , 3 , 5 , 7 ] ; NEW_LINE B = [ 2 , 2 , 5 , 8 , 9 ] ; NEW_LINE n = len ( A ) ; NEW_LINE if ( IsSorted ( A , B , n ) ) : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT DEDENT
Find the largest possible k | Function to find the largest possible k - multiple set ; Sort the given array ; To store k - multiple set ; Traverse through the whole array ; Check if x / k is already present or not ; Print the k - multiple set ; Driver code ; Function call
def K_multiple ( a , n , k ) : NEW_LINE INDENT a . sort ( ) ; NEW_LINE s = set ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] % k == 0 and a [ i ] // k not in s ) or a [ i ] % k != 0 ) : NEW_LINE INDENT s . add ( a [ i ] ) ; NEW_LINE DEDENT DEDENT for i in s : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 3 , 4 , 5 , 6 , 10 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( a ) ; NEW_LINE K_multiple ( a , n , k ) ; NEW_LINE DEDENT
Maximum water that can be stored between two buildings | Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
def maxWater ( height , n ) : NEW_LINE INDENT maximum = 0 ; NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( height [ i ] < height [ j ] ) : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT elif ( height [ j ] < height [ i ] ) : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ j ] ) ; NEW_LINE j -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT DEDENT return maximum ; NEW_LINE DEDENT height = [ 2 , 1 , 3 , 4 , 6 , 5 ] NEW_LINE n = len ( height ) NEW_LINE print ( maxWater ( height , n ) ) ; NEW_LINE
Count the maximum number of elements that can be selected from the array | Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
def maxSelectionCount ( a , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE a . sort ( ) ; NEW_LINE select = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= select ) : NEW_LINE DEDENT DEDENT res += 1 ; NEW_LINE select += 1 ; NEW_LINE INDENT return res ; NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 3 , 5 , 1 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( maxSelectionCount ( arr , N ) ) ; NEW_LINE
Print combinations of distinct numbers which add up to give sum N | arr [ ] to store all the distinct elements index - next location in array num - given number reducedNum - reduced number ; Set to store all the distinct elements ; Base condition ; Iterate over all the elements and store it into the set ; Calculate the sum of all the elements of the set ; Compare whether the sum is equal to n or not , if it is equal to n print the numbers ; Find previous number stored in the array ; Store all the numbers recursively into the arr [ ] ; Function to find all the distinct combinations of n ; Driver code
def findCombinationsUtil ( arr , index , n , red_num ) : NEW_LINE INDENT s = set ( ) NEW_LINE sum = 0 NEW_LINE if ( red_num < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( red_num == 0 ) : NEW_LINE INDENT for i in range ( index ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT for itr in s : NEW_LINE INDENT sum = sum + ( itr ) NEW_LINE DEDENT if ( sum == n ) : NEW_LINE INDENT for i in s : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE return NEW_LINE DEDENT DEDENT if ( index == 0 ) : NEW_LINE INDENT prev = 1 NEW_LINE DEDENT else : NEW_LINE INDENT prev = arr [ index - 1 ] NEW_LINE DEDENT for k in range ( prev , n + 1 , 1 ) : NEW_LINE INDENT arr [ index ] = k NEW_LINE findCombinationsUtil ( arr , index + 1 , n , red_num - k ) NEW_LINE DEDENT DEDENT def findCombinations ( n ) : NEW_LINE INDENT a = [ 0 for i in range ( n + 1 ) ] NEW_LINE findCombinationsUtil ( a , 0 , n , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE findCombinations ( n ) NEW_LINE DEDENT
Check whether an array can be made strictly increasing by modifying atmost one element | Function that returns true if arr [ ] can be made strictly increasing after modifying at most one element ; To store the number of modifications required to make the array strictly increasing ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; Check whether the last element needs to be modify or not ; If more than 1 modification is required ; Driver code
def check ( arr , n ) : NEW_LINE INDENT modify = 0 NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT arr [ 0 ] = arr [ 1 ] // 2 NEW_LINE modify += 1 NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( ( arr [ i - 1 ] < arr [ i ] and arr [ i + 1 ] < arr [ i ] ) or ( arr [ i - 1 ] > arr [ i ] and arr [ i + 1 ] > arr [ i ] ) ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) // 2 NEW_LINE if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT modify += 1 NEW_LINE DEDENT DEDENT if ( arr [ n - 1 ] < arr [ n - 2 ] ) : NEW_LINE INDENT modify += 1 NEW_LINE DEDENT if ( modify > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 8 , 6 , 9 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if the given matrix is increasing row and column wise | Python3 implementation of the approach ; Function that returns true if the matrix is strictly increasing ; Check if the matrix is strictly increasing ; Out of bound condition ; Out of bound condition ; Driver code
N , M = 2 , 2 NEW_LINE def isMatrixInc ( a ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( i - 1 >= 0 ) : NEW_LINE INDENT if ( a [ i ] [ j ] <= a [ i - 1 ] [ j ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if ( j - 1 >= 0 ) : NEW_LINE INDENT if ( a [ i ] [ j ] <= a [ i ] [ j - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ [ 2 , 10 ] , [ 11 , 20 ] ] ; NEW_LINE if ( isMatrixInc ( a ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT