codeparrot/xlcost-text-to-code huggingface dataset:
Count array elements with rank not exceeding K | Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Driver Code ; Given array ; Given K ; Function Call
def rankLessThanK ( arr , k , n ) : NEW_LINE INDENT rank = 1 ; NEW_LINE position = 1 ; NEW_LINE arr = sorted ( arr ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i == n - 1 or arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT rank = position ; NEW_LINE if ( rank > k ) : NEW_LINE INDENT return position - 1 ; NEW_LINE DEDENT DEDENT position += 1 ; NEW_LINE DEDENT return n ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 4 ; NEW_LINE print ( rankLessThanK ( arr , K , N ) ) ; NEW_LINE DEDENT
Remove Sub | Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call
def eraseSubdirectory ( dir ) : NEW_LINE INDENT res = [ ] NEW_LINE dir . sort ( ) NEW_LINE res . append ( dir [ 0 ] ) NEW_LINE print ( " { " , dir [ 0 ] , end = " , ▁ " ) NEW_LINE for i in range ( 1 , len ( dir ) ) : NEW_LINE INDENT curr = dir [ i ] NEW_LINE prev = res [ len ( res ) - 1 ] NEW_LINE l = len ( prev ) NEW_LINE if ( len ( curr ) > l and curr [ l ] == ' / ' and prev == curr [ : l ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT res . append ( curr ) NEW_LINE print ( curr , end = " , ▁ " ) NEW_LINE DEDENT print ( " } " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT dir = [ " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ] NEW_LINE eraseSubdirectory ( dir ) NEW_LINE DEDENT
Maximize count of pairs ( i , j ) from two arrays having element from first array not exceeding that from second array | Function to return the maximum number of required pairs ; Max Heap to add values of arr2 [ ] ; Sort the array arr1 [ ] ; Push all arr2 [ ] into Max Heap ; Initialize the ans ; Traverse the arr1 [ ] in decreasing order ; Remove element until a required pair is found ; Return maximum number of pairs ; Driver Code ; Given arrays ; Function Call
def numberOfPairs ( arr1 , n , arr2 , m ) : NEW_LINE INDENT pq = [ ] NEW_LINE arr1 . sort ( reverse = False ) NEW_LINE for j in range ( m ) : NEW_LINE INDENT pq . append ( arr2 [ j ] ) NEW_LINE DEDENT ans = 2 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT pq . sort ( reverse = False ) NEW_LINE if ( pq [ 0 ] >= 2 * arr1 [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE print ( pq [ 0 ] ) NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 3 , 2 , 1 ] NEW_LINE arr2 = [ 3 , 4 , 2 , 1 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( numberOfPairs ( arr1 , N , arr2 , M ) ) NEW_LINE DEDENT
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | Python3 program for above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given arr [ ] ; Function calling
import sys NEW_LINE def minCost ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE raised = 1 / ( n - 1 ) NEW_LINE temp = pow ( arr [ n - 1 ] , raised ) NEW_LINE r = round ( temp ) + 1 NEW_LINE min_cost = sys . maxsize NEW_LINE common_ratio = 1 NEW_LINE for j in range ( 1 , r + 1 ) : NEW_LINE INDENT curr_cost = 0 NEW_LINE prod = 1 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT curr_cost += abs ( arr [ i ] - prod ) NEW_LINE prod *= j NEW_LINE if ( curr_cost >= min_cost ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT min_cost = min ( min_cost , curr_cost ) NEW_LINE common_ratio = j NEW_LINE DEDENT DEDENT print ( min_cost , common_ratio ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE arr = [ 1 , 11 , 4 , 27 , 15 , 33 ] NEW_LINE minCost ( arr , N ) NEW_LINE DEDENT
Mth bit in Nth binary string from a sequence generated by the given operations | Python3 program for above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the string Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code
maxN = 10 NEW_LINE def calculateFib ( fib , n ) : NEW_LINE INDENT fib [ 0 ] = fib [ 1 ] = 1 NEW_LINE for x in range ( 2 , n ) : NEW_LINE INDENT fib [ x ] = ( fib [ x - 1 ] + fib [ x - 2 ] ) NEW_LINE DEDENT DEDENT def find_mth_bit ( n , m , fib ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT len_left = fib [ n - 2 ] NEW_LINE len_right = fib [ n - 1 ] NEW_LINE if ( m <= len_left ) : NEW_LINE INDENT return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) NEW_LINE DEDENT else : NEW_LINE INDENT return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) NEW_LINE DEDENT DEDENT def find_mth_bitUtil ( n , m ) : NEW_LINE INDENT fib = [ 0 for i in range ( maxN ) ] NEW_LINE calculateFib ( fib , maxN ) NEW_LINE ans = find_mth_bit ( n , m , fib ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE m = 3 NEW_LINE find_mth_bitUtil ( n , m ) NEW_LINE DEDENT
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | Function to count the minimum number of adjacent swaps ; Stores the size of the given matrix ; Stores the count of zero at the end of each row ; Traverse the given matrix ; Count of 0 s at the end of the ith row ; Stores the count of swaps ; Traverse the cntZero array ; If count of zero in the i - th row < ( N - i - 1 ) ; Stores the index of the row where count of zero > ( N - i - 1 ) ; If no row found that satisfy the condition ; Swap the adjacent row ; Driver Code
def minAdjSwaps ( mat ) : NEW_LINE INDENT N = len ( mat ) NEW_LINE cntZero = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if mat [ i ] [ j ] != 0 : NEW_LINE INDENT break NEW_LINE DEDENT cntZero [ i ] += 1 NEW_LINE DEDENT DEDENT cntSwaps = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( cntZero [ i ] < ( N - i - 1 ) ) : NEW_LINE INDENT First = i NEW_LINE while ( First < N and cntZero [ First ] < ( N - i - 1 ) ) : NEW_LINE INDENT First += 1 NEW_LINE DEDENT if ( First == N ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( First > i ) : NEW_LINE INDENT cntZero [ First ] = cntZero [ First - 1 ] NEW_LINE cntZero [ First - 1 ] = cntZero [ First ] NEW_LINE First -= 1 NEW_LINE cntSwaps += 1 NEW_LINE DEDENT DEDENT DEDENT return cntSwaps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 0 , 0 , 2 ] , [ 3 , 1 , 0 ] , [ 4 , 0 , 0 ] ] NEW_LINE print ( minAdjSwaps ( mat ) ) NEW_LINE DEDENT
Largest area in a grid unbounded by towers | Function to calculate the largest area unguarded by towers ; Sort the x - coordinates of the list ; Sort the y - coordinates of the list ; dx -- > maximum uncovered tiles in x coordinates ; dy -- > maximum uncovered tiles in y coordinates ; Calculate the maximum uncovered distances for both x and y coordinates ; Largest unguarded area is max ( dx ) - 1 * max ( dy ) - 1 ; Driver Code ; Length and width of the grid ; No of guard towers ; Array to store the x and y coordinates ; Function call
def maxArea ( point_x , point_y , n , length , width ) : NEW_LINE INDENT point_x . sort ( ) NEW_LINE point_y . sort ( ) NEW_LINE dx = point_x [ 0 ] NEW_LINE dy = point_y [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dx = max ( dx , point_x [ i ] - point_x [ i - 1 ] ) NEW_LINE dy = max ( dy , point_y [ i ] - point_y [ i - 1 ] ) NEW_LINE DEDENT dx = max ( dx , ( length + 1 ) - point_x [ n - 1 ] ) NEW_LINE dy = max ( dy , ( width + 1 ) - point_y [ n - 1 ] ) NEW_LINE print ( ( dx - 1 ) * ( dy - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT length = 15 NEW_LINE width = 8 NEW_LINE n = 3 NEW_LINE point_x = [ 3 , 11 , 8 ] NEW_LINE point_y = [ 8 , 2 , 6 ] NEW_LINE maxArea ( point_x , point_y , n , length , width ) NEW_LINE DEDENT
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Given array and K ; Function call
def make_cuts ( arr , n , K ) : NEW_LINE INDENT ans = 0 NEW_LINE poss = [ ] NEW_LINE ce = 0 NEW_LINE co = 0 NEW_LINE for x in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ x ] % 2 == 0 ) : NEW_LINE INDENT ce += 1 NEW_LINE DEDENT else : NEW_LINE INDENT co += 1 NEW_LINE DEDENT if ( ce == co and co > 0 and ce > 0 ) : NEW_LINE INDENT poss . append ( abs ( arr [ x ] - arr [ x + 1 ] ) ) NEW_LINE DEDENT DEDENT poss . sort ( ) NEW_LINE for x in poss : NEW_LINE INDENT if ( K >= x ) : NEW_LINE INDENT ans += 1 NEW_LINE K -= x NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT N = 6 NEW_LINE K = 4 NEW_LINE arr = [ 1 , 2 , 5 , 10 , 15 , 20 ] NEW_LINE print ( make_cuts ( arr , N , K ) ) NEW_LINE
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | Function to check if given elements can be arranged such that sum of its neighbours is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Given array ; Function call
def Arrange ( arr , n ) : NEW_LINE INDENT cost = 0 NEW_LINE index = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT index [ arr [ i ] ] = i NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] + arr [ - 1 ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT cost += abs ( index [ arr [ i ] ] - i ) NEW_LINE DEDENT DEDENT elif ( i == n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT cost += abs ( index [ arr [ i ] ] - i ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT cost += abs ( index [ arr [ i ] ] - i ) NEW_LINE DEDENT DEDENT DEDENT print ( cost ) NEW_LINE return NEW_LINE DEDENT arr = [ 2 , 4 , 5 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE Arrange ( arr , N ) NEW_LINE
XOR of all possible pairwise sum from two given Arrays | Function to calculate the sum of XOR of the sum of every pair ; Stores the XOR of sums of every pair ; Iterate to generate all possible pairs ; Update XOR ; Return the answer ; Driver Code
def XorSum ( A , B , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT ans = ans ^ ( A [ i ] + B [ j ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 4 , 6 , 0 , 0 , 3 , 3 ] NEW_LINE B = [ 0 , 5 , 6 , 5 , 0 , 3 ] NEW_LINE N = len ( A ) NEW_LINE print ( XorSum ( A , B , N ) ) NEW_LINE DEDENT
Maximum non | Python3 program for the above approach ; Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the final answer ; Count this character if freq 1 ; Return count of distinct characters ; Driver Code ; Given string ; Given k ; Function Call
from collections import defaultdict NEW_LINE def maxDistinctChar ( s , n , k ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ s [ i ] ] += 1 NEW_LINE DEDENT v = [ ] NEW_LINE for it in freq . values ( ) : NEW_LINE INDENT v . append ( it ) NEW_LINE DEDENT v . sort ( ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT mn = min ( v [ i ] - 1 , k ) NEW_LINE v [ i ] -= mn NEW_LINE k -= mn NEW_LINE DEDENT if ( k > 0 ) : NEW_LINE INDENT for i in range ( len ( v ) ) : NEW_LINE INDENT mn = min ( v [ i ] , k ) ; NEW_LINE v [ i ] -= mn NEW_LINE k -= mn NEW_LINE DEDENT DEDENT res = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ i ] == 1 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE N = len ( S ) NEW_LINE K = 1 NEW_LINE print ( maxDistinctChar ( S , N , K ) ) NEW_LINE DEDENT
Split N as the sum of K numbers satisfying the given conditions | list to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
primes = [ ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime = [ True ] * 10005 NEW_LINE p = 2 NEW_LINE while p * p <= 1000 : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 1001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , 1001 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT primes . append ( p ) NEW_LINE DEDENT DEDENT DEDENT def generate ( n , k ) : NEW_LINE INDENT prod = [ ] NEW_LINE SieveOfEratosthenes ( ) NEW_LINE l = len ( primes ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( i + 1 , l ) : NEW_LINE INDENT if ( primes [ i ] * primes [ j ] > 0 ) : NEW_LINE INDENT prod . append ( primes [ i ] * primes [ j ] ) NEW_LINE DEDENT DEDENT DEDENT prod . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT sum += prod [ i ] NEW_LINE DEDENT if ( sum > n ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT print ( prod [ i ] , end = " , ▁ " ) NEW_LINE DEDENT print ( n - sum ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 52 NEW_LINE k = 5 NEW_LINE generate ( n , k ) NEW_LINE DEDENT
Maximum modified Array sum possible by choosing elements as per the given conditions | Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Driver code ; Given array arr [ ] ; Function call
def findMaxValue ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = arr [ n - 1 ] NEW_LINE maxPossible = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( maxPossible > 0 ) : NEW_LINE INDENT if ( arr [ i ] >= maxPossible ) : NEW_LINE INDENT ans += ( maxPossible - 1 ) NEW_LINE maxPossible = maxPossible - 1 NEW_LINE DEDENT else : NEW_LINE INDENT maxPossible = arr [ i ] NEW_LINE ans += maxPossible NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 4 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxValue ( arr , n ) ) NEW_LINE DEDENT
Minimum cost to sort an Array such that swapping X and Y costs XY | Function returns the minimum cost to sort the given array ; Create array of pairs in which 1 st element is the array element and 2 nd element is index of first ; Initialize the total cost ; Sort the array with respect to array value ; Initialize the overall minimum which is the 1 st element ; To keep track of visited elements create a visited array & initialize all elements as not visited ; Iterate over every element of the array ; If the element is visited or in the sorted position , and check for next element ; Create a vector which stores all elements of a cycle ; It covers all the elements of a cycle ; If cycle is found then the swapping is required ; Initialize local minimum with 1 st element of the vector as it contains the smallest element in the beginning ; Stores the cost with using only local minimum value . ; Stores the cost of using both local minimum and overall minimum ; Update the result2 ; Store the minimum of the two result to total cost ; Return the minimum cost ; Given array arr [ ] ; Function call
def minCost ( arr , n ) : NEW_LINE INDENT sortedarr = [ ] NEW_LINE total_cost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sortedarr . append ( [ arr [ i ] , i ] ) NEW_LINE DEDENT sortedarr . sort ( ) NEW_LINE overall_minimum = sortedarr [ 0 ] [ 0 ] NEW_LINE vis = [ False ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if vis [ i ] and sortedarr [ i ] [ 1 ] == i : NEW_LINE INDENT continue NEW_LINE DEDENT v = [ ] NEW_LINE j = i NEW_LINE size = 0 NEW_LINE while vis [ j ] == False : NEW_LINE INDENT vis [ j ] = True NEW_LINE v . append ( sortedarr [ j ] [ 0 ] ) NEW_LINE j = sortedarr [ j ] [ 1 ] NEW_LINE size += 1 NEW_LINE DEDENT if size != 0 : NEW_LINE INDENT local_minimum = v [ 0 ] NEW_LINE result1 = 0 NEW_LINE result2 = 0 NEW_LINE for k in range ( 1 , size ) : NEW_LINE INDENT result1 += local_minimum * v [ k ] NEW_LINE DEDENT for k in range ( size ) : NEW_LINE INDENT result2 += overall_minimum * v [ k ] NEW_LINE DEDENT result2 += ( overall_minimum * local_minimum ) NEW_LINE total_cost += min ( result1 , result2 ) NEW_LINE DEDENT DEDENT return total_cost NEW_LINE DEDENT A = [ 1 , 8 , 9 , 7 , 6 ] NEW_LINE ans = minCost ( A , len ( A ) ) NEW_LINE print ( ans ) NEW_LINE
Maximize the last Array element as per the given conditions | Function to find the maximum possible value that can be placed at the last index ; Sort the array elements in ascending order ; If the first element is is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
def maximizeFinalElement ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE if ( arr [ 0 ] != 1 ) : NEW_LINE INDENT arr [ 0 ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] > 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + 1 ; NEW_LINE DEDENT DEDENT return arr [ n - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE arr = [ 3 , 1 , 3 , 4 ] ; NEW_LINE max = maximizeFinalElement ( arr , n ) ; NEW_LINE print ( max ) ; NEW_LINE DEDENT
Find the point on X | Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
def findLeastDist ( A , N ) : NEW_LINE INDENT A . sort ( ) ; NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT return A [ ( N - 1 ) // 2 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT return A [ N // 2 ] ; NEW_LINE DEDENT DEDENT A = [ 4 , 1 , 5 , 10 , 2 ] ; NEW_LINE N = len ( A ) ; NEW_LINE print ( " ( " , findLeastDist ( A , N ) , " , ▁ " , 0 , " ) " ) ; NEW_LINE
Maximum sum of any submatrix of a Matrix which is sorted row | Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; dp [ ] [ ] matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the final result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the dp [ ] [ ] matrix from bottom to the top row ; Update sum at each cell in dp [ ] [ ] ; Update the maximum sum ; Return the maximum sum ; Driver Code ; Given matrix mat [ ] [ ] ; Function call
def maxSubMatSum ( mat ) : NEW_LINE INDENT n = len ( mat ) NEW_LINE m = len ( mat [ 0 ] ) NEW_LINE dp = [ [ 0 ] * m for _ in range ( n ) ] NEW_LINE dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ] NEW_LINE res = dp [ n - 1 ] [ m - 1 ] NEW_LINE for i in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT dp [ n - 1 ] [ i ] = ( mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] ) NEW_LINE res = max ( res , dp [ n - 1 ] [ i ] ) NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] [ m - 1 ] = ( mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] ) NEW_LINE res = max ( res , dp [ i ] [ m - 1 ] ) NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = ( mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] ) NEW_LINE res = max ( res , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ - 6 , - 4 , - 1 ] , [ - 3 , 2 , 4 ] , [ 2 , 5 , 8 ] ] NEW_LINE print ( maxSubMatSum ( mat ) ) NEW_LINE DEDENT
Arrange the numbers in the Array as per given inequalities | Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the final integer ; Return the answer ; Driver Code ; Given List of Integers ; Given list of inequalities ; Function Call ; Print the output
def formAnInequality ( integers , inequalities ) : NEW_LINE INDENT integers . sort ( ) NEW_LINE lowerIndex = 0 NEW_LINE higherIndex = len ( integers ) - 1 NEW_LINE sb = " " NEW_LINE for ch in inequalities : NEW_LINE INDENT if ( ch == ' < ' ) : NEW_LINE INDENT sb += ( " ▁ " + chr ( integers [ lowerIndex ] ) + " ▁ " + ch ) NEW_LINE lowerIndex += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sb += ( " ▁ " + chr ( integers [ higherIndex ] ) + " ▁ " + ch ) NEW_LINE higherIndex -= 1 NEW_LINE DEDENT DEDENT sb += ( " ▁ " + chr ( integers [ lowerIndex ] ) ) NEW_LINE return sb NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT integers = [ 2 , 5 , 1 , 0 ] NEW_LINE inequalities = [ ' < ' , ' > ' , ' < ' ] NEW_LINE output = formAnInequality ( integers , inequalities ) NEW_LINE print ( output ) NEW_LINE DEDENT
Check if two arrays can be made equal by reversing subarrays multiple times | Function to check if array B can be made equal to array A ; Sort both the arrays ; Check if both the arrays are equal or not ; Driver Code
def canMadeEqual ( A , B , n ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 ] NEW_LINE B = [ 1 , 3 , 2 ] NEW_LINE n = len ( A ) NEW_LINE if ( canMadeEqual ( A , B , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT