codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the size of array by deleting exactly k sub | Python implementation of above approach ; Sieve of Eratosthenes ; Function to return the size of the maximized array ; Insert the indices of composite numbers ; Compute the number of prime between two consecutive composite ; Sort the diff vector ; Compute the prefix sum of diff vector ; Impossible case ; Delete sub - arrays of length 1 ; Find the number of primes to be deleted when deleting the sub - arrays ; Driver code
N = 10000005 NEW_LINE prime = [ False ] * N NEW_LINE def seive ( ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE INDENT if not prime [ i ] : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT prime [ j ] = True NEW_LINE DEDENT DEDENT DEDENT prime [ 1 ] = True NEW_LINE DEDENT def maxSizeArr ( arr , n , k ) : NEW_LINE INDENT v , diff = [ ] , [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT diff . append ( v [ i ] - v [ i - 1 ] - 1 ) NEW_LINE DEDENT diff . sort ( ) NEW_LINE for i in range ( 1 , len ( diff ) ) : NEW_LINE INDENT diff [ i ] += diff [ i - 1 ] NEW_LINE DEDENT if k > n or ( k == 0 and len ( v ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif len ( v ) <= k : NEW_LINE INDENT return ( n - k ) NEW_LINE DEDENT elif len ( v ) > k : NEW_LINE INDENT tt = len ( v ) - k NEW_LINE s = 0 NEW_LINE s += diff [ tt - 1 ] NEW_LINE res = n - ( len ( v ) + s ) NEW_LINE return res NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT seive ( ) NEW_LINE arr = [ 2 , 4 , 2 , 2 , 4 , 2 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( maxSizeArr ( arr , n , k ) ) NEW_LINE DEDENT
Sort an array according to count of set bits | Set 2 | function to sort the array according to the number of set bits in elements ; Counting no of setBits in arr [ i ] ; The count is subtracted from 32 because the result needs to be in descending order ; Printing the numbers in descending order of set bit count ; Driver code
def sortArr ( arr , n ) : NEW_LINE INDENT mp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE k = arr [ i ] NEW_LINE while ( k ) : NEW_LINE INDENT k = k & k - 1 NEW_LINE count += 1 NEW_LINE DEDENT mp . append ( ( 32 - count , arr [ i ] ) ) NEW_LINE DEDENT mp . sort ( key = lambda x : x [ 0 ] ) NEW_LINE for it in mp : NEW_LINE INDENT print ( it [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 2 , 3 , 9 , 4 , 6 , 7 , 15 , 32 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT
Smallest multiple of N formed using the given set of digits | Function to return the required number ; Initialize both vectors with their initial values ; Sort the vector of digits ; Initialize the queue ; If modulus value is not present in the queue ; Compute digits modulus given number and update the queue and vectors ; While queue is not empty ; Remove the first element of the queue ; Compute new modulus values by using old queue values and each digit of the set ; If value is not present in the queue ; If required condition can 't be satisfied  ; Initialize new vector ; Constructing the solution by backtracking ; Reverse the vector ; Return the required number ; Driver code
def findSmallestNumber ( arr , n ) : NEW_LINE INDENT dp = [ float ( ' inf ' ) ] * n NEW_LINE result = [ ( - 1 , 0 ) ] * n NEW_LINE arr . sort ( ) NEW_LINE q = [ ] NEW_LINE for i in arr : NEW_LINE INDENT if i != 0 : NEW_LINE INDENT if dp [ i % n ] > 10 ** 9 : NEW_LINE INDENT q . append ( i % n ) NEW_LINE dp [ i % n ] = 1 NEW_LINE result [ i % n ] = - 1 , i NEW_LINE DEDENT DEDENT DEDENT while len ( q ) > 0 : NEW_LINE INDENT u = q . pop ( 0 ) NEW_LINE for i in arr : NEW_LINE INDENT v = ( u * 10 + i ) % n NEW_LINE if dp [ u ] + 1 < dp [ v ] : NEW_LINE INDENT dp [ v ] = dp [ u ] + 1 NEW_LINE q . append ( v ) NEW_LINE result [ v ] = u , i NEW_LINE DEDENT DEDENT DEDENT if dp [ 0 ] > 10 ** 9 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = [ ] NEW_LINE u = 0 NEW_LINE while u != - 1 : NEW_LINE INDENT ans . append ( result [ u ] [ 1 ] ) NEW_LINE u = result [ u ] [ 0 ] NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE for i in ans : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 2 , 3 ] NEW_LINE n = 12 NEW_LINE print ( findSmallestNumber ( arr , n ) ) NEW_LINE DEDENT
Count number of subsets whose median is also present in the same subset | Python 3 implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return ' a ' raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
mod = 1000000007 NEW_LINE def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return int ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def powmod ( a , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT pt = powmod ( a , int ( n / 2 ) ) NEW_LINE pt = ( pt * pt ) % mod NEW_LINE if ( n % 2 ) : NEW_LINE INDENT return ( pt * a ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT return pt NEW_LINE DEDENT DEDENT def CountSubset ( arr , n ) : NEW_LINE INDENT ans = powmod ( 2 , n - 1 ) NEW_LINE arr . sort ( reverse = False ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = i + 1 NEW_LINE while ( j < n and arr [ j ] == arr [ i ] ) : NEW_LINE INDENT r = n - 1 - j NEW_LINE l = i NEW_LINE ans = ( ans + nCr ( l + r , l ) ) % mod NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CountSubset ( arr , n ) ) NEW_LINE DEDENT
Count number of subsets whose median is also present in the same subset | Python3 implementation of the approach ; Function to store pascal triangle in 2 - d array ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
mod = 1000000007 NEW_LINE arr = [ [ None for i in range ( 1001 ) ] for j in range ( 1001 ) ] NEW_LINE def Preprocess ( ) : NEW_LINE INDENT arr [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , 1001 ) : NEW_LINE INDENT arr [ i ] [ 0 ] = 1 NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT arr [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ) % mod NEW_LINE DEDENT arr [ i ] [ i ] = 1 NEW_LINE DEDENT DEDENT def powmod ( a , n ) : NEW_LINE INDENT if not n : NEW_LINE INDENT return 1 NEW_LINE DEDENT pt = powmod ( a , n // 2 ) NEW_LINE pt = ( pt * pt ) % mod NEW_LINE if n % 2 : NEW_LINE INDENT return ( pt * a ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT return pt NEW_LINE DEDENT DEDENT def CountSubset ( val , n ) : NEW_LINE INDENT ans = powmod ( 2 , n - 1 ) NEW_LINE val . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < n and val [ j ] == val [ i ] : NEW_LINE INDENT r = n - 1 - j NEW_LINE l = i NEW_LINE ans = ( ans + arr [ l + r ] [ l ] ) % mod NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Preprocess ( ) NEW_LINE val = [ 2 , 3 , 2 ] NEW_LINE n = len ( val ) NEW_LINE print ( CountSubset ( val , n ) ) NEW_LINE DEDENT
Reorder the position of the words in alphabetical order | Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
def reArrange ( words , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ words [ i ] ] = i + 1 NEW_LINE DEDENT words . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mp [ words [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT words = [ " live " , " place " , " travel " , " word " , " sky " ] NEW_LINE n = len ( words ) NEW_LINE reArrange ( words , n ) ; NEW_LINE
Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | Python 3 implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code
MAX = 100000 NEW_LINE def findSumofEle ( arr1 , m , arr2 , n ) : NEW_LINE INDENT hash = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ arr2 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , MAX , 1 ) : NEW_LINE INDENT hash [ i ] = hash [ i ] + hash [ i - 1 ] NEW_LINE DEDENT maximumFreq = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT maximumFreq = max ( maximumFreq , hash [ arr1 [ i ] ] ) NEW_LINE DEDENT sumOfElements = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( maximumFreq == hash [ arr1 [ i ] ] ) : NEW_LINE INDENT sumOfElements += arr1 [ i ] NEW_LINE DEDENT DEDENT return sumOfElements NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 2 , 5 , 6 , 8 ] NEW_LINE arr2 = [ 4 , 10 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE print ( findSumofEle ( arr1 , m , arr2 , n ) ) NEW_LINE DEDENT
Print 2 | Function to print the coordinates along with their frequency in ascending order ; map to store the pairs and their frequencies ; Store the coordinates along with their frequencies ; Driver code
def Print ( x , y , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ ( x [ i ] , y [ i ] ) ] = m . get ( ( x [ i ] , y [ i ] ) , 0 ) + 1 NEW_LINE DEDENT e = sorted ( m ) NEW_LINE for i in e : NEW_LINE INDENT print ( i [ 0 ] , i [ 1 ] , m [ i ] ) NEW_LINE DEDENT DEDENT x = [ 1 , 2 , 1 , 1 , 1 ] NEW_LINE y = [ 1 , 1 , 3 , 1 , 3 ] NEW_LINE n = len ( x ) NEW_LINE Print ( x , y , n ) NEW_LINE
Split the array elements into strictly increasing and decreasing sequence | Function to print both the arrays ; Store both arrays ; Used for hashing ; Iterate for every element ; Increase the count ; If first occurrence ; If second occurrence ; If occurs more than 2 times ; Sort in increasing order ; Print the increasing array ; Sort in reverse order ; Print the decreasing array ; Driver code
def PrintBothArrays ( a , n ) : NEW_LINE INDENT v1 , v2 = [ ] , [ ] ; NEW_LINE mpp = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mpp [ a [ i ] ] += 1 ; NEW_LINE if ( mpp [ a [ i ] ] == 1 ) : NEW_LINE INDENT v1 . append ( a [ i ] ) ; NEW_LINE DEDENT elif ( mpp [ a [ i ] ] == 2 ) : NEW_LINE INDENT v2 . append ( a [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT v1 . sort ( ) ; NEW_LINE print ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; NEW_LINE for it in v1 : NEW_LINE INDENT print ( it , end = " ▁ " ) ; NEW_LINE DEDENT v2 . sort ( reverse = True ) ; NEW_LINE print ( " Strictly decreasing array is : " ) ; NEW_LINE for it in v2 : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 7 , 2 , 7 , 3 , 3 , 1 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE PrintBothArrays ( a , n ) ; NEW_LINE DEDENT
Iterative selection sort for linked list |  ; Traverse the List ; Traverse the unsorted sublist ; Swap Data
def selectionSort ( head ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp ) : NEW_LINE INDENT minn = temp NEW_LINE r = temp . next NEW_LINE while ( r ) : NEW_LINE INDENT if ( minn . data > r . data ) : NEW_LINE INDENT minn = r NEW_LINE DEDENT r = r . next NEW_LINE DEDENT x = temp . data NEW_LINE temp . data = minn . data NEW_LINE minn . data = x NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT
Sort ugly numbers in an array at their relative positions | Function that returns true if n is an ugly number ; While divisible by 2 , keep dividing ; While divisible by 3 , keep dividing ; While divisible by 5 , keep dividing ; n must be 1 if it was ugly ; Function to sort ugly numbers in their relative positions ; To store the ugly numbers from the array ; If current element is an ugly number ; Add it to the ArrayList and set arr [ i ] to - 1 ; Sort the ugly numbers ; Position of an ugly number ; Driver code
def isUgly ( n ) : NEW_LINE INDENT while n % 2 == 0 : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT while n % 3 == 0 : NEW_LINE INDENT n = n // 3 NEW_LINE DEDENT while n % 5 == 0 : NEW_LINE INDENT n = n // 5 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def sortUglyNumbers ( arr , n ) : NEW_LINE INDENT list = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isUgly ( arr [ i ] ) : NEW_LINE INDENT list . append ( arr [ i ] ) NEW_LINE arr [ i ] = - 1 NEW_LINE DEDENT DEDENT list . sort ( ) NEW_LINE j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] == - 1 : NEW_LINE INDENT print ( list [ j ] , end = " ▁ " ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 7 , 12 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE sortUglyNumbers ( arr , n ) NEW_LINE DEDENT
Sort elements of the array that occurs in between multiples of K | Utility function to print the contents of an array ; Function to sort elements in between multiples of k ; To store the index of previous multiple of k ; If it is not the first multiple of k ; Sort the elements in between the previous and the current multiple of k ; Update previous to be current ; Print the updated array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def sortArr ( arr , n , k ) : NEW_LINE INDENT prev = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT if ( prev != - 1 ) : NEW_LINE INDENT temp = arr [ prev + 1 : i ] ; NEW_LINE temp . sort ( ) ; NEW_LINE arr = arr [ : prev + 1 ] + temp + arr [ i : ] ; NEW_LINE DEDENT prev = i ; NEW_LINE DEDENT DEDENT printArr ( arr , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 13 , 3 , 7 , 8 , 21 , 13 , 12 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE sortArr ( arr , n , k ) ; NEW_LINE DEDENT
Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
def printNumbers ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE A , B = arr [ n - 1 ] , - 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if A % arr [ i ] != 0 : NEW_LINE INDENT B = arr [ i ] NEW_LINE break NEW_LINE DEDENT if i - 1 >= 0 and arr [ i ] == arr [ i - 1 ] : NEW_LINE INDENT B = arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT print ( " A ▁ = " , A , " , ▁ B ▁ = " , B ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE printNumbers ( arr , n ) NEW_LINE DEDENT
Find the modified array after performing k operations of given type | Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def removeMin ( arr , n ) : NEW_LINE INDENT minVal = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT minVal = min ( minVal , arr [ i ] ) ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] - minVal ; NEW_LINE DEDENT DEDENT def removeFromMax ( arr , n ) : NEW_LINE INDENT maxVal = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT maxVal = max ( maxVal , arr [ i ] ) ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = maxVal - arr [ i ] ; NEW_LINE DEDENT DEDENT def modifyArray ( arr , n , k ) : NEW_LINE INDENT if ( k % 2 == 0 ) : NEW_LINE INDENT removeMin ( arr , n ) ; NEW_LINE DEDENT else : NEW_LINE INDENT removeFromMax ( arr , n ) ; NEW_LINE DEDENT printArray ( arr , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 8 , 12 , 16 ] ; NEW_LINE n = len ( arr ) NEW_LINE k = 2 ; NEW_LINE modifyArray ( arr , n , k ) ; NEW_LINE DEDENT
Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k | Function to return the maximum product value ; To store the product ; Sort the array ; Efficiently finding product including every element once ; Storing values in hash map ; Including the greater repeating values so that product can be maximized ; Driver code
def maxProd ( arr , n , k ) : NEW_LINE INDENT product = 1 ; NEW_LINE s = dict . fromkeys ( arr , 0 ) ; NEW_LINE arr . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ arr [ i ] ] == 0 ) : NEW_LINE INDENT product = product * arr [ i ] ; NEW_LINE DEDENT s [ arr [ i ] ] = s [ arr [ i ] ] + 1 ; NEW_LINE DEDENT j = n - 1 ; NEW_LINE while ( j >= 0 and k > 0 ) : NEW_LINE INDENT if ( ( k > ( s [ arr [ j ] ] - 1 ) ) and ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) : NEW_LINE INDENT product *= pow ( arr [ j ] , s [ arr [ j ] ] - 1 ) ; NEW_LINE k = k - s [ arr [ j ] ] + 1 ; NEW_LINE s [ arr [ j ] ] = 0 ; NEW_LINE DEDENT if ( k <= ( s [ arr [ j ] ] - 1 ) and ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) : NEW_LINE INDENT product *= pow ( arr [ j ] , k ) ; NEW_LINE break ; NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT return product ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( maxProd ( arr , n , k ) ) ; NEW_LINE DEDENT
Merge K sorted arrays of different sizes | ( Divide and Conquer Approach ) | Function to merge two arrays ; array to store the result after merging l and r ; variables to store the current pointers for l and r ; loop to merge l and r using two pointer ; Function to merge all the arrays ; 2D - array to store the results of a step temporarily ; Loop to make pairs of arrays and merge them ; To clear the data of previous steps ; Returning the required output array ; Driver Code ; Input arrays ; Merged sorted array
def mergeTwoArrays ( l , r ) : NEW_LINE INDENT ret = [ ] NEW_LINE l_in , r_in = 0 , 0 NEW_LINE while l_in + r_in < len ( l ) + len ( r ) : NEW_LINE INDENT if ( l_in != len ( l ) and ( r_in == len ( r ) or l [ l_in ] < r [ r_in ] ) ) : NEW_LINE INDENT ret . append ( l [ l_in ] ) NEW_LINE l_in += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ret . append ( r [ r_in ] ) NEW_LINE r_in += 1 NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT def mergeArrays ( arr ) : NEW_LINE INDENT arr_s = [ ] NEW_LINE while len ( arr ) != 1 : NEW_LINE INDENT arr_s [ : ] = [ ] NEW_LINE for i in range ( 0 , len ( arr ) , 2 ) : NEW_LINE INDENT if i == len ( arr ) - 1 : NEW_LINE INDENT arr_s . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT arr_s . append ( mergeTwoArrays ( arr [ i ] , arr [ i + 1 ] ) ) NEW_LINE DEDENT DEDENT arr = arr_s [ : ] NEW_LINE DEDENT return arr [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 3 , 13 ] , [ 8 , 10 , 11 ] , [ 9 , 15 ] ] NEW_LINE output = mergeArrays ( arr ) NEW_LINE for i in range ( 0 , len ( output ) ) : NEW_LINE INDENT print ( output [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Minimize the sum of the squares of the sum of elements of each group the array is divided into | Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
def findAnswer ( n , arr ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE sum = 0 NEW_LINE for i in range ( int ( n / 2 ) ) : NEW_LINE INDENT sum += ( ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 53 , 28 , 143 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findAnswer ( n , arr ) ) NEW_LINE DEDENT