codeparrot/xlcost-text-to-code huggingface dataset:
Length of smallest subarray to be removed to make sum of remaining elements divisible by K | Python3 program for the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is divisible by K ; Stores the remainder of each arr [ i ] when divided by K ; Stores total sum of elements ; K has been added to each arr [ i ] to handle - ve integers ; Update the total sum ; Remainder when total_sum is divided by K ; If given array is already divisible by K ; Stores curr_remainder and the most recent index at which curr_remainder has occurred ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; If not possible ; Print the result ; Given array arr [ ] ; Size of array ; Given K ; Function Call
import sys NEW_LINE def removeSmallestSubarray ( arr , n , k ) : NEW_LINE INDENT mod_arr = [ 0 ] * n NEW_LINE total_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mod_arr [ i ] = ( arr [ i ] + k ) % k NEW_LINE total_sum += arr [ i ] NEW_LINE DEDENT target_remainder = total_sum % k NEW_LINE if ( target_remainder == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE return NEW_LINE DEDENT map1 = { } NEW_LINE map1 [ 0 ] = - 1 NEW_LINE curr_remainder = 0 NEW_LINE res = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_remainder = ( curr_remainder + arr [ i ] + k ) % k NEW_LINE map1 [ curr_remainder ] = i NEW_LINE mod = ( curr_remainder - target_remainder + k ) % k NEW_LINE if ( mod in map1 . keys ( ) ) : NEW_LINE INDENT res = min ( res , i - map1 [ mod ] ) NEW_LINE DEDENT DEDENT if ( res == sys . maxsize or res == n ) : NEW_LINE INDENT res = - 1 NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT arr = [ 3 , 1 , 4 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 6 NEW_LINE removeSmallestSubarray ( arr , N , K ) NEW_LINE
Queries to flip characters of a binary string in given range | Function to find the binary by performing all the given queries ; Stores length of the string ; prefixCnt [ i ] : Stores number of times strr [ i ] toggled by performing all the queries ; Update prefixCnt [ Q [ i ] [ 0 ] ] ; Update prefixCnt [ Q [ i ] [ 1 ] + 1 ] ; Calculate prefix sum of prefixCnt [ i ] ; Traverse prefixCnt [ ] array ; If ith element toggled odd number of times ; Toggled i - th element of binary string ; Driver Code
def toggleQuery ( strr , Q , M ) : NEW_LINE INDENT strr = [ i for i in strr ] NEW_LINE N = len ( strr ) NEW_LINE prefixCnt = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT prefixCnt [ Q [ i ] [ 0 ] ] += 1 NEW_LINE prefixCnt [ Q [ i ] [ 1 ] + 1 ] -= 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT prefixCnt [ i ] += prefixCnt [ i - 1 ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( prefixCnt [ i ] % 2 ) : NEW_LINE INDENT strr [ i ] = ( chr ( ord ( '1' ) - ord ( strr [ i ] ) + ord ( '0' ) ) ) NEW_LINE DEDENT DEDENT return " " . join ( strr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = "101010" ; NEW_LINE Q = [ [ 0 , 1 ] , [ 2 , 5 ] , [ 2 , 3 ] , [ 1 , 4 ] , [ 0 , 5 ] ] NEW_LINE M = len ( Q ) NEW_LINE print ( toggleQuery ( strr , Q , M ) ) NEW_LINE DEDENT
Maximum length possible by cutting N given woods into at least K pieces | Function to check if it is possible to cut woods into K pieces of length len ; Stores count of pieces having length equal to K ; Traverse wood [ ] array ; Update count ; Function to find the maximum value of L ; Stores minimum possible of L ; Stores maximum possible value of L ; Apply binary search over the range [ left , right ] ; Stores mid value of left and right ; If it is possible to cut woods into K pieces having length of each piece equal to mid ; Update left ; Update right ; Driver Code
def isValid ( wood , N , len , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += wood [ i ] // len NEW_LINE DEDENT return ( count >= K ) NEW_LINE DEDENT def findMaxLen ( wood , N , K ) : NEW_LINE INDENT left = 1 NEW_LINE right = max ( wood ) NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE if ( isValid ( wood , N , mid , K ) ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return right NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT wood = [ 5 , 9 , 7 ] NEW_LINE N = len ( wood ) NEW_LINE K = 4 NEW_LINE print ( findMaxLen ( wood , N , K ) ) NEW_LINE DEDENT
Count quadruplets with sum K from given array | Function to return the number of quadruplets with the given sum ; Initialize answer ; All possible first elements ; All possible second elements ; All possible third elements ; All possible fourth elements ; Increment counter by 1 if quadruplet sum is S ; Return the final count ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call
def countSum ( a , n , sum ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 3 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 2 ) : NEW_LINE INDENT for k in range ( j + 1 , n - 1 ) : NEW_LINE INDENT for l in range ( k + 1 , n ) : NEW_LINE INDENT if ( a [ i ] + a [ j ] + a [ k ] + a [ l ] == sum ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] NEW_LINE S = 13 NEW_LINE N = len ( arr ) NEW_LINE print ( countSum ( arr , N , S ) ) NEW_LINE DEDENT
Minimum adjacent swaps to group similar characters together | Function to find minimum adjacent swaps required to make all the same character adjacent ; Initialize answer ; Create a 2D array of size 26 ; Traverse the string ; Get character ; Append the current index in the corresponding vector ; Traverse each character from a to z ; Add difference of adjacent index ; Return answer ; Driver Code ; Given string ; Size of string ; Function Call
def minSwaps ( S , n ) : NEW_LINE INDENT swaps = 0 NEW_LINE arr = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pos = ord ( S [ i ] ) - ord ( ' a ' ) NEW_LINE arr [ pos ] . append ( i ) NEW_LINE DEDENT for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT pos = ch - ord ( ' a ' ) NEW_LINE for i in range ( 1 , len ( arr [ pos ] ) ) : NEW_LINE INDENT swaps += abs ( arr [ pos ] [ i ] - arr [ pos ] [ i - 1 ] - 1 ) NEW_LINE DEDENT DEDENT return swaps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abbccabbcc " NEW_LINE N = len ( S ) NEW_LINE print ( minSwaps ( S , N ) ) NEW_LINE DEDENT
Construct a graph which does not contain any pair of adjacent nodes with same value | Function that prints the edges of the generated graph ; First print connections stored in store [ ] ; Check if there is more than one occurrence of 1 st unique element ; Print all other occurrence of 1 st unique element with second unique element ; Function to construct the graph such that the every adjacent nodes have different value ; Stores pair of edges formed ; Stores first unique occurrence ; Check for the second unique occurrence ; Store indices of 2 nd unique occurrence ; To check if arr has only 1 unique element or not ; Store the connections of all unique elements with Node 1 ; If value at node ( i + 1 ) is same as value at Node 1 then store its indices ; If count is zero then it 's not  possible to construct the graph ; If more than 1 unique element is present ; Print the edges ; Driver Code ; Given array having node values ; Function Call
def printConnections ( store , ind , ind1 ) : NEW_LINE INDENT for pr in store : NEW_LINE INDENT print ( pr [ 0 ] , pr [ 1 ] ) NEW_LINE DEDENT if ( len ( ind ) != 0 ) : NEW_LINE INDENT for x in ind : NEW_LINE INDENT print ( ind1 , x + 1 ) NEW_LINE DEDENT DEDENT DEDENT def constructGraph ( arr , N ) : NEW_LINE INDENT ind = [ ] NEW_LINE store = [ ] NEW_LINE x = arr [ 0 ] NEW_LINE count , ind1 = 0 , 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] != x ) : NEW_LINE INDENT ind1 = i + 1 NEW_LINE count += 1 NEW_LINE store . append ( [ 1 , i + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ind . append ( i ) NEW_LINE DEDENT DEDENT if count == 0 : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Possible " ) NEW_LINE printConnections ( store , ind , ind1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE arr = [ ' a ' , ' b ' , ' a ' , ' b ' , ' c ' ] NEW_LINE constructGraph ( arr , N ) NEW_LINE DEDENT
Minimum substring flips required to convert given binary string to another | Function that finds the minimum number of operations required such that string A and B are the same ; Stores the count of steps ; Stores the last index whose bits are not same ; Iterate until both string are unequal ; Check till end of string to find rightmost unequals bit ; Update the last index ; Flipping characters up to the last index ; Flip the bit ; Increasing steps by one ; Print the count of steps ; Driver Code ; Given strings A and B ; Function Call
def findMinimumOperations ( a , b ) : NEW_LINE INDENT step = 0 NEW_LINE last_index = 0 NEW_LINE while ( a != b ) : NEW_LINE INDENT a = [ i for i in a ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE INDENT last_index = i NEW_LINE DEDENT DEDENT for i in range ( last_index + 1 ) : NEW_LINE INDENT if ( a [ i ] == '0' ) : NEW_LINE INDENT a [ i ] = '1' NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] = '0' NEW_LINE DEDENT DEDENT a = " " . join ( a ) NEW_LINE step += 1 NEW_LINE DEDENT print ( step ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = "101010" NEW_LINE B = "110011" NEW_LINE findMinimumOperations ( A , B ) NEW_LINE DEDENT
Count all disjoint pairs having absolute difference at least K from a given array | Function to check if it is possible to form M pairs with abs diff at least K ; Traverse the array over [ 0 , M ] ; If valid index ; Return 1 ; Function to count distinct pairs with absolute difference atleasr K ; Stores the count of all possible pairs ; Initialize left and right ; Sort the array ; Perform Binary Search ; Find the value of mid ; Check valid index ; Update ans ; Print the answer ; Driver Code ; Given array arr [ ] ; Given difference K ; Size of the array ; Function call
def isValid ( arr , n , m , d ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT if ( abs ( arr [ n - m + i ] - arr [ i ] ) < d ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def countPairs ( arr , N , K ) : NEW_LINE INDENT ans = 0 NEW_LINE left = 0 NEW_LINE right = N // 2 + 1 NEW_LINE arr . sort ( reverse = False ) NEW_LINE while ( left < right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( isValid ( arr , N , mid , K ) ) : NEW_LINE INDENT ans = mid NEW_LINE left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT print ( ans , end = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 5 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N , K ) NEW_LINE DEDENT
Minimum common element in subarrays of all possible lengths | Function to find maximum distance between every two element ; Stores index of last occurence of each array element ; Initialize temp [ ] with - 1 ; Traverse the array ; If array element has not occurred previously ; Update index in temp ; Otherwise ; Compare temp [ a [ i ] ] with distance from its previous occurence and store the maximum ; Compare temp [ i ] with distance of its last occurence from the end of the array and store the maximum ; Function to find the minimum common element in subarrays of all possible lengths ; Function call to find a the maximum distance between every pair of repetition ; Initialize ans [ ] to - 1 ; Check if subarray of length temp [ i ] contains i as one of the common elements ; Find the minimum of all common elements ; Driver Code
def max_distance ( a , temp , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp [ i ] = - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] not in mp ) : NEW_LINE INDENT temp [ a [ i ] ] = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ a [ i ] ] = max ( temp [ a [ i ] ] , i - mp [ a [ i ] ] ) NEW_LINE DEDENT mp [ a [ i ] ] = i NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( temp [ i ] != - 1 ) : NEW_LINE INDENT temp [ i ] = max ( temp [ i ] , n - mp [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def min_comm_ele ( a , ans , temp , n ) : NEW_LINE INDENT max_distance ( a , temp , n ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans [ i ] = - 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( ans [ temp [ i ] ] == - 1 ) : NEW_LINE INDENT ans [ temp [ i ] ] = i NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i > 1 and ans [ i - 1 ] != - 1 ) : NEW_LINE INDENT if ( ans [ i ] == - 1 ) : NEW_LINE INDENT ans [ i ] = ans [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans [ i ] = min ( ans [ i ] , ans [ i - 1 ] ) NEW_LINE DEDENT DEDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE a = [ 1 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE temp = [ 0 ] * 100 NEW_LINE ans = [ 0 ] * 100 NEW_LINE min_comm_ele ( a , ans , temp , N ) NEW_LINE DEDENT
Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | Function to find a triplet that satisfy the conditions ; Traverse the given array ; Stores current element ; Stores element just before the current element ; Stores element just after the current element ; Check the given conditions ; Print a triplet ; If no triplet found ; Driver Code
def FindTrip ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N - 1 ) : NEW_LINE INDENT p = arr [ i - 1 ] NEW_LINE q = arr [ i ] NEW_LINE r = arr [ i + 1 ] NEW_LINE if ( p < q and q > r ) : NEW_LINE INDENT print ( i - 1 , i , i + 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE FindTrip ( arr , N ) NEW_LINE DEDENT
Count subarrays having each distinct element occurring at least twice | Python3 program to implement the above approach ; Function to get the count of subarrays having each element occurring at least twice ; Stores count of subarrays having each distinct element occurring at least twice ; Stores count of unique elements in a subarray ; Store frequency of each element of a subarray ; Traverse the given array ; Count frequency and check conditions for each subarray ; Update frequency ; Check if frequency of arr [ j ] equal to 1 ; Update Count of unique elements ; Update count of unique elements ; If each element of subarray occurs at least twice ; Update cntSub ; Remove all elements from the subarray ; Update cntUnique ; Driver code
from collections import defaultdict NEW_LINE def cntSubarrays ( arr , N ) : NEW_LINE INDENT cntSub = 0 NEW_LINE cntUnique = 0 NEW_LINE cntFreq = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i , N ) : NEW_LINE INDENT cntFreq [ arr [ j ] ] += 1 NEW_LINE if ( cntFreq [ arr [ j ] ] == 1 ) : NEW_LINE INDENT cntUnique += 1 NEW_LINE DEDENT elif ( cntFreq [ arr [ j ] ] == 2 ) : NEW_LINE INDENT cntUnique -= 1 NEW_LINE DEDENT if ( cntUnique == 0 ) : NEW_LINE INDENT cntSub += 1 NEW_LINE DEDENT DEDENT cntFreq . clear ( ) NEW_LINE cntUnique = 0 NEW_LINE DEDENT return cntSub NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntSubarrays ( arr , N ) ) NEW_LINE DEDENT
Check if permutation of a given string can be made palindromic by removing at most K characters | Function to check if the satisfies the given conditions or not ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; Stores count of distinct character whose frequency is odd ; Traverse the cntFreq [ ] array . ; If frequency of character i is odd ; Update cntOddFreq ; If count of distinct character having odd frequency is <= K + 1 ; Driver Code ; If str satisfy the given conditions
def checkPalinK ( str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE cntFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntFreq [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT cntOddFreq = 0 NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT if ( cntFreq [ i ] % 2 == 1 ) : NEW_LINE INDENT cntOddFreq += 1 NEW_LINE DEDENT DEDENT if ( cntOddFreq <= ( K + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE K = 2 NEW_LINE if ( checkPalinK ( str , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Maximum number of elements that can be removed such that MEX of the given array remains unchanged | Function to find the maximum number of elements that can be removed ; Initialize hash [ ] with 0 s ; Initialize MEX ; Set hash [ i ] = 1 , if i is present in arr [ ] ; Find MEX from the hash ; Print the maximum numbers that can be removed ; Driver Code ; Given array ; Size of the array ; Function Call
def countRemovableElem ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * ( N + 1 ) NEW_LINE mex = N + 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] <= N ) : NEW_LINE INDENT hash [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( hash [ i ] == 0 ) : NEW_LINE INDENT mex = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( N - ( mex - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 1 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE countRemovableElem ( arr , N ) NEW_LINE DEDENT
Print all positions of a given string having count of smaller characters equal on both sides | Function to find indexes of the given that satisfy the condition ; Stores length of given string ; Stores frequency of each character of strr ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given string ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ strr [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; Driver Code
def printIndexes ( strr ) : NEW_LINE INDENT N = len ( strr ) NEW_LINE cntFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntFreq [ ord ( strr [ i ] ) ] += 1 NEW_LINE DEDENT cntLeftFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntLeft = 0 NEW_LINE cntRight = 0 NEW_LINE for j in range ( ord ( strr [ i ] ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT cntLeft += cntLeftFreq [ j ] NEW_LINE cntRight += ( cntFreq [ j ] - cntLeftFreq [ j ] ) NEW_LINE DEDENT cntLeftFreq [ ord ( strr [ i ] ) ] += 1 NEW_LINE if ( cntLeft == cntRight and cntLeft != 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " aabacdabbb " NEW_LINE printIndexes ( strr ) NEW_LINE DEDENT
Count three | Function to count three - digit numbers having difference x with its reverse ; If x is not multiple of 99 ; No solution exists ; Generate all possible pairs of digits [ 1 , 9 ] ; If any pair is obtained with difference x / 99 ; Increase count ; Return the count ; Driver Code
def Count_Number ( x ) : NEW_LINE INDENT ans = 0 ; NEW_LINE if ( x % 99 != 0 ) : NEW_LINE INDENT ans = - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT diff = x / 99 ; NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT if ( ( i - j ) == diff ) : NEW_LINE INDENT ans += 10 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 792 ; NEW_LINE print ( Count_Number ( x ) ) ; NEW_LINE DEDENT
Check if a point having maximum X and Y coordinates exists or not | Python3 program for the above approach ; Initialize INF as inifnity ; Function to return the pohaving maximum X and Y coordinates ; Base Case ; Stores if valid poexists ; If poarr [ i ] is valid ; Check for the same point ; Check for a valid point ; If current pois the required point ; Otherwise ; Function to find the required point ; Stores the powith maximum X and Y - coordinates ; If no required poexists ; Driver Code ; Given array of points ; Function Call
import sys NEW_LINE INF = sys . maxsize ; NEW_LINE def findMaxPoint ( arr , i , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return [ INF , INF ] NEW_LINE DEDENT flag = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( j == i ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( arr [ j ] [ 0 ] >= arr [ i ] [ 0 ] or arr [ j ] [ 1 ] >= arr [ i ] [ 1 ] ) : NEW_LINE INDENT flag = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return arr [ i ] ; NEW_LINE DEDENT return findMaxPoint ( arr , i + 1 , n ) ; NEW_LINE DEDENT def findMaxPoints ( arr , n ) : NEW_LINE INDENT ans = findMaxPoint ( arr , 0 , n ) ; NEW_LINE if ( ans [ 0 ] == INF ) : 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 = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 3 , 4 ] , [ 4 , 3 ] , [ 5 , 5 ] ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findMaxPoints ( arr , N ) ; NEW_LINE DEDENT
Check if a point having maximum X and Y coordinates exists or not | Python3 program for the above approach ; Function to find the pohaving max X and Y coordinates ; Initialize maxX and maxY ; Length of the given array ; Get maximum X & Y coordinates ; Check if the required point i . e . , ( maxX , maxY ) is present ; If powith maximum X and Y coordinates is present ; If no such poexists ; Driver Code ; Given array of points ; Pranswer
import sys ; NEW_LINE def findMaxPoint ( arr ) : NEW_LINE INDENT maxX = - sys . maxsize ; NEW_LINE maxY = - sys . maxsize ; NEW_LINE n = len ( arr ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxX = max ( maxX , arr [ i ] [ 0 ] ) ; NEW_LINE maxY = max ( maxY , arr [ i ] [ 1 ] ) ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( maxX == arr [ i ] [ 0 ] and maxY == arr [ i ] [ 1 ] ) : NEW_LINE INDENT print ( " ( " , maxX , " , ▁ " , maxY , " ) " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT print ( - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 3 , 4 ] , [ 4 , 3 ] , [ 5 , 5 ] ] ; NEW_LINE findMaxPoint ( arr ) ; NEW_LINE DEDENT
Search insert position of K in a sorted array | Function to find insert position of K ; Traverse the array ; If K is found ; If arr [ i ] exceeds K ; If all array elements are smaller ; Driver Code
def find_index ( arr , n , K ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == K : NEW_LINE INDENT return i NEW_LINE DEDENT elif arr [ i ] > K : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( find_index ( arr , n , K ) ) NEW_LINE