codeparrot/xlcost-text-to-code huggingface dataset:
Number of Larger Elements on right side in a string | Python3 program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code
MAX_CHAR = 26 ; NEW_LINE def printGreaterCount ( str1 ) : NEW_LINE INDENT len1 = len ( str1 ) ; NEW_LINE ans = [ 0 ] * len1 ; NEW_LINE count = [ 0 ] * MAX_CHAR ; NEW_LINE for i in range ( len1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT count [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE for j in range ( ord ( str1 [ i ] ) - ord ( ' a ' ) + 1 , MAX_CHAR ) : NEW_LINE INDENT ans [ i ] += count [ j ] ; NEW_LINE DEDENT DEDENT for i in range ( len1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT str1 = " abcd " ; NEW_LINE printGreaterCount ( str1 ) ; NEW_LINE
Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to sum ; Store counts of all elements in a dictionary ; Traverse through all the elements ; Search if a pair can be formed with arr [ i ] ; Driver code
def printPairs ( arr , n , sum ) : NEW_LINE INDENT mydict = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = sum - arr [ i ] NEW_LINE if temp in mydict : NEW_LINE INDENT count = mydict [ temp ] NEW_LINE for j in range ( count ) : NEW_LINE INDENT print ( " ( " , temp , " , ▁ " , arr [ i ] , " ) " , sep = " " , end =   ' ' ) NEW_LINE DEDENT DEDENT if arr [ i ] in mydict : NEW_LINE INDENT mydict [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mydict [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 , - 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 6 NEW_LINE printPairs ( arr , n , sum ) NEW_LINE DEDENT
Maximum product quadruple ( sub | A O ( n ) Python 3 program to find maximum quadruple inan array . ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver program to test above function
import sys NEW_LINE def maxProduct ( arr , n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT maxA = - sys . maxsize - 1 NEW_LINE maxB = - sys . maxsize - 1 NEW_LINE maxC = - sys . maxsize - 1 NEW_LINE maxD = - sys . maxsize - 1 NEW_LINE minA = sys . maxsize NEW_LINE minB = sys . maxsize NEW_LINE minC = sys . maxsize NEW_LINE minD = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > maxA ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = maxB NEW_LINE maxB = maxA NEW_LINE maxA = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > maxB ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = maxB NEW_LINE maxB = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > maxC ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > maxD ) : NEW_LINE INDENT maxD = arr [ i ] NEW_LINE DEDENT if ( arr [ i ] < minA ) : NEW_LINE INDENT minD = minC NEW_LINE minC = minB NEW_LINE minB = minA NEW_LINE minA = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < minB ) : NEW_LINE INDENT minD = minC NEW_LINE minC = minB NEW_LINE minB = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < minC ) : NEW_LINE INDENT minD = minC NEW_LINE minC = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < minD ) : NEW_LINE INDENT minD = arr [ i ] NEW_LINE DEDENT DEDENT x = maxA * maxB * maxC * maxD NEW_LINE y = minA * minB * minC * minD NEW_LINE z = minA * minB * maxA * maxB NEW_LINE return max ( x , max ( y , z ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 4 , 3 , - 6 , 7 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE max1 = maxProduct ( arr , n ) NEW_LINE if ( max1 == - 1 ) : NEW_LINE INDENT print ( " No ▁ Quadruple ▁ Exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Maximum ▁ product ▁ is " , max1 ) NEW_LINE DEDENT DEDENT
Ways to choose three points with distance between the most distant points <= L | Returns the number of triplets with distance between farthest points <= L ; sort to get ordered triplets so that we can find the distance between farthest points belonging to a triplet ; generate and check for all possible triplets : { arr [ i ] , arr [ j ] , arr [ k ] } ; Since the array is sorted the farthest points will be a [ i ] and a [ k ] ; ; Driver Code ; set of n points on the X axis
def countTripletsLessThanL ( n , L , arr ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ways = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT mostDistantDistance = arr [ k ] - arr [ i ] NEW_LINE if ( mostDistantDistance <= L ) : NEW_LINE INDENT ways += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ways NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE L = 3 NEW_LINE ans = countTripletsLessThanL ( n , L , arr ) NEW_LINE print ( " Total ▁ Number ▁ of ▁ ways ▁ = " , ans ) NEW_LINE DEDENT
Triplets in array with absolute difference less than k | Return the lower bound i . e smallest index of element having value greater or equal to value ; Return the number of triplet indices satisfies the three constraints ; sort the array ; for each element from index 2 to n - 1. ; finding the lower bound of arr [ i ] - k . ; If there are at least two elements between lower bound and current element . ; increment the count by lb - i C 2. ; Driver code
def binary_lower ( value , arr , n ) : NEW_LINE INDENT start = 0 NEW_LINE end = n - 1 NEW_LINE ans = - 1 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( arr [ mid ] >= value ) : NEW_LINE INDENT end = mid - 1 NEW_LINE ans = mid NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def countTriplet ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT cur = ( binary_lower ( arr [ i ] - k , arr , n ) ) NEW_LINE if ( cur <= i - 2 ) : NEW_LINE INDENT count += ( ( i - cur ) * ( i - cur - 1 ) ) // 2 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 3 ] NEW_LINE k = 1 NEW_LINE n = len ( arr ) NEW_LINE print ( countTriplet ( arr , n , k ) ) NEW_LINE DEDENT
Find the one missing number in range | Find the missing number in a range ; here we xor of all the number ; xor last number ; Driver method
def missingNum ( arr , n ) : NEW_LINE INDENT minvalue = min ( arr ) NEW_LINE xornum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT xornum ^= ( minvalue ) ^ arr [ i ] NEW_LINE minvalue = minvalue + 1 NEW_LINE DEDENT return xornum ^ minvalue NEW_LINE DEDENT arr = [ 13 , 12 , 11 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( missingNum ( arr , n ) ) NEW_LINE
Find last index of a character in a string | Returns last index of x if it is present . Else returns - 1. ; String in which char is to be found ; char whose index is to be found
def findLastIndex ( str , x ) : NEW_LINE INDENT index = - 1 NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if str [ i ] == x : NEW_LINE INDENT index = i NEW_LINE DEDENT DEDENT return index NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE x = ' e ' NEW_LINE index = findLastIndex ( str , x ) NEW_LINE if index == - 1 : NEW_LINE INDENT print ( " Character ▁ not ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' Last ▁ index ▁ is ' , index ) NEW_LINE DEDENT
Find last index of a character in a string | Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code
def findLastIndex ( str , x ) : NEW_LINE INDENT for i in range ( len ( str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == x ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE x = ' e ' NEW_LINE index = findLastIndex ( str , x ) NEW_LINE if ( index == - 1 ) : NEW_LINE INDENT print ( " Character ▁ not ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Last ▁ index ▁ is ▁ " , index ) NEW_LINE DEDENT
Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; driver code
def countMaxSetBits ( left , right ) : NEW_LINE INDENT while ( left | ( left + 1 ) ) <= right : NEW_LINE INDENT left |= left + 1 NEW_LINE DEDENT return left NEW_LINE DEDENT l = 1 NEW_LINE r = 5 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE l = 1 NEW_LINE r = 10 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE
Largest number less than or equal to N in BST ( Iterative Approach ) | Python3 code to find the largest value smaller than or equal to N ; To create new BST Node ; To insert a new node in BST ; If tree is empty return new node ; If key is less then or greater then node value then recur down the tree ; Return the ( unchanged ) node pointer ; Returns largest value smaller than or equal to key . If key is smaller than the smallest , it returns - 1. ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( node , key ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if ( key < node . key ) : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE DEDENT elif ( key > node . key ) : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT def findFloor ( root , key ) : NEW_LINE INDENT curr = root NEW_LINE ans = None NEW_LINE while ( curr ) : NEW_LINE INDENT if ( curr . key <= key ) : NEW_LINE INDENT ans = curr NEW_LINE curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT curr = curr . left NEW_LINE DEDENT DEDENT if ( ans ) : NEW_LINE INDENT return ans . key NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE root = None NEW_LINE root = insert ( root , 19 ) NEW_LINE insert ( root , 2 ) NEW_LINE insert ( root , 1 ) NEW_LINE insert ( root , 3 ) NEW_LINE insert ( root , 12 ) NEW_LINE insert ( root , 9 ) NEW_LINE insert ( root , 21 ) NEW_LINE insert ( root , 19 ) NEW_LINE insert ( root , 25 ) NEW_LINE print ( findFloor ( root , N ) ) NEW_LINE DEDENT
Find if given number is sum of first n natural numbers | Function to find no . of elements to be added to get s ; Apply Binary search ; Find mid ; find sum of 1 to mid natural numbers using formula ; If sum is equal to n return mid ; If greater than n do r = mid - 1 ; else do l = mid + 1 ; If not possible , return - 1 ; Drivers code
def findS ( s ) : NEW_LINE INDENT l = 1 NEW_LINE r = int ( s / 2 ) + 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = int ( ( l + r ) / 2 ) NEW_LINE sum = int ( mid * ( mid + 1 ) / 2 ) NEW_LINE if ( sum == s ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( sum > s ) : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT s = 15 NEW_LINE n = findS ( s ) NEW_LINE if ( n == - 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT
Search in an array of strings where non | Compare two string equals are not ; Main function to find string location ; Move mid to the middle ; If mid is empty , find closest non - empty string ; If mid is empty , search in both sides of mid and find the closest non - empty string , and set mid accordingly . ; If str is found at mid ; If str is greater than mid ; If str is smaller than mid ; Driver Code ; Input arr of Strings . ; input Search String
def compareStrings ( str1 , str2 ) : NEW_LINE INDENT i = 0 NEW_LINE while i < len ( str1 ) - 1 and str1 [ i ] == str2 [ i ] : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if str1 [ i ] > str2 [ i ] : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return str1 [ i ] < str2 [ i ] NEW_LINE DEDENT def searchStr ( arr , string , first , last ) : NEW_LINE INDENT if first > last : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( last + first ) // 2 NEW_LINE if len ( arr [ mid ] ) == 0 : NEW_LINE INDENT left , right = mid - 1 , mid + 1 NEW_LINE while True : NEW_LINE INDENT if left < first and right > last : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if right <= last and len ( arr [ right ] ) != 0 : NEW_LINE INDENT mid = right NEW_LINE break NEW_LINE DEDENT if left >= first and len ( arr [ left ] ) != 0 : NEW_LINE INDENT mid = left NEW_LINE break NEW_LINE DEDENT right += 1 NEW_LINE left -= 1 NEW_LINE DEDENT DEDENT if compareStrings ( string , arr [ mid ] ) == 0 : NEW_LINE INDENT return mid NEW_LINE DEDENT if compareStrings ( string , arr [ mid ] ) < 0 : NEW_LINE INDENT return searchStr ( arr , string , mid + 1 , last ) NEW_LINE DEDENT return searchStr ( arr , string , first , mid - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " for " , " " , " " , " " , " geeks " , " ide " , " " , " practice " , " " , " " , " quiz " , " " , " " ] NEW_LINE string = " quiz " NEW_LINE n = len ( arr ) NEW_LINE print ( searchStr ( arr , string , 0 , n - 1 ) ) NEW_LINE DEDENT
Rearrange array elements to maximize the sum of MEX of all prefix arrays | Function to find the maximum sum of MEX of prefix arrays for any arrangement of the given array ; Stores the final arrangement ; Sort the array in increasing order ; Iterate over the array arr [ ] ; Iterate over the array , arr [ ] and push the remaining occurrences of the elements into ans [ ] ; Prthe array , ans [ ] ; Driver Code ; Given array ; Store the size of the array ; Function Call
def maximumMex ( arr , N ) : NEW_LINE INDENT ans = [ ] NEW_LINE arr = sorted ( arr ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i == 0 or arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( i > 0 and arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE maximumMex ( arr , N ) NEW_LINE DEDENT
Sort an array using Bubble Sort without using loops | Function to implement bubble sort without using loops ; Base Case : If array contains a single element ; Base Case : If array contains two elements ; Store the first two elements of the list in variables a and b ; Store remaining elements in the list bs ; Store the list after each recursive call ; If a < b ; Otherwise , if b >= a ; Recursively call for the list less than the last element and and return the newly formed list ; Driver Code ; Print the array
def bubble_sort ( ar ) : NEW_LINE INDENT if len ( ar ) <= 1 : NEW_LINE INDENT return ar NEW_LINE DEDENT if len ( ar ) == 2 : NEW_LINE INDENT return ar if ar [ 0 ] < ar [ 1 ] else [ ar [ 1 ] , ar [ 0 ] ] NEW_LINE DEDENT a , b = ar [ 0 ] , ar [ 1 ] NEW_LINE bs = ar [ 2 : ] NEW_LINE res = [ ] NEW_LINE if a < b : NEW_LINE INDENT res = [ a ] + bubble_sort ( [ b ] + bs ) NEW_LINE DEDENT else : NEW_LINE INDENT res = [ b ] + bubble_sort ( [ a ] + bs ) NEW_LINE DEDENT return bubble_sort ( res [ : - 1 ] ) + res [ - 1 : ] NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 5 , 6 , 2 ] NEW_LINE res = bubble_sort ( arr ) NEW_LINE print ( * res ) NEW_LINE
Modify a given matrix by placing sorted boundary elements in clockwise manner | Function to print the elements of the matrix in row - wise manner ; Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Stores the current boundary elements ; Push the first row ; Push the last column ; Push the last row ; Push the first column ; Sort the boundary elements ; Update the first row ; Update the last column ; Update the last row ; Update the first column ; Print the resultant matrix ; Driver Code ; Given matrix
def printMatrix ( a ) : NEW_LINE INDENT for x in a : NEW_LINE INDENT for y in x : NEW_LINE INDENT print ( y , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def sortBoundaryWise ( a ) : NEW_LINE INDENT k = 0 NEW_LINE l = 0 NEW_LINE m = len ( a ) NEW_LINE n = len ( a [ 0 ] ) NEW_LINE n_k = 0 NEW_LINE n_l = 0 NEW_LINE n_m = m NEW_LINE n_n = n NEW_LINE while ( k < m and l < n ) : NEW_LINE INDENT boundary = [ ] NEW_LINE for i in range ( l , n ) : NEW_LINE INDENT boundary . append ( a [ k ] [ i ] ) NEW_LINE DEDENT k += 1 NEW_LINE for i in range ( k , m ) : NEW_LINE INDENT boundary . append ( a [ i ] [ n - 1 ] ) NEW_LINE DEDENT n -= 1 NEW_LINE if ( k < m ) : NEW_LINE INDENT for i in range ( n - 1 , l - 1 , - 1 ) : NEW_LINE INDENT boundary . append ( a [ m - 1 ] [ i ] ) NEW_LINE DEDENT m -= 1 NEW_LINE DEDENT if ( l < n ) : NEW_LINE INDENT for i in range ( m - 1 , k - 1 , - 1 ) : NEW_LINE INDENT boundary . append ( a [ i ] [ l ] ) NEW_LINE DEDENT l += 1 NEW_LINE DEDENT boundary . sort ( ) NEW_LINE ind = 0 NEW_LINE for i in range ( n_l , n_n ) : NEW_LINE INDENT a [ n_k ] [ i ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_k += 1 NEW_LINE for i in range ( n_k , n_m ) : NEW_LINE INDENT a [ i ] [ n_n - 1 ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_n -= 1 NEW_LINE if ( n_k < n_m ) : NEW_LINE INDENT for i in range ( n_n - 1 , n_l - 1 , - 1 ) : NEW_LINE INDENT a [ n_m - 1 ] [ i ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_m -= 1 NEW_LINE DEDENT if ( n_l < n_n ) : NEW_LINE INDENT for i in range ( n_m - 1 , n_k - 1 , - 1 ) : NEW_LINE INDENT a [ i ] [ n_l ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_l += 1 NEW_LINE DEDENT DEDENT printMatrix ( a ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT matrix = [ [ 9 , 7 , 4 , 5 ] , [ 1 , 6 , 2 , - 6 ] , [ 12 , 20 , 2 , 0 ] , [ - 5 , - 6 , 7 , - 2 ] ] NEW_LINE sortBoundaryWise ( matrix ) NEW_LINE DEDENT
Minimum sum of absolute differences between pairs of a triplet from an array | Python 3 Program for the above approach ; Function to find minimum sum of absolute differences of pairs of a triplet ; Sort the array ; Stores the minimum sum ; Traverse the array ; Update the minimum sum ; Print the minimum sum ; Driver Code ; Input ; Function call to find minimum sum of absolute differences of pairs in a triplet
import sys NEW_LINE def minimum_sum ( A , N ) : NEW_LINE INDENT A . sort ( reverse = False ) NEW_LINE sum = sys . maxsize NEW_LINE for i in range ( N - 2 ) : NEW_LINE INDENT sum = min ( sum , abs ( A [ i ] - A [ i + 1 ] ) + abs ( A [ i + 1 ] - A [ i + 2 ] ) ) NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE minimum_sum ( A , N ) NEW_LINE DEDENT
Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code
def sameChar ( S , N ) : NEW_LINE INDENT S = ' ' . join ( sorted ( S ) ) NEW_LINE mid = ord ( S [ N // 2 ] ) NEW_LINE total_operations = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT total_operations += abs ( ord ( S [ i ] ) - mid ) NEW_LINE DEDENT print ( total_operations ) NEW_LINE DEDENT S = " geeks " NEW_LINE N = len ( S ) NEW_LINE sameChar ( S , N ) NEW_LINE
Maximum score possible from an array with jumps of at most length K | Python program for the above approach ; Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A ; Array to store memoization ; Initialize dp with - 1 ; Driver Code
import sys NEW_LINE def maxScore ( i , A , K , N , dp ) : NEW_LINE INDENT if ( i >= N - 1 ) : NEW_LINE INDENT return A [ N - 1 ] ; NEW_LINE DEDENT if ( dp [ i ] != - 1 ) : NEW_LINE INDENT return dp [ i ] ; NEW_LINE DEDENT score = 1 - sys . maxsize ; NEW_LINE for j in range ( 1 , K + 1 ) : NEW_LINE INDENT score = max ( score , maxScore ( i + j , A , K , N , dp ) ) ; NEW_LINE DEDENT dp [ i ] = score + A [ i ] ; NEW_LINE return dp [ i ] ; NEW_LINE DEDENT def getScore ( A , N , K ) : NEW_LINE INDENT dp = [ 0 ] * N ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT dp [ i ] = - 1 ; NEW_LINE DEDENT print ( maxScore ( 0 , A , K , N , dp ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 100 , - 30 , - 50 , - 15 , - 20 , - 30 ] ; NEW_LINE K = 3 ; NEW_LINE N = len ( A ) ; NEW_LINE getScore ( A , N , K ) ; NEW_LINE DEDENT
Count triplets from an array which can form quadratic equations with real roots | Function to find the count of triplets ( a , b , c ) Such that the equations ax ^ 2 + bx + c = 0 has real roots ; store count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; base case ; Generate all possible triplets ( a , b , c ) ; if the coefficient of X ^ 2 and x are equal ; if coefficient of X ^ 2 or x are equal to the constant ; condition for having real roots ; Driver code ; Function Call
def getcount ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE if ( N < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for b in range ( 0 , N ) : NEW_LINE INDENT for a in range ( 0 , N ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT for c in range ( 0 , N ) : NEW_LINE INDENT if ( c == a or c == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT d = arr [ b ] * arr [ b ] // 4 NEW_LINE if ( arr [ a ] * arr ) <= d : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getcount ( arr , N ) ) NEW_LINE
Check if all array elements can be reduced to less than X | Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Function to calculate gcd of two numbers ; Driver Code
def findAns ( A , N , X ) : NEW_LINE INDENT if ( check ( A , X ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE gcd = GCD ( A [ i ] , A [ j ] ) NEW_LINE if ( gcd != 1 ) : NEW_LINE INDENT if ( gcd <= X ) : NEW_LINE return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def check ( A , X ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT if ( A [ i ] > X ) : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT 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 X = 4 NEW_LINE A = [ 2 , 1 , 5 , 3 , 6 ] NEW_LINE N = 5 NEW_LINE print ( findAns ( A , N , X ) ) NEW_LINE