codeparrot/xlcost-text-to-code huggingface dataset:
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Subtract 1 from every element so that the elements become in range from 0 to n - 1 ; Use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; To print counts , simply print the number of times n was added at index corresponding to every element ; Driver code
def printfrequency ( arr , n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT arr [ j ] = arr [ j ] - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ arr [ i ] % n ] = arr [ arr [ i ] % n ] + n NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( i + 1 , " - > " , arr [ i ] // n ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 3 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printfrequency ( arr , n ) NEW_LINE
Delete an element from array ( Using two traversals and one traversal ) | This function removes an element x from arr [ ] and returns new size after removal ( size is reduced only when x is present in arr [ ] ; Search x in array ; If x found in array ; reduce size of array and move all elements on space ahead ; Driver Code ; Delete x from arr [ ]
def deleteElement ( arr , n , x ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i < n ) : NEW_LINE INDENT n = n - 1 ; NEW_LINE for j in range ( i , n , 1 ) : NEW_LINE INDENT arr [ j ] = arr [ j + 1 ] NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE x = 6 NEW_LINE n = deleteElement ( arr , n , x ) NEW_LINE print ( " Modified ▁ array ▁ is " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Delete an element from array ( Using two traversals and one traversal ) | This function removes an element x from arr [ ] and returns new size after removal . Returned size is n - 1 when element is present . Otherwise 0 is returned to indicate failure . ; If x is last element , nothing to do ; Start from rightmost element and keep moving elements one position ahead . ; If element was not found ; Else move the next element in place of x ; Driver code ; Delete x from arr [ ]
def deleteElement ( arr , n , x ) : NEW_LINE INDENT if arr [ n - 1 ] == x : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT prev = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] != x : NEW_LINE INDENT curr = arr [ i ] NEW_LINE arr [ i ] = prev NEW_LINE prev = curr NEW_LINE DEDENT DEDENT if i < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT arr [ i ] = prev NEW_LINE return n - 1 NEW_LINE DEDENT arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE x = 6 NEW_LINE n = deleteElement ( arr , n , x ) NEW_LINE print ( " Modified ▁ array ▁ is " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Count Inversions of size three in a given array | Returns count of inversions of size 3 ; Initialize result ; Count all smaller elements on right of arr [ i ] ; Count all greater elements on left of arr [ i ] ; Update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; Driver program to test above function
def getInvCount ( arr , n ) : NEW_LINE INDENT invcount = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT small = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT small += 1 NEW_LINE DEDENT DEDENT great = 0 ; NEW_LINE for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] ) : NEW_LINE INDENT great += 1 NEW_LINE DEDENT DEDENT invcount += great * small NEW_LINE DEDENT return invcount NEW_LINE DEDENT arr = [ 8 , 4 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Inversion ▁ Count ▁ : " , getInvCount ( arr , n ) ) NEW_LINE
Trapping Rain Water | Function to return the maximum water that can be stored ; To store the maximum water that can be stored ; For every element of the array ; Find the maximum element on its left ; Find the maximum element on its right ; Update the maximum water ; Driver code
def maxWater ( arr , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT left = arr [ i ] ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT left = max ( left , arr [ j ] ) ; NEW_LINE DEDENT right = arr [ i ] ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT right = max ( right , arr [ j ] ) ; NEW_LINE DEDENT res = res + ( min ( left , right ) - arr [ i ] ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxWater ( arr , n ) ) ; NEW_LINE DEDENT
Trapping Rain Water | Python program to find maximum amount of water that can be trapped within given set of bars . ; left [ i ] contains height of tallest bar to the left of i 'th bar including itself ; Right [ i ] contains height of tallest bar to the right of ith bar including itself ; Initialize result ; Fill left array ; Fill right array ; Calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; Driver program
def findWater ( arr , n ) : NEW_LINE INDENT left = [ 0 ] * n NEW_LINE right = [ 0 ] * n NEW_LINE water = 0 NEW_LINE left [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT left [ i ] = max ( left [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT right [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT right [ i ] = max ( right [ i + 1 ] , arr [ i ] ) ; NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT water += min ( left [ i ] , right [ i ] ) - arr [ i ] NEW_LINE DEDENT return water NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is " , findWater ( arr , n ) ) NEW_LINE
Trapping Rain Water | Python program to find maximum amount of water that can be trapped within given set of bars . Space Complexity : O ( 1 ) ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; Driver program
def findWater ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE left_max = 0 NEW_LINE right_max = 0 NEW_LINE lo = 0 NEW_LINE hi = n - 1 NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT if ( arr [ lo ] < arr [ hi ] ) : NEW_LINE INDENT if ( arr [ lo ] > left_max ) : NEW_LINE INDENT left_max = arr [ lo ] NEW_LINE DEDENT else : NEW_LINE INDENT result += left_max - arr [ lo ] NEW_LINE DEDENT lo += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( arr [ hi ] > right_max ) : NEW_LINE INDENT right_max = arr [ hi ] NEW_LINE DEDENT else : NEW_LINE INDENT result += right_max - arr [ hi ] NEW_LINE DEDENT hi -= 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " , findWater ( arr , n ) ) NEW_LINE
Trapping Rain Water | Function to return the maximum water that can be stored ; Let the first element be stored as previous , we shall loop from index 1 ; To store previous wall 's index ; To store the water until a larger wall is found , if there are no larger walls then delete temp value from water ; If the current wall is taller than the previous wall then make current wall as the previous wall and its index as previous wall 's index  for the subsequent loops ; Because larger or same height wall is found ; Since current wall is shorter than the previous , we subtract previous wall ' s ▁ height ▁ from ▁ the ▁ current ▁ wall ' s height and add it to the water ; Store the same value in temp as well If we dont find any larger wall then we will subtract temp from water ; If the last wall was larger than or equal to the previous wall then prev_index would be equal to size of the array ( last element ) If we didn 't find a wall greater than or equal  to the previous wall from the left then  prev_index must be less than the index  of the last element ; Temp would ' ve ▁ stored ▁ the ▁ water ▁ collected ▁ ▁ from ▁ previous ▁ largest ▁ wall ▁ till ▁ the ▁ end ▁ ▁ of ▁ array ▁ if ▁ no ▁ larger ▁ wall ▁ was ▁ found ▁ then ▁ ▁ it ▁ has ▁ excess ▁ water ▁ and ▁ remove ▁ that ▁ ▁ from ▁ ' water ' var ; We start from the end of the array , so previous should be assigned to the last element ; Loop from the end of array up to the ' previous ▁ index ' which would contain the " largest ▁ wall ▁ from ▁ the ▁ left " ; Right end wall will be definitely smaller than the ' previous ▁ index ' wall ; Return the maximum water ; Driver code
def maxWater ( arr , n ) : NEW_LINE INDENT size = n - 1 NEW_LINE prev = arr [ 0 ] NEW_LINE prev_index = 0 NEW_LINE water = 0 NEW_LINE temp = 0 NEW_LINE for i in range ( 1 , size + 1 ) : NEW_LINE INDENT if ( arr [ i ] >= prev ) : NEW_LINE INDENT prev = arr [ i ] NEW_LINE prev_index = i NEW_LINE temp = 0 NEW_LINE DEDENT else : NEW_LINE INDENT water += prev - arr [ i ] NEW_LINE temp += prev - arr [ i ] NEW_LINE DEDENT DEDENT if ( prev_index < size ) : NEW_LINE INDENT water -= temp NEW_LINE prev = arr [ size ] NEW_LINE for i in range ( size , prev_index - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] >= prev ) : NEW_LINE INDENT prev = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT water += prev - arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return water NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxWater ( arr , n ) ) NEW_LINE
Trapping Rain Water | Function to return the maximum water that can be stored ; Stores the indices of the bars ; size of the array ; Stores the final result ; Loop through the each bar ; Remove bars from the stack until the condition holds ; store the height of the top and pop it . ; If the stack does not have any bars or the the popped bar has no left boundary ; Get the distance between the left and right boundary of popped bar ; Calculate the min . height ; If the stack is either empty or height of the current bar is less than or equal to the top bar of stack ; Driver code
def maxWater ( height ) : NEW_LINE INDENT stack = [ ] NEW_LINE n = len ( height ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( stack ) != 0 and ( height [ stack [ - 1 ] ] < height [ i ] ) ) : NEW_LINE INDENT pop_height = height [ stack [ - 1 ] ] NEW_LINE stack . pop ( ) NEW_LINE if ( len ( stack ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT distance = i - stack [ - 1 ] - 1 NEW_LINE min_height = min ( height [ stack [ - 1 ] ] , height [ i ] ) - pop_height NEW_LINE ans += distance * min_height NEW_LINE DEDENT stack . append ( i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE print ( maxWater ( arr ) ) NEW_LINE
Trapping Rain Water | Function to return the maximum water that can be stored ; indices to traverse the array ; To store Left max and right max for two pointers left and right ; To store the total amount of rain water trapped ; We need check for minimum of left and right max for each element ; Add the difference between current value and right max at index r ; Update right max ; Update right pointer ; Add the difference between current value and left max at index l ; Update left max ; Update left pointer ; Driver code
def maxWater ( arr , n ) : NEW_LINE INDENT left = 0 NEW_LINE right = n - 1 NEW_LINE l_max = 0 NEW_LINE r_max = 0 NEW_LINE result = 0 NEW_LINE while ( left <= right ) : NEW_LINE INDENT if r_max <= l_max : NEW_LINE INDENT result += max ( 0 , r_max - arr [ right ] ) NEW_LINE r_max = max ( r_max , arr [ right ] ) NEW_LINE right -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT result += max ( 0 , l_max - arr [ left ] ) NEW_LINE l_max = max ( l_max , arr [ left ] ) NEW_LINE left += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxWater ( arr , n ) ) NEW_LINE
Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | Function to find median of given two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . If j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; if i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an a [ ] in the first half is an empty set that , because we compare elements from these two groups . searching on left ; we have found the desired halves . ; this condition happens when we don 't have  any elements in the first half from a[] so  we returning the last element in b[] from  the first half. ; and this condition happens when we don 't  have any elements in the first half from  b[] so we returning the last element in  a[] from the first half. ; Function to find maximum ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
def findMedianSortedArrays ( a , n , b , m ) : NEW_LINE INDENT min_index = 0 NEW_LINE max_index = n NEW_LINE while ( min_index <= max_index ) : NEW_LINE INDENT i = ( min_index + max_index ) // 2 NEW_LINE j = ( ( n + m + 1 ) // 2 ) - i NEW_LINE if ( i < n and j > 0 and b [ j - 1 ] > a [ i ] ) : NEW_LINE INDENT min_index = i + 1 NEW_LINE DEDENT elif ( i > 0 and j < m and b [ j ] < a [ i - 1 ] ) : NEW_LINE INDENT max_index = i - 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return b [ j - 1 ] NEW_LINE DEDENT if ( j == 0 ) : NEW_LINE INDENT return a [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return maximum ( a [ i - 1 ] , b [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT print ( " ERROR ! ! ! ▁ " ,   " returning " ) NEW_LINE return 0 NEW_LINE DEDENT def maximum ( a , b ) : NEW_LINE INDENT return a if a > b else b NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 900 ] NEW_LINE b = [ 10 , 13 , 14 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE if ( n < m ) : NEW_LINE INDENT print ( " The ▁ median ▁ is : ▁ " , findMedianSortedArrays ( a , n , b , m ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ median ▁ is : ▁ " , findMedianSortedArrays ( b , m , a , n ) ) NEW_LINE DEDENT DEDENT
Print uncommon elements from two sorted arrays | Python 3 program to find uncommon elements of two sorted arrays ; If not common , print smaller ; Skip common element ; printing remaining elements ; Driver code
def printUncommon ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE while ( i < n1 and j < n2 ) : NEW_LINE INDENT if ( arr1 [ i ] < arr2 [ j ] ) : NEW_LINE INDENT print ( arr1 [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE k = k + 1 NEW_LINE DEDENT elif ( arr2 [ j ] < arr1 [ i ] ) : NEW_LINE INDENT print ( arr2 [ j ] , end = " ▁ " ) NEW_LINE k = k + 1 NEW_LINE j = j + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT while ( i < n1 ) : NEW_LINE INDENT print ( arr1 [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE k = k + 1 NEW_LINE DEDENT while ( j < n2 ) : NEW_LINE INDENT print ( arr2 [ j ] , end = " ▁ " ) NEW_LINE j = j + 1 NEW_LINE k = k + 1 NEW_LINE DEDENT DEDENT arr1 = [ 10 , 20 , 30 ] NEW_LINE arr2 = [ 20 , 25 , 30 , 40 , 50 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE n2 = len ( arr2 ) NEW_LINE printUncommon ( arr1 , arr2 , n1 , n2 ) NEW_LINE
Least frequent element in an array | Python 3 program to find the least frequent element in an array . ; Sort the array ; find the min frequency using linear traversal ; If last element is least frequent ; Driver program
def leastFrequent ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE min_count = n + 1 NEW_LINE res = - 1 NEW_LINE curr_count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT curr_count = curr_count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( curr_count < min_count ) : NEW_LINE INDENT min_count = curr_count NEW_LINE res = arr [ i - 1 ] NEW_LINE DEDENT curr_count = 1 NEW_LINE DEDENT DEDENT if ( curr_count < min_count ) : NEW_LINE INDENT min_count = curr_count NEW_LINE res = arr [ n - 1 ] NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( leastFrequent ( arr , n ) ) NEW_LINE
Least frequent element in an array | Python3 program to find the most frequent element in an array . ; Insert all elements in Hash . ; find the max frequency ; Driver Code
import math as mt NEW_LINE def leastFrequent ( arr , n ) : NEW_LINE INDENT Hash = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in Hash . keys ( ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Hash [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT min_count = n + 1 NEW_LINE res = - 1 NEW_LINE for i in Hash : NEW_LINE INDENT if ( min_count >= Hash [ i ] ) : NEW_LINE INDENT res = i NEW_LINE min_count = Hash [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( leastFrequent ( arr , n ) ) NEW_LINE
Maximum sum of increasing order elements from n arrays | Python3 program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Sort each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; Driver Code
M = 4 ; NEW_LINE def maximumSum ( a , n ) : NEW_LINE INDENT global M ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT a [ i ] . sort ( ) ; NEW_LINE DEDENT sum = a [ n - 1 ] [ M - 1 ] ; NEW_LINE prev = a [ n - 1 ] [ M - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] [ j ] < prev ) : NEW_LINE INDENT prev = a [ i ] [ j ] ; NEW_LINE sum += prev ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( j == - 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT arr = [ [ 1 , 7 , 3 , 4 ] , [ 4 , 2 , 5 , 1 ] , [ 9 , 5 , 1 , 8 ] ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maximumSum ( arr , n ) ) ; NEW_LINE
Maximum sum of increasing order elements from n arrays | Python3 program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; max_smaller equals to INT_MIN means no element is found in a [ i ] so return 0 ; Driver Code
M = 4 NEW_LINE def maximumSum ( a , n ) : NEW_LINE INDENT prev = max ( max ( a ) ) NEW_LINE Sum = prev NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT max_smaller = - 10 ** 9 NEW_LINE for j in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] [ j ] < prev and a [ i ] [ j ] > max_smaller ) : NEW_LINE INDENT max_smaller = a [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( max_smaller == - 10 ** 9 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT prev = max_smaller NEW_LINE Sum += max_smaller NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ [ 1 , 7 , 3 , 4 ] , [ 4 , 2 , 5 , 1 ] , [ 9 , 5 , 1 , 8 ] ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumSum ( arr , n ) ) NEW_LINE
Pairs such that one is a power multiple of other | Program to find pairs count ; function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; driver program
import math NEW_LINE def countPairs ( A , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE A . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( ( A [ i ] * math . pow ( k , x ) ) <= A [ j ] ) : NEW_LINE INDENT if ( ( A [ i ] * math . pow ( k , x ) ) == A [ j ] ) : NEW_LINE INDENT ans += 1 NEW_LINE break NEW_LINE DEDENT x += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 ] NEW_LINE n = len ( A ) NEW_LINE k = 3 NEW_LINE print ( countPairs ( A , n , k ) ) NEW_LINE
Minimum distance between two occurrences of maximum | Function to return min distance ; case a ; case b ; case c ; driver program
def minDistance ( arr , n ) : NEW_LINE INDENT maximum_element = arr [ 0 ] NEW_LINE min_dis = n NEW_LINE index = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( maximum_element == arr [ i ] ) : NEW_LINE INDENT min_dis = min ( min_dis , ( i - index ) ) NEW_LINE index = i NEW_LINE DEDENT elif ( maximum_element < arr [ i ] ) : NEW_LINE INDENT maximum_element = arr [ i ] NEW_LINE min_dis = n NEW_LINE index = i NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT return min_dis NEW_LINE DEDENT arr = [ 6 , 3 , 1 , 3 , 6 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ distance ▁ = " , minDistance ( arr , n ) ) NEW_LINE
Find final value if we double after every successful search in array | Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver 's Code
def findValue ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == k ) : NEW_LINE INDENT k = k * 2 NEW_LINE DEDENT DEDENT return k NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 10 , 8 , 1 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( findValue ( arr , n , k ) ) NEW_LINE
Last duplicate element in a sorted array | Python3 code to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; If we reach here , then no duplicate found . ; Driver Code
def dupLastIndex ( arr , n ) : NEW_LINE INDENT if ( arr == None or n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT print ( " Last ▁ index : " , i ,   " Last " , ▁ " duplicate item : " , arr [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " no ▁ duplicate ▁ found " ) NEW_LINE DEDENT arr = [ 1 , 5 , 5 , 6 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE dupLastIndex ( arr , n ) NEW_LINE
Find an array element such that all elements are divisible by it | Function to find smallest num ; Traverse for all elements ; Stores the minimum if it divides all ; Driver code
def findSmallest ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( a [ j ] % a [ i ] ) >= 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == n - 1 ) : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ 25 , 20 , 5 , 10 , 100 ] NEW_LINE n = len ( a ) NEW_LINE print ( findSmallest ( a , n ) ) NEW_LINE
Find an array element such that all elements are divisible by it | Function to find the smallest element ; Function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code
def min_element ( a ) : NEW_LINE INDENT m = 10000000 NEW_LINE for i in range ( 0 , len ( a ) ) : NEW_LINE INDENT if ( a [ i ] < m ) : NEW_LINE INDENT m = a [ i ] NEW_LINE DEDENT DEDENT return m NEW_LINE DEDENT def findSmallest ( a , n ) : NEW_LINE INDENT smallest = min_element ( a ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] % smallest >= 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT return smallest NEW_LINE DEDENT a = [ 25 , 20 , 5 , 10 , 100 ] NEW_LINE n = len ( a ) NEW_LINE print ( findSmallest ( a , n ) ) NEW_LINE