codeparrot/xlcost-text-to-code huggingface dataset:
Maximize cost to empty given array by repetitively removing K array elements | Function to find the maximum cost to remove all array elements ; Stores maximum cost to remove array elements ; Sort array in ascending order ; Traverse the array ; Update maxCost ; Driver Code
def maxCostToRemove ( arr , N , K ) : NEW_LINE INDENT maxCost = 0 NEW_LINE arr = sorted ( arr ) NEW_LINE for i in range ( 0 , N , K ) : NEW_LINE INDENT maxCost += arr [ i + 1 ] NEW_LINE DEDENT return maxCost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 4 NEW_LINE print ( maxCostToRemove ( arr , N , K ) ) NEW_LINE DEDENT
Minimize cost to convert all array elements to 0 | Python3 program for the above approach ; Function to calculate the minimum cost of converting all array elements to 0 s ; Stores subarrays of 0 s only ; Traverse the array ; If consecutive 0 s occur ; Increment if needed ; Push the current length of consecutive 0 s in a vector ; Update lengths as 0 ; Sorting vector ; Stores the number of subarrays consisting of 1 s ; Traverse the array ; If current element is 1 ; Otherwise ; Increment count of consecutive ones ; Stores the minimum cost ; Traverse the array ; First element ; Traverse the subarray sizes ; Update cost ; Cost of performing X and Y operations ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
import sys NEW_LINE def minimumCost ( binary , n , a , b ) : NEW_LINE INDENT groupOfZeros = [ ] NEW_LINE length = 0 NEW_LINE i = 0 NEW_LINE increment_need = True NEW_LINE while ( i < n ) : NEW_LINE INDENT increment_need = True NEW_LINE while ( i < n and binary [ i ] == 0 ) : NEW_LINE INDENT length += 1 NEW_LINE i += 1 NEW_LINE increment_need = False NEW_LINE DEDENT if ( increment_need == True ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( length != 0 ) : NEW_LINE INDENT groupOfZeros . append ( length ) NEW_LINE DEDENT length = 0 NEW_LINE DEDENT groupOfZeros . sort ( ) NEW_LINE i = 0 NEW_LINE found_ones = False NEW_LINE NumOfOnes = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT found_ones = False NEW_LINE while ( i < n and binary [ i ] == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE found_ones = True NEW_LINE DEDENT if ( found_ones == False ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT NumOfOnes += 1 NEW_LINE DEDENT DEDENT ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr = 0 NEW_LINE totalOnes = NumOfOnes NEW_LINE if ( i == 0 ) : NEW_LINE INDENT curr = totalOnes * a NEW_LINE DEDENT else : NEW_LINE INDENT mark = i NEW_LINE num_of_changes = 0 NEW_LINE for x in groupOfZeros : NEW_LINE INDENT if ( mark >= x ) : NEW_LINE INDENT totalOnes -= 1 NEW_LINE mark -= x NEW_LINE num_of_changes += x NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT curr = ( ( num_of_changes * b ) + ( totalOnes * a ) ) NEW_LINE DEDENT ans = min ( ans , curr ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE X = 10 NEW_LINE Y = 4 NEW_LINE minimumCost ( arr , N , X , Y ) NEW_LINE DEDENT
Reduce array to longest sorted array possible by removing either half of given array in each operation | Function to check if the subarray arr [ i . . j ] is a sorted subarray or not ; Traverse all elements of the subarray arr [ i ... j ] ; If the previous element of the subarray exceeds current element ; Return 0 ; Return 1 ; Recursively partition the array into two equal halves ; If atmost one element is left in the subarray arr [ i . . j ] ; Checks if subarray arr [ i . . j ] is a sorted subarray or not ; If the subarray arr [ i ... j ] is a sorted subarray ; Stores middle element of the subarray arr [ i . . j ] ; Recursively partition the current subarray arr [ i . . j ] into equal halves ; Driver Code
def isSortedparitions ( arr , i , j ) : NEW_LINE INDENT for k in range ( i + 1 , j + 1 ) : NEW_LINE INDENT if ( arr [ k ] < arr [ k - 1 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def partitionsArr ( arr , i , j ) : NEW_LINE INDENT if ( i >= j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT flag = int ( isSortedparitions ( arr , i , j ) ) NEW_LINE if ( flag != 0 ) : NEW_LINE INDENT return ( j - i + 1 ) NEW_LINE DEDENT mid = ( i + j ) // 2 NEW_LINE X = partitionsArr ( arr , i , mid ) ; NEW_LINE Y = partitionsArr ( arr , mid + 1 , j ) NEW_LINE return max ( X , Y ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( partitionsArr ( arr , 0 , N - 1 ) ) NEW_LINE DEDENT
Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array | Function to check if it is possible to merge the two given arrays with given conditions ; Stores index of the array A [ ] ; Stores index of the array B [ ] ; Check if the previous element are from the array A [ ] or from the array B [ ] ; Check if it is possible to merge the two given sorted arrays with given conditions ; Traverse both the arrays ; If A [ i ] is less than B [ j ] and previous element are not from A [ ] ; Update prev ; Update i ; If B [ j ] is less than A [ i ] and previous element are not from B [ ] ; Update prev ; Update j ; If A [ i ] equal to B [ j ] ; If previous element are not from B [ ] ; Update prev ; Update j ; If previous element is not from A [ ] ; Update prev ; Update i ; If it is not possible to merge two sorted arrays with given conditions ; Update flag ; Driver Code
def checkIfPossibleMerge ( A , B , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE prev = - 1 NEW_LINE flag = 1 NEW_LINE while ( i < N and j < N ) : NEW_LINE INDENT if ( A [ i ] < B [ j ] and prev != 0 ) : NEW_LINE INDENT prev = 0 NEW_LINE i += 1 NEW_LINE DEDENT elif ( B [ j ] < A [ i ] and prev != 1 ) : NEW_LINE INDENT prev = 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( A [ i ] == B [ j ] ) : NEW_LINE INDENT if ( prev != 1 ) : NEW_LINE INDENT prev = 1 NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prev = 0 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT return flag NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 5 , 8 ] NEW_LINE B = [ 2 , 4 , 6 ] NEW_LINE N = len ( A ) NEW_LINE if ( checkIfPossibleMerge ( A , B , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Perform K operations ; Stores smallest element in the array ; Stores index of the smallest array element ; Stores largest element in the array ; Stores index of the largest array element ; Traverse the array elements ; If current element exceeds largest_element ; Update the largest element ; Update index of the largest array element ; If current element is smaller than smallest_element ; Update the smallest element ; Update index of the smallest array element ; Stores the value of smallest element by given operations ; Stores the value of largest element by given operations ; If the value of a + b less than the sum of smallest and largest element of the array ; Update smallest element of the array ; Update largest element of the array ; Stores sum of elements of the array by given operations ; Traverse the array ; Update ans ; Driver Code
def minimum_possible_sum ( arr , n , k ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT smallest_element = arr [ 0 ] NEW_LINE smallest_pos = 0 NEW_LINE largest_element = arr [ 0 ] NEW_LINE largest_pos = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] >= largest_element ) : NEW_LINE INDENT largest_element = arr [ i ] NEW_LINE largest_pos = i NEW_LINE DEDENT if ( arr [ i ] < smallest_element ) : NEW_LINE INDENT smallest_element = arr [ i ] NEW_LINE smallest_pos = i NEW_LINE DEDENT DEDENT a = smallest_element * 2 NEW_LINE b = largest_element // 2 NEW_LINE if ( a + b < smallest_element + largest_element ) : NEW_LINE INDENT arr [ smallest_pos ] = a NEW_LINE arr [ largest_pos ] = b NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 50 , 1 , 100 , 100 , 1 ] NEW_LINE K = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( minimum_possible_sum ( arr , n , K ) ) NEW_LINE DEDENT
Rearrange array elements excluded by given ranges to maximize sum of subarrays starting from the first index | Function that finds the maximum sum all subarrays from the starting index after rearranging the array ; Stores elements after rearranging ; Keeps track of visited elements ; Traverse the queries ; Mark elements that are not allowed to rearranged ; Stores the indices ; Get indices and elements that are allowed to rearranged ; Store the current index and element ; Sort vector v in descending order ; Insert elements in array ; Stores the resultant sum ; Stores the prefix sums ; Traverse the given array ; Return the maximum sum ; Driver Code ; Given array ; Given size ; Queries ; Number of queries ; Function Call
def maxSum ( n , a , l , q ) : NEW_LINE INDENT v = [ ] NEW_LINE d = [ 0 ] * n NEW_LINE for i in range ( q ) : NEW_LINE INDENT for x in range ( l [ i ] [ 0 ] , l [ i ] [ 1 ] + 1 ) : NEW_LINE INDENT if ( d [ x ] == 0 ) : NEW_LINE INDENT d [ x ] = 1 NEW_LINE DEDENT DEDENT DEDENT st = set ( [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( d [ i ] == 0 ) : NEW_LINE INDENT v . append ( a [ i ] ) NEW_LINE st . add ( i ) NEW_LINE DEDENT DEDENT v . sort ( reverse = True ) NEW_LINE c = 0 NEW_LINE for it in st : NEW_LINE INDENT a [ it ] = v NEW_LINE c += 1 NEW_LINE DEDENT pref_sum = 0 NEW_LINE temp_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp_sum += a [ i ] NEW_LINE pref_sum += temp_sum NEW_LINE DEDENT return pref_sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE q = [ [ 0 , 0 ] , [ 4 , 5 ] ] NEW_LINE queries = len ( q ) NEW_LINE print ( maxSum ( N , arr , q , queries ) ) NEW_LINE DEDENT
Count pairs with Bitwise XOR greater than both the elements of the pair | Python3 program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call
import math NEW_LINE def countPairs ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE A . sort ( ) NEW_LINE bits = [ 0 ] * 32 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( ( 1 << j ) & A [ i ] ) == 0 ) : NEW_LINE INDENT count += bits [ j ] NEW_LINE DEDENT DEDENT bits [ ( int ) ( math . log ( A [ i ] , 2 ) ) ] += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Maximum occurred integers after M circular operations in given range | Function to find the maximum occurred integer after completing all circular operations ; Stores the highest visit count for any element ; Stors the number of times an element is visited ; Iterate over the array ; Iterate over the range circularly form start to end ; Count number of times an element is visited ; Increment frequency of N ; Update maxVisited ; Increment frequency of start ; Update maxVisited ; Increment the start ; Increment the count for the last visited element ; Print most visited elements ; Driver Code ; Function Call
def mostvisitedsector ( N , A ) : NEW_LINE INDENT maxVisited = 0 NEW_LINE mp = { } NEW_LINE for i in range ( 0 , len ( A ) - 1 ) : NEW_LINE INDENT start = A [ i ] % N NEW_LINE end = A [ i + 1 ] % N NEW_LINE while ( start != end ) : NEW_LINE INDENT if ( start == 0 ) : NEW_LINE INDENT if N in mp : NEW_LINE INDENT mp [ N ] = mp [ N ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ N ] = 1 NEW_LINE DEDENT if ( mp [ N ] > maxVisited ) : NEW_LINE INDENT maxVisited = mp [ N ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if start in mp : NEW_LINE INDENT mp [ start ] = mp [ start ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ start ] = 1 NEW_LINE DEDENT if ( mp [ start ] > maxVisited ) : NEW_LINE INDENT maxVisited = mp [ start ] NEW_LINE DEDENT DEDENT start = ( start + 1 ) % N NEW_LINE DEDENT DEDENT if A [ - 1 ] in mp : NEW_LINE INDENT mp [ A [ - 1 ] ] = mp [ A [ - 1 ] ] + 1 NEW_LINE DEDENT if ( mp [ A [ - 1 ] ] > maxVisited ) : NEW_LINE INDENT maxVisited = mp [ A [ - 1 ] ] NEW_LINE DEDENT for x in mp : NEW_LINE INDENT if ( mp [ x ] == maxVisited ) : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE arr = [ 1 , 2 , 1 , 2 ] NEW_LINE mostvisitedsector ( N , arr ) NEW_LINE DEDENT
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points | Function to return the area of the largest rectangle formed by lines parallel to X and Y axis from given set of points ; Initialize two arrays ; Store x and y coordinates ; Sort arrays ; Initialize max differences ; Find max adjacent differences ; Return answer ; Driver Code ; Given points ; Total points ; Function call
def maxRectangle ( sequence , size ) : NEW_LINE INDENT X_Cord = [ 0 ] * size NEW_LINE Y_Cord = [ 0 ] * size NEW_LINE for i in range ( size ) : NEW_LINE INDENT X_Cord [ i ] = sequence [ i ] [ 0 ] NEW_LINE Y_Cord [ i ] = sequence [ i ] [ 1 ] NEW_LINE DEDENT X_Cord . sort ( ) NEW_LINE Y_Cord . sort ( ) NEW_LINE X_Max = 0 NEW_LINE Y_Max = 0 NEW_LINE for i in range ( size - 1 ) : NEW_LINE INDENT X_Max = max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] ) NEW_LINE Y_Max = max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] ) NEW_LINE DEDENT return X_Max * Y_Max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT point = [ [ - 2 , 0 ] , [ 2 , 0 ] , [ 4 , 0 ] , [ 4 , 2 ] ] NEW_LINE n = len ( point ) NEW_LINE print ( maxRectangle ( point , n ) ) NEW_LINE DEDENT
Rearrange two given arrays such that sum of same indexed elements lies within given range | Function to check if there exists any arrangements of the arrays such that sum of element lie in the range [ K / 2 , K ] ; Sort the array arr1 [ ] in increasing order ; Sort the array arr2 [ ] in decreasing order ; Traverse the array ; If condition is not satisfied break the loop ; Print the result ; Driver Code
def checkArrangement ( A1 , A2 , n , k ) : NEW_LINE INDENT A1 = sorted ( A1 ) NEW_LINE A2 = sorted ( A2 ) NEW_LINE A2 = A2 [ : : - 1 ] NEW_LINE flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( A1 [ i ] + A2 [ i ] > k ) or ( A1 [ i ] + A2 [ i ] < k // 2 ) ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 3 , 4 , 5 ] NEW_LINE arr2 = [ 2 , 0 , 1 , 1 ] NEW_LINE K = 6 NEW_LINE N = len ( arr1 ) NEW_LINE checkArrangement ( arr1 , arr2 , N , K ) NEW_LINE DEDENT
Rearrange two given arrays to maximize sum of same indexed elements | Function to find the maximum possible sum by rearranging the given array elements ; Sort the array A [ ] in ascending order ; Sort the array B [ ] in descending order ; Stores maximum possible sum by rearranging array elements ; Traverse both the arrays ; Update maxSum ; Driver Code
def MaxRearrngeSum ( A , B , N ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( reverse = True ) NEW_LINE maxSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT maxSum += abs ( A [ i ] - B [ i ] ) NEW_LINE DEDENT return maxSum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 2 , 4 , 5 ] NEW_LINE B = [ 5 , 5 , 5 , 6 , 6 ] NEW_LINE N = len ( A ) NEW_LINE print ( MaxRearrngeSum ( A , B , N ) ) NEW_LINE DEDENT
Area of the largest rectangle possible from given coordinates | Function to find the maximum possible area of a rectangle ; Initialize variables ; Sort array arr1 [ ] ; Sort array arr2 [ ] ; Traverse arr1 [ ] and arr2 [ ] ; If arr1 [ i ] is same as arr2 [ j ] ; If no starting point is found yet ; Update maximum end ; If arr [ i ] > arr2 [ j ] ; If no rectangle is found ; Return the area ; Driver Code ; Given point ; Given length ; Given points ; Given length ; Function Call
def largestArea ( arr1 , n , arr2 , m ) : NEW_LINE INDENT end = 0 NEW_LINE start = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE arr1 . sort ( reverse = False ) NEW_LINE arr2 . sort ( reverse = False ) NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( arr1 [ i ] == arr2 [ j ] ) : NEW_LINE INDENT if ( start == 0 ) : NEW_LINE INDENT start = arr1 [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT end = arr1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( arr1 [ i ] > arr2 [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT if ( end == 0 or start == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( end - start ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 4 ] NEW_LINE N = len ( arr1 ) NEW_LINE arr2 = [ 1 , 3 , 4 ] NEW_LINE M = len ( arr2 ) NEW_LINE print ( largestArea ( arr1 , N , arr2 , M ) ) NEW_LINE DEDENT
Count pairs from two arrays with difference exceeding K | Function to count pairs that satisfy the given conditions ; Stores index of the left pointer . ; Stores index of the right pointer ; Stores count of total pairs that satisfy the conditions ; Sort arr [ ] array ; Sort brr [ ] array ; Traverse both the array and count then pairs ; If the value of ( brr [ j ] - arr [ i ] ) exceeds K ; Update cntPairs ; Update ; Update j ; Driver Code
def count_pairs ( arr , brr , N , M , K ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE cntPairs = 0 NEW_LINE arr = sorted ( arr ) NEW_LINE brr = sorted ( brr ) NEW_LINE while ( i < N and j < M ) : NEW_LINE INDENT if ( brr [ j ] - arr [ i ] > K ) : NEW_LINE INDENT cntPairs += ( M - j ) NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT return cntPairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 9 , 1 , 8 ] NEW_LINE brr = [ 10 , 12 , 7 , 4 , 2 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE print ( count_pairs ( arr , brr , N , M , K ) ) NEW_LINE DEDENT
Merge two sorted arrays in O ( 1 ) extra space using Heap | Function to perform min heapify on array brr [ ] ; Stores index of left child of i . ; Stores index of right child of i . ; Stores index of the smallest element in ( arr [ i ] , arr [ left ] , arr [ right ] ) ; Check if arr [ left ] less than arr [ smallest ] ; Update smallest ; Check if arr [ right ] less than arr [ smallest ] ; Update smallest ; If i is not the index of smallest element ; Swap arr [ i ] and arr [ smallest ] ; Perform heapify on smallest ; Function to merge two sorted arrays ; Traverse the array arr [ ] ; Check if current element is less than brr [ 0 ] ; Swap arr [ i ] and brr [ 0 ] ; Perform heapify on brr [ ] ; Sort array brr [ ] ; Function to print array elements ; Traverse array arr [ ] ; Driver code ; Function call to merge two array ; Print first array ; Print Second array .
def minHeapify ( brr , i , M ) : NEW_LINE INDENT left = 2 * i + 1 NEW_LINE right = 2 * i + 2 NEW_LINE smallest = i NEW_LINE if ( left < M and brr [ left ] < brr [ smallest ] ) : NEW_LINE INDENT smallest = left NEW_LINE DEDENT if ( right < M and brr [ right ] < brr [ smallest ] ) : NEW_LINE INDENT smallest = right NEW_LINE DEDENT if ( smallest != i ) : NEW_LINE INDENT brr [ i ] , brr [ smallest ] = brr [ smallest ] , brr [ i ] NEW_LINE minHeapify ( brr , smallest , M ) NEW_LINE DEDENT DEDENT def merge ( arr , brr , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > brr [ 0 ] ) : NEW_LINE INDENT arr [ i ] , brr [ 0 ] = brr [ 0 ] , arr [ i ] NEW_LINE minHeapify ( brr , 0 , M ) NEW_LINE DEDENT DEDENT brr . sort ( ) NEW_LINE DEDENT def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 23 , 35 , 235 , 2335 ] NEW_LINE brr = [ 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE merge ( arr , brr , N , M ) NEW_LINE printArray ( arr , N ) NEW_LINE printArray ( brr , M ) NEW_LINE DEDENT
Merge two sorted arrays in O ( 1 ) extra space using QuickSort partition | Function to perform the partition around the pivot element ; Stores index of each element of the array , arr [ ] ; Stores index of each element of the array , brr [ ] ; Traverse both the array ; If pivot is smaller than arr [ l ] ; If Pivot is greater than brr [ r ] ; If either arr [ l ] > Pivot or brr [ r ] < Pivot ; Function to merge the two sorted array ; Stores index of each element of the array arr [ ] ; Stores index of each element of the array brr [ ] ; Stores index of each element the final sorted array ; Stores the pivot element ; Traverse both the array ; If pivot element is not found or index < N ; If pivot element is not found or index < N ; Place the first N elements of the sorted array into arr [ ] and the last M elements of the sorted array into brr [ ] ; Sort both the arrays ; Print the first N elements in sorted order ; Print the last M elements in sorted order ; Driver Code
def partition ( arr , N , brr , M , Pivot ) : NEW_LINE INDENT l = N - 1 NEW_LINE r = 0 NEW_LINE while ( l >= 0 and r < M ) : NEW_LINE INDENT if ( arr [ l ] < Pivot ) : NEW_LINE INDENT l -= 1 NEW_LINE DEDENT elif ( brr [ r ] > Pivot ) : NEW_LINE INDENT r += 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ l ] , brr [ r ] = brr [ r ] , arr [ l ] NEW_LINE l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT DEDENT DEDENT def Merge ( arr , N , brr , M ) : NEW_LINE INDENT l = 0 NEW_LINE r = 0 NEW_LINE index = - 1 NEW_LINE Pivot = 0 NEW_LINE while ( index < N and l < N and r < M ) : NEW_LINE INDENT if ( arr [ l ] < brr [ r ] ) : NEW_LINE INDENT Pivot = arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Pivot = brr [ r ] NEW_LINE r += 1 NEW_LINE DEDENT index += 1 NEW_LINE DEDENT while ( index < N and l < N ) : NEW_LINE INDENT Pivot = arr [ l ] NEW_LINE l += 1 NEW_LINE index += 1 NEW_LINE DEDENT while ( index < N and r < M ) : NEW_LINE INDENT Pivot = brr [ r ] NEW_LINE r += 1 NEW_LINE index += 1 NEW_LINE DEDENT partition ( arr , N , brr , M , Pivot ) NEW_LINE arr = sorted ( arr ) NEW_LINE brr = sorted ( brr ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT print ( brr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 9 ] NEW_LINE brr = [ 2 , 4 , 7 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE Merge ( arr , N , brr , M ) NEW_LINE DEDENT