codeparrot/xlcost-text-to-code huggingface dataset:
Maximum items that can be bought from the cost Array based on given conditions | Function to find the maximum number of items that can be bought from the given cost array ; Sort the given array ; Variables to store the prefix sum , answer and the counter variables ; Initializing the first element of the prefix array ; If we can buy at least one item ; Iterating through the first K items and finding the prefix sum ; Check the number of items that can be bought ; Finding the prefix sum for the remaining elements ; Check the number of iteams that can be bought ; Driver code
def number ( a , n , p , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE pre = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pre . append ( 0 ) NEW_LINE DEDENT ans = 0 NEW_LINE val = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE pre [ 0 ] = a [ 0 ] NEW_LINE if pre [ 0 ] <= p : NEW_LINE INDENT ans = 1 NEW_LINE DEDENT for i in range ( 1 , k - 1 ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + a [ i ] NEW_LINE if pre [ i ] <= p : NEW_LINE INDENT ans = i + 1 NEW_LINE DEDENT DEDENT pre [ k - 1 ] = a [ k - 1 ] NEW_LINE for i in range ( k - 1 , n ) : NEW_LINE INDENT if i >= k : NEW_LINE INDENT pre [ i ] += pre [ i - k ] + a [ i ] NEW_LINE DEDENT if pre [ i ] <= p : NEW_LINE INDENT ans = i + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE arr = [ 2 , 4 , 3 , 5 , 7 ] NEW_LINE p = 11 NEW_LINE k = 2 NEW_LINE print ( number ( arr , n , p , k ) ) NEW_LINE
Find the Maximum possible Sum for the given conditions | Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; print profit ; Driver code
def maxProfit ( arr ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] - ( 1 * i ) ) > 0 : NEW_LINE INDENT ans += ( arr [ i ] - ( 1 * i ) ) NEW_LINE DEDENT if ( arr [ i ] - ( 1 * i ) ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 6 , 6 ] NEW_LINE print ( maxProfit ( arr ) ) NEW_LINE DEDENT
Check if the array can be sorted only if the elements on given positions can be swapped | Function to check if the array can be sorted only if the elements on the given positions can be swapped ; Creating an array for marking the positions ; Iterating through the array and mark the positions ; Iterating through the given array ; If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos ; Sorting the required segment ; Checking if the vector is sorted or not ; Print yes if it is sorted ; Driver code
def check_vector ( A , n , p ) : NEW_LINE INDENT pos = [ 0 for i in range ( len ( A ) ) ] NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT pos [ p [ i ] - 1 ] = 1 NEW_LINE DEDENT flag = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( pos [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT j = i NEW_LINE while ( j < n and pos [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT p = A [ : i ] NEW_LINE q = A [ i : i + j + 1 ] NEW_LINE r = A [ i + j + 1 : len ( A ) ] NEW_LINE q . sort ( reverse = False ) NEW_LINE A = p + q + r NEW_LINE i = j NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 2 , 1 ] NEW_LINE p = [ 1 , 2 ] NEW_LINE check_vector ( A , len ( A ) , p ) NEW_LINE DEDENT
Sort an array of strings based on the given order | Python3 program to sort an array of strings based on the given order ; For storing priority of each character ; Custom comparator function for sort ; Loop through the minimum size between two words ; Check if the characters at position i are different , then the word containing lower valued character is smaller ; When loop breaks without returning , it means the prefix of both words were same till the execution of the loop . Now , the word with the smaller size will occur before in sorted order ; Function to print the new sorted array of strings ; Mapping each character to its occurrence position ; Sorting with custom sort function ; Printing the sorted order of words ; Driver code
from functools import cmp_to_key NEW_LINE mp = { } NEW_LINE def comp ( a , b ) : NEW_LINE INDENT for i in range ( min ( len ( a ) , len ( b ) ) ) : NEW_LINE INDENT if ( mp [ a [ i ] ] != mp [ b [ i ] ] ) : NEW_LINE INDENT if mp [ a [ i ] ] < mp [ b [ i ] ] : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT DEDENT if ( len ( a ) < len ( b ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def printSorted ( words , order ) : NEW_LINE INDENT for i in range ( len ( order ) ) : NEW_LINE INDENT mp [ order [ i ] ] = i NEW_LINE DEDENT words = sorted ( words , key = cmp_to_key ( comp ) ) NEW_LINE for x in words : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT words = [ " word " , " world " , " row " ] NEW_LINE order = " worldabcefghijkmnpqstuvxyz " NEW_LINE printSorted ( words , order ) NEW_LINE
Sort elements of an array in increasing order of absolute difference of adjacent elements | Function to sort the elements of the array by difference ; Sorting the array ; Array to store the elements of the array ; Iterating over the length of the array to include each middle element of array ; Appending the middle element of the array ; Driver Code
def sortDiff ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE out = [ ] NEW_LINE while n : NEW_LINE INDENT out . append ( arr . pop ( n // 2 ) ) NEW_LINE n = n - 1 NEW_LINE DEDENT print ( * out ) NEW_LINE return out NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 1 , 2 , 3 , 0 ] NEW_LINE n = 5 NEW_LINE sortDiff ( arr , n ) NEW_LINE DEDENT
Minimum cost of choosing the array element | Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of arr [ ] ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Print the pref [ ] for the result ; Driver Code
def minimumCost ( arr , N , M ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE pref = [ ] NEW_LINE pref . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pref . append ( arr [ i ] + pref [ i - 1 ] ) NEW_LINE DEDENT for i in range ( M , N ) : NEW_LINE INDENT pref [ i ] += pref [ i - M ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( pref [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 ] NEW_LINE M = 2 NEW_LINE N = len ( arr ) NEW_LINE minimumCost ( arr , N , M ) ; NEW_LINE
Sort an Array alphabetically when each number is converted into words | Variable to store the word form of units digit and up to twenty ; Variable to store the word form of tens digit ; Function to convert a two digit number to the word by using the above defined arrays ; If n is more than 19 , divide it ; If n is non - zero ; Function to print a given number in words ; Stores the word representation of the given number n ; Handles digits at ten millions and hundred millions places ; Handles digits at hundred thousands and one millions places ; Handles digits at thousands and tens thousands places ; Handles digit at hundreds places ; Call the above function to convert the number into words ; Function to sort the array according to the albhabetical order ; Vector to store the number in words with respective elements ; Inserting number in words with respective elements in vector pair ; Sort the vector , this will sort the pair according to the alphabetical order . ; Print the sorted vector content ; Driver code
one = [ " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " ] NEW_LINE ten = [ " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " ] NEW_LINE def numToWords ( n , s ) : NEW_LINE INDENT st = " " NEW_LINE if ( n > 19 ) : NEW_LINE INDENT st += ten [ n // 10 ] + one [ n % 10 ] NEW_LINE DEDENT else : NEW_LINE INDENT st += one [ n ] NEW_LINE DEDENT if ( n ) : NEW_LINE INDENT st += s NEW_LINE DEDENT return st NEW_LINE DEDENT def convertToWords ( n ) : NEW_LINE INDENT out = " " NEW_LINE out += numToWords ( ( n // 10000000 ) , " crore ▁ " ) NEW_LINE out += numToWords ( ( ( n // 100000 ) % 100 ) , " lakh ▁ " ) NEW_LINE out += numToWords ( ( ( n // 1000 ) % 100 ) , " thousand ▁ " ) NEW_LINE out += numToWords ( ( ( n // 100 ) % 10 ) , " hundred ▁ " ) NEW_LINE if ( n > 100 and n % 100 ) : NEW_LINE INDENT out += " and ▁ " NEW_LINE DEDENT out += numToWords ( ( n % 100 ) , " " ) NEW_LINE return out NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( ( convertToWords ( arr [ i ] ) , arr [ i ] ) ) ; NEW_LINE DEDENT vp . sort ( ) NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 10 , 102 , 31 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT
Maximum Possible Rating of a Coding Contest | Python3 program to find the Maximum Possible Rating of a Coding Contest ; Function to sort all problems descending to upvotes ; Function to return maximum rating ; Declaring vector of pairs ; Each pair represents a problem with its points and upvotes ; Step ( 1 ) - Sort problems by their upvotes value in decreasing order ; Declaring min_heap or priority queue to track of the problem with minimum points . ; Step ( 2 ) - Loop for i = 0 to K - 1 and do accordingly ; Step ( 3 ) - Loop for i = K to N - 1 and do accordingly ; Driver code
import heapq NEW_LINE def Comparator ( p1 ) : NEW_LINE INDENT return p1 [ 1 ] NEW_LINE DEDENT def FindMaxRating ( N , Point , Upvote , K ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT vec . append ( [ Point [ i ] , Upvote [ i ] ] ) NEW_LINE DEDENT vec . sort ( reverse = True , key = Comparator ) NEW_LINE pq = [ ] NEW_LINE heapq . heapify ( pq ) NEW_LINE total_points , max_rating = 0 , 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT total_points = ( total_points + vec [ i ] [ 0 ] ) NEW_LINE max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] ) NEW_LINE heapq . heappush ( pq , vec [ i ] [ 0 ] ) NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT if pq [ 0 ] < vec [ i ] [ 0 ] : NEW_LINE INDENT total_points = ( total_points - pq [ 0 ] + vec [ i ] [ 0 ] ) NEW_LINE max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] ) NEW_LINE heapq . heappop ( pq ) NEW_LINE heapq . heappush ( pq , vec [ i ] [ 0 ] ) NEW_LINE DEDENT DEDENT return max_rating NEW_LINE DEDENT Point = [ 2 , 10 , 3 , 1 , 5 , 8 ] NEW_LINE Upvote = [ 5 , 4 , 3 , 9 , 7 , 2 ] NEW_LINE N = len ( Point ) NEW_LINE K = 2 NEW_LINE print ( " Maximum ▁ Rating ▁ of ▁ Coding ▁ Contest ▁ is : " , FindMaxRating ( N , Point , Upvote , K ) ) NEW_LINE
Find sum of all unique elements in the array for K queries | Function to find the sum of unique elements after Q Query ; Updating the array after processing each query ; Making it to 0 - indexing ; Iterating over the array to get the final array ; Variable to store the sum ; Hash to maintain perviously occured elements ; Loop to find the maximum sum ; Driver code
def uniqueSum ( A , R , N , M ) : NEW_LINE INDENT for i in range ( 0 , M ) : NEW_LINE INDENT l = R [ i ] [ 0 ] NEW_LINE r = R [ i ] [ 1 ] + 1 NEW_LINE l -= 1 NEW_LINE r -= 1 NEW_LINE A [ l ] += 1 NEW_LINE if ( r < N ) : NEW_LINE INDENT A [ r ] -= 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT A [ i ] += A [ i - 1 ] NEW_LINE DEDENT ans = 0 NEW_LINE s = { chr } NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] not in s ) : NEW_LINE INDENT ans += A [ i ] NEW_LINE DEDENT s . add ( A [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT A = [ 0 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] , [ 3 , 3 ] ] NEW_LINE N = len ( A ) NEW_LINE M = len ( R ) NEW_LINE print ( uniqueSum ( A , R , N , M ) ) NEW_LINE
Find the Kth pair in ordered list of all possible sorted pairs of the Array | Function to find the k - th pair ; Sorting the array ; Iterating through the array ; Finding the number of same elements ; Checking if N * T is less than the remaining K . If it is , then arr [ i ] is the first element in the required pair ; Printing the K - th pair ; Driver code
def kthpair ( n , k , arr ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE k -= 1 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT t = 1 NEW_LINE while ( arr [ i ] == arr [ i + t ] ) : NEW_LINE INDENT t += 1 NEW_LINE DEDENT if ( t * n > k ) : NEW_LINE INDENT break NEW_LINE DEDENT k = k - t * n NEW_LINE i += t NEW_LINE DEDENT print ( arr [ i ] , " ▁ " , arr [ k // t ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 3 , 2 NEW_LINE arr = [ 3 , 1 , 5 ] NEW_LINE kthpair ( n , k , arr ) NEW_LINE DEDENT
Minimum characters to be replaced to make frequency of all characters same | Function to find the minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the Frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code
def minOperations ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT freq . sort ( reverse = True ) NEW_LINE answer = n NEW_LINE for i in range ( 1 , 27 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT x = n // i NEW_LINE y = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT y += min ( freq [ j ] , x ) NEW_LINE DEDENT answer = min ( answer , n - y ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " BBC " NEW_LINE print ( minOperations ( s ) ) NEW_LINE DEDENT
Check if it is possible to sort an array with conditional swapping of elements at distance K | Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code
def fun ( arr , n , k ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( i , n , k ) : NEW_LINE INDENT v . append ( arr [ j ] ) ; NEW_LINE DEDENT v . sort ( ) ; NEW_LINE x = 0 NEW_LINE for j in range ( i , n , k ) : NEW_LINE INDENT arr [ j ] = v [ x ] ; NEW_LINE x += 1 NEW_LINE DEDENT v = [ ] NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 4 , 2 , 3 , 7 , 6 ] NEW_LINE K = 2 ; NEW_LINE n = len ( arr ) NEW_LINE if ( fun ( arr , n , K ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Sort an Array based on the absolute difference of adjacent elements | Function that arrange the array such that all absolute difference between adjacent element are sorted ; To store the resultant array ; Sorting the given array in ascending order ; Variable to represent left and right ends of the given array ; Traversing the answer array in reverse order and arrange the array elements from arr [ ] in reverse order ; Inserting elements in zig - zag manner ; Displaying the resultant array ; Driver Code ; Function Call
def sortedAdjacentDifferences ( arr , n ) : NEW_LINE INDENT ans = [ 0 ] * n NEW_LINE arr = sorted ( arr ) NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT ans [ i ] = arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans [ i ] = arr [ r ] NEW_LINE r -= 1 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 = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sortedAdjacentDifferences ( arr , n ) NEW_LINE DEDENT
Minimum steps to convert an Array into permutation of numbers from 1 to N | Function to find minimum number of steps to convert a given sequence into a permutation ; Sort the given array ; To store the required minimum number of operations ; Find the operations on each step ; Return the answer ; Driver code ; Function call
def get_permutation ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT result += abs ( arr [ i ] - ( i + 1 ) ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( get_permutation ( arr , n ) ) NEW_LINE DEDENT
Minimum increment or decrement required to sort the array | Top | Python3 program of the above approach ; Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then arr [ ] is sorted ; If dp [ N ] [ maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N ] [ maxE ] ; Return the final result ; Driver Code ; Find the minimum and maximum element from the arr [ ] ; Function Call
import sys NEW_LINE dp = [ [ 0 for x in range ( 1000 ) ] for y in range ( 1000 ) ] NEW_LINE def minimumIncDec ( arr , N , maxE , minE ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ N ] [ maxE ] ) : NEW_LINE INDENT return dp [ N ] [ maxE ] NEW_LINE DEDENT ans = sys . maxsize NEW_LINE for k in range ( minE , maxE + 1 ) : NEW_LINE INDENT x = minimumIncDec ( arr , N - 1 , k , minE ) NEW_LINE ans = min ( ans , x + abs ( arr [ N - 1 ] - k ) ) NEW_LINE DEDENT dp [ N ] [ maxE ] = ans NEW_LINE return dp [ N ] [ maxE ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minE = min ( arr ) NEW_LINE maxE = max ( arr ) NEW_LINE print ( minimumIncDec ( arr , N , maxE , minE ) ) NEW_LINE DEDENT
Sort the major diagonal of the matrix | Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
def sortDiagonal ( a , M , N ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT sm = a [ i ] [ i ] NEW_LINE pos = i NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( sm > a [ j ] [ j ] ) : NEW_LINE INDENT sm = a [ j ] [ j ] NEW_LINE pos = j NEW_LINE DEDENT DEDENT a [ i ] [ i ] , a [ pos ] [ pos ] = a [ pos ] [ pos ] , a [ i ] [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( a [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT a = [ [ 4 , 2 ] , [ 3 , 1 ] ] NEW_LINE sortDiagonal ( a , 2 , 2 ) NEW_LINE
Minimum cost to make an Array a permutation of first N natural numbers | Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call
def make_permutation ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += abs ( i + 1 - arr [ i ] ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 8 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( make_permutation ( arr , n ) ) ; NEW_LINE DEDENT
Find maximum sum from top to bottom row with no adjacent diagonal elements | Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver Code ; Function to find maximum path
def maxSum ( V , n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT aux = [ ] NEW_LINE for j in range ( m ) : NEW_LINE INDENT aux . append ( [ V [ i + 1 ] [ j ] , j ] ) NEW_LINE DEDENT aux = sorted ( aux ) NEW_LINE aux = aux [ : : - 1 ] NEW_LINE for j in range ( m ) : NEW_LINE INDENT for k in range ( m ) : NEW_LINE INDENT if ( aux [ k ] [ 1 ] - j == 0 or abs ( aux [ k ] [ 1 ] - j ) > 1 ) : NEW_LINE INDENT V [ i ] [ j ] += aux [ k ] [ 0 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT ans = max ( ans , V [ 0 ] [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = [ [ 1 , 2 , 3 , 4 ] , [ 8 , 7 , 6 , 5 ] , [ 10 , 11 , 12 , 13 ] ] NEW_LINE n = len ( V ) NEW_LINE m = len ( V [ 0 ] ) NEW_LINE print ( maxSum ( V , n , m ) ) NEW_LINE DEDENT
Count numbers whose maximum sum of distinct digit | Function to find the digit - sum of a number ; Loop to iterate the number digit - wise to find digit - sum ; variable to store last digit ; Function to find the count of number ; Vector to store the Sum of Digits ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; Count variable to store the Count ; Finding the Count of Numbers ; Driver Code ; Function Call
def SumofDigits ( digit ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( digit != 0 ) : NEW_LINE INDENT rem = digit % 10 NEW_LINE sum += rem NEW_LINE digit //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def findCountofNumbers ( arr , n , M ) : NEW_LINE INDENT SumDigits = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = SumofDigits ( arr [ i ] ) NEW_LINE SumDigits . append ( s ) NEW_LINE DEDENT SumDigits . sort ( ) NEW_LINE ip = list ( set ( SumDigits ) ) NEW_LINE count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( len ( SumDigits ) ) : NEW_LINE INDENT if ( sum > M ) : NEW_LINE INDENT break NEW_LINE DEDENT sum += SumDigits [ i ] NEW_LINE if ( sum <= M ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 45 , 16 , 17 , 219 , 32 , 22 ] NEW_LINE M = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( findCountofNumbers ( arr , n , M ) ) NEW_LINE DEDENT
Minimum sum of product of elements of pairs of the given array | Function to find the minimum product ; Sort the array using STL sort ( ) function ; Initialise product to 1 ; Find product of sum of all pairs ; Return the product ; Driver code ; Function call to find product
def minimumProduct ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE product = 1 NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT product *= ( arr [ i ] + arr [ i + 1 ] ) NEW_LINE DEDENT return product NEW_LINE DEDENT arr = [ 1 , 6 , 3 , 1 , 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumProduct ( arr , n ) ) NEW_LINE