codeparrot/xlcost-text-to-code huggingface dataset:
Maximum consecutive numbers present in an array | Python3 program to find largest consecutive numbers present in arr . ; We insert all the array elements into unordered set . ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; increment the value of array element and repeat search in the set ; Update optimal length if this length is more . To get the length as it is incremented one by one ; Driver code
def findLongestConseqSubseq ( arr , n ) : NEW_LINE INDENT S = set ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT S . add ( arr [ i ] ) ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if S . __contains__ ( arr [ i ] ) : NEW_LINE INDENT j = arr [ i ] ; NEW_LINE while ( S . __contains__ ( j ) ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT ans = max ( ans , j - arr [ i ] ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 94 , 93 , 1000 , 5 , 92 , 78 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findLongestConseqSubseq ( arr , n ) ) ; NEW_LINE DEDENT
N / 3 repeated number in an array with O ( 1 ) space | Python 3 program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
import sys NEW_LINE def appearsNBy3 ( arr , n ) : NEW_LINE INDENT count1 = 0 NEW_LINE count2 = 0 NEW_LINE first = sys . maxsize NEW_LINE second = sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( first == arr [ i ] ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT elif ( second == arr [ i ] ) : NEW_LINE INDENT count2 += 1 NEW_LINE DEDENT elif ( count1 == 0 ) : NEW_LINE INDENT count1 += 1 NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( count2 == 0 ) : NEW_LINE INDENT count2 += 1 NEW_LINE second = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT count1 -= 1 NEW_LINE count2 -= 1 NEW_LINE DEDENT DEDENT count1 = 0 NEW_LINE count2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == first ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT elif ( arr [ i ] == second ) : NEW_LINE INDENT count2 += 1 NEW_LINE DEDENT DEDENT if ( count1 > n / 3 ) : NEW_LINE INDENT return first NEW_LINE DEDENT if ( count2 > n / 3 ) : NEW_LINE INDENT return second NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( appearsNBy3 ( arr , n ) ) NEW_LINE
Count pairs in array whose sum is divisible by 4 | Python3 code to count pairs whose sum is divisible by '4 ; Function to count pairs whose sum is divisible by '4 ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4 ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code
' NEW_LINE ' NEW_LINE def count4Divisibiles ( arr , n ) : NEW_LINE INDENT freq = [ 0 , 0 , 0 , 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] % 4 ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 NEW_LINE ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 NEW_LINE ans += freq [ 1 ] * freq [ 3 ] NEW_LINE return int ( ans ) NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 7 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( count4Divisibiles ( arr , n ) ) NEW_LINE
Find Sum of all unique sub | function for finding grandSum ; calculate cumulative sum of array cArray [ 0 ] will store sum of zero elements ; store all subarray sum in vector ; sort the vector ; mark all duplicate sub - array sum to zero ; calculate total sum ; return totalSum ; Drivers code
def findSubarraySum ( arr , n ) : NEW_LINE INDENT cArray = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT cArray [ i + 1 ] = cArray [ i ] + arr [ i ] NEW_LINE DEDENT subArrSum = [ ] NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT for j in range ( i , n + 1 , 1 ) : NEW_LINE INDENT subArrSum . append ( cArray [ j ] - cArray [ i - 1 ] ) NEW_LINE DEDENT DEDENT subArrSum . sort ( reverse = False ) NEW_LINE totalSum = 0 NEW_LINE for i in range ( 0 , len ( subArrSum ) - 1 , 1 ) : NEW_LINE INDENT if ( subArrSum [ i ] == subArrSum [ i + 1 ] ) : NEW_LINE INDENT j = i + 1 NEW_LINE while ( subArrSum [ j ] == subArrSum [ i ] and j < len ( subArrSum ) ) : NEW_LINE INDENT subArrSum [ j ] = 0 NEW_LINE j += 1 NEW_LINE DEDENT subArrSum [ i ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( subArrSum ) , 1 ) : NEW_LINE INDENT totalSum += subArrSum [ i ] NEW_LINE DEDENT return totalSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 3 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSubarraySum ( arr , n ) ) NEW_LINE DEDENT
Smallest number whose set bits are maximum in a given range | Python code to find number whose set bits are maximum among ' l ' and 'r ; Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; driver code
' NEW_LINE def countMaxSetBits ( left , right ) : NEW_LINE INDENT max_count = - 1 NEW_LINE for i in range ( left , right + 1 ) : NEW_LINE INDENT temp = i NEW_LINE cnt = 0 NEW_LINE while temp : NEW_LINE INDENT if temp & 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT temp = temp >> 1 NEW_LINE DEDENT if cnt > max_count : NEW_LINE INDENT max_count = cnt NEW_LINE num = i NEW_LINE DEDENT DEDENT return num NEW_LINE DEDENT l = 1 NEW_LINE r = 5 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE l = 1 NEW_LINE r = 10 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE
Find Sum of all unique sub | function for finding grandSum ; Go through all subarrays , compute sums and count occurrences of sums . ; Print all those Sums that appear once . ; Driver code
def findSubarraySum ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT Sum += arr [ j ] NEW_LINE m [ Sum ] = m . get ( Sum , 0 ) + 1 NEW_LINE DEDENT DEDENT for x in m : NEW_LINE INDENT if m [ x ] == 1 : NEW_LINE INDENT res += x NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 3 , 2 , 3 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSubarraySum ( arr , n ) ) NEW_LINE
Recaman 's sequence | Prints first n terms of Recaman sequence ; Create an array to store terms ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
def recaman ( n ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE arr [ 0 ] = 0 NEW_LINE print ( arr [ 0 ] , end = " , ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = arr [ i - 1 ] - i NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if ( ( arr [ j ] == curr ) or curr < 0 ) : NEW_LINE INDENT curr = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = curr NEW_LINE print ( arr [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE
Recaman 's sequence | Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
def recaman ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( 0 , " , " , end = ' ' ) NEW_LINE s = set ( [ ] ) NEW_LINE s . add ( 0 ) NEW_LINE prev = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = prev - i NEW_LINE if ( curr < 0 or curr in s ) : NEW_LINE INDENT curr = prev + i NEW_LINE DEDENT s . add ( curr ) NEW_LINE print ( curr , " , " , end = ' ' ) NEW_LINE prev = curr NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE DEDENT
Largest subset whose all elements are Fibonacci numbers | Prints largest subset of an array whose all elements are fibonacci numbers ; Find maximum element in arr [ ] ; Generate all Fibonacci numbers till max and store them in hash . ; Npw iterate through all numbers and quickly check for Fibonacci using hash . ; Driver code
def findFibSubset ( arr , n ) : NEW_LINE INDENT m = max ( arr ) NEW_LINE a = 0 NEW_LINE b = 1 NEW_LINE hash = [ ] NEW_LINE hash . append ( a ) NEW_LINE hash . append ( b ) NEW_LINE while ( b < m ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE hash . append ( b ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in hash : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 ] NEW_LINE n = len ( arr ) NEW_LINE findFibSubset ( arr , n ) NEW_LINE DEDENT
Pairs of Amicable Numbers | Efficient Python3 program to count Amicable pairs in an array . ; Calculating the sum of proper divisors ; 1 is a proper divisor ; To handle perfect squares ; check if pair is ambicle ; This function prints count of amicable pairs present in the input array ; Map to store the numbers ; Iterate through each number , and find the sum of proper divisors and check if it 's  also present in the array ; It 's sum of proper divisors ; As the pairs are counted twice , thus divide by 2 ; Driver Code
import math NEW_LINE def sumOfDiv ( x ) : NEW_LINE INDENT sum = 1 ; NEW_LINE for i in range ( 2 , int ( math . sqrt ( x ) ) ) : NEW_LINE INDENT if x % i == 0 : NEW_LINE INDENT sum += i NEW_LINE if i != x / i : NEW_LINE INDENT sum += x / i NEW_LINE DEDENT DEDENT DEDENT return int ( sum ) ; NEW_LINE DEDENT def isAmbicle ( a , b ) : NEW_LINE INDENT return ( sumOfDiv ( a ) == b and sumOfDiv ( b ) == a ) NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if sumOfDiv ( arr [ i ] ) in s : NEW_LINE INDENT sum = sumOfDiv ( arr [ i ] ) NEW_LINE if isAmbicle ( arr [ i ] , sum ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return int ( count / 2 ) ; NEW_LINE DEDENT arr1 = [ 220 , 284 , 1184 , 1210 , 2 , 5 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( countPairs ( arr1 , n1 ) ) NEW_LINE arr2 = [ 2620 , 2924 , 5020 , 5564 , 6232 , 6368 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( countPairs ( arr2 , n2 ) ) NEW_LINE
Minimum number of characters required to be removed such that every character occurs same number of times | Python3 program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input
import sys NEW_LINE def minimumDeletion ( s , n ) : NEW_LINE INDENT countMap = { } NEW_LINE for i in s : NEW_LINE INDENT countMap [ i ] = countMap . get ( i , 0 ) + 1 NEW_LINE DEDENT countMultiset = [ ] NEW_LINE for it in countMap : NEW_LINE INDENT countMultiset . append ( countMap [ it ] ) NEW_LINE DEDENT ans = sys . maxsize + 1 NEW_LINE i = 0 NEW_LINE m = len ( countMultiset ) NEW_LINE for j in sorted ( countMultiset ) : NEW_LINE INDENT ans = min ( ans , n - ( m - i ) * j ) NEW_LINE i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE N = len ( S ) NEW_LINE print ( minimumDeletion ( S , N ) ) NEW_LINE DEDENT
Calculate cost of visiting all array elements in increasing order | Function to calculate total cost of visiting array elements in increasing order ; Stores the pair of element and their positions ; Traverse the array arr [ ] ; Push the pair { arr [ i ] , i } in v ; Sort the vector in ascending order . ; Stores the total cost ; Stores the index of last element visited ; Traverse the vector v ; Increment ans ; Assign ; Return ans ; Driver Code
def calculateDistance ( arr , N ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT v . append ( [ arr [ i ] , i ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE ans = 0 NEW_LINE last = 0 NEW_LINE for j in v : NEW_LINE INDENT ans += abs ( j [ 1 ] - last ) NEW_LINE last = j [ 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( calculateDistance ( arr , N ) ) NEW_LINE DEDENT
Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | Python 3 program of the above approach ; Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
import math NEW_LINE def sortArr ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT s = str ( arr [ i ] ) NEW_LINE list1 = list ( s ) NEW_LINE list1 . sort ( reverse = True ) NEW_LINE s = ' ' . join ( list1 ) NEW_LINE arr [ i ] = int ( s ) NEW_LINE sr = int ( math . sqrt ( arr [ i ] ) ) NEW_LINE a = sr * sr NEW_LINE b = ( sr + 1 ) * ( sr + 1 ) NEW_LINE if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) : NEW_LINE INDENT arr [ i ] = a NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = b NEW_LINE DEDENT DEDENT arr . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 29 , 43 , 28 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE sortArr ( arr , N ) NEW_LINE DEDENT
Minimize cost required to make all array elements greater than or equal to zero | Python3 program for the above approach ; Function to find the minimum cost to make all array of elements greater than or equal to 0 ; sort the array in ascending order ; stores the count to make current array element >= 0 ; stores the cost to make all array elements >= 0 ; Traverse the array and insert all the elements which are <= 0 ; if current array element is negative ; cost to make all array elements >= 0 ; update curr if ans is minimum ; return minimum cost ; Given array ; size of the array ; Given value of x ; Function call to find minimum cost to make all array elements >= 0
import sys NEW_LINE def mincost ( arr , N , X ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE sum = 0 NEW_LINE cost = 0 NEW_LINE min_cost = sys . maxsize NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT cost = abs ( arr [ i ] ) * x + ( sum - abs ( arr [ i ] ) * i ) NEW_LINE sum += abs ( arr [ i ] ) NEW_LINE min_cost = min ( min_cost , cost ) NEW_LINE DEDENT DEDENT return min_cost NEW_LINE DEDENT arr = [ - 1 , - 3 , - 2 , 4 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE x = 2 NEW_LINE print ( mincost ( arr , N , x ) ) NEW_LINE
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Given vector ; Given value of X & K
ans = 0 NEW_LINE def countArraysUtil ( arr , X , K , xorVal ) : NEW_LINE INDENT global ans NEW_LINE if ( X == 0 ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > xorVal ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if ( cnt >= K ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return NEW_LINE DEDENT temp = arr [ 0 ] NEW_LINE arr . pop ( 0 ) NEW_LINE countArraysUtil ( arr , X - 1 , K , xorVal ) NEW_LINE arr . insert ( 0 , temp ) NEW_LINE temp = arr [ - 1 ] NEW_LINE arr . pop ( ) NEW_LINE countArraysUtil ( arr , X - 1 , K , xorVal ) NEW_LINE arr . append ( temp ) NEW_LINE arr [ 0 ] += 1 NEW_LINE countArraysUtil ( arr , X - 1 , K , xorVal ) NEW_LINE arr [ 0 ] -= 1 NEW_LINE arr [ len ( arr ) - 1 ] += 1 NEW_LINE countArraysUtil ( arr , X - 1 , K , xorVal ) NEW_LINE arr [ len ( arr ) - 1 ] -= 1 NEW_LINE DEDENT def countArrays ( arr , X , K ) : NEW_LINE INDENT xorVal = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT xorVal = xorVal ^ arr [ i ] NEW_LINE DEDENT countArraysUtil ( arr , X , K , xorVal ) NEW_LINE print ( ans ) NEW_LINE DEDENT arr = [ 10 , 2 , 10 , 5 ] NEW_LINE X = 3 NEW_LINE K = 3 NEW_LINE countArrays ( arr , X , K ) NEW_LINE
Difference between maximum and minimum of a set of anagrams from an array | Python3 program for the above approach ; Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; If the end of Map is reached , then no anagrams are present ; Given array ; Size of the array
import math NEW_LINE from collections import defaultdict NEW_LINE def hashFunction ( N ) : NEW_LINE INDENT prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 ] NEW_LINE value = 1 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT r = N % 10 NEW_LINE value = value * prime [ r ] NEW_LINE N = N // 10 NEW_LINE DEDENT return value NEW_LINE DEDENT def findDiff ( arr , n ) : NEW_LINE INDENT m = defaultdict ( lambda : [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT h = hashFunction ( arr [ i ] ) NEW_LINE m [ h ] . append ( arr [ i ] ) NEW_LINE DEDENT i = 0 NEW_LINE while ( i != len ( m ) ) : NEW_LINE INDENT if ( len ( m [ i ] ) > 1 ) : NEW_LINE INDENT minn = min ( m [ i ] ) NEW_LINE maxx = max ( m [ i ] ) NEW_LINE print ( maxx - minn ) NEW_LINE break NEW_LINE DEDENT elif ( i == ( len ( m ) - 1 ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT arr = [ 121 , 312 , 234 , 211 , 112 , 102 ] NEW_LINE N = len ( arr ) NEW_LINE findDiff ( arr , N ) NEW_LINE
Maximum number of apples that can be eaten by a person | Function to find the maximum number of apples a person can eat such that the person eat at most one apple in a day . ; Store count of apples and number of days those apples are edible ; Stores indices of the array ; Stores count of days ; Stores maximum count of edible apples ; Traverse both the arrays ; If top element of the apple is not already expired ; Insert count of apples and their expiration date ; Remove outdated apples ; Insert all the apples produces by tree on current day ; Stores top element of pq ; Remove top element of pq ; If count of apples in curr is greater than 0 ; Insert count of apples and their expiration date ; Update total_apples ; Update index ; Driver Code
def cntMaxApples ( apples , days ) : NEW_LINE INDENT pq = [ ] NEW_LINE i = 0 NEW_LINE n = len ( apples ) NEW_LINE total_apples = 0 NEW_LINE while ( i < n or len ( pq ) > 0 ) : NEW_LINE if ( i < n and apples [ i ] != 0 ) : NEW_LINE INDENT pq . append ( [ i + days [ i ] - 1 , apples [ i ] ] ) NEW_LINE pq . sort ( ) NEW_LINE DEDENT while ( len ( pq ) > 0 and pq [ 0 ] [ 0 ] < i ) : NEW_LINE INDENT pq . pop ( 0 ) NEW_LINE DEDENT if ( len ( pq ) > 0 ) : NEW_LINE INDENT curr = pq [ 0 ] NEW_LINE pq . pop ( 0 ) NEW_LINE if ( len ( curr ) > 1 ) : NEW_LINE pq . append ( [ curr [ 0 ] , curr [ 1 ] - 1 ] ) NEW_LINE pq . sort ( ) NEW_LINE total_apples += 1 NEW_LINE DEDENT i += 1 NEW_LINE return total_apples NEW_LINE DEDENT apples = [ 1 , 2 , 3 , 5 , 2 ] NEW_LINE days = [ 3 , 2 , 1 , 4 , 2 ] NEW_LINE print ( cntMaxApples ( apples , days ) ) NEW_LINE
Maximum area rectangle by picking four sides from array | function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; Driver code
def findArea ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE dimension = [ 0 , 0 ] NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while ( i < n - 1 and j < 2 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT dimension [ j ] = arr [ i ] NEW_LINE j += 1 NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( dimension [ 0 ] * dimension [ 1 ] ) NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findArea ( arr , n ) ) NEW_LINE
Maximum of sum of length of rectangles and squares formed by given sticks | Python3 implementation of the above approach ; Function to find the maximum of sum of lengths of rectangles and squares formed using given set of sticks ; Stores the count of frequencies of all the array elements ; Stores frequencies which are at least 2 ; Convert all frequencies to nearest smaller even values . ; Sum of elements up to index of multiples of 4 ; Print the sum ; Driver Code
from collections import Counter NEW_LINE def findSumLength ( arr , n ) : NEW_LINE INDENT freq = dict ( Counter ( arr ) ) NEW_LINE freq_2 = { } NEW_LINE for i in freq : NEW_LINE INDENT if freq [ i ] >= 2 : NEW_LINE INDENT freq_2 [ i ] = freq [ i ] NEW_LINE DEDENT DEDENT arr1 = [ ] NEW_LINE for i in freq_2 : NEW_LINE arr1 . extend ( [ i ] * ( freq_2 [ i ] // 2 ) * 2 ) NEW_LINE arr1 . sort ( reverse = True ) NEW_LINE summ = 0 NEW_LINE for i in range ( ( len ( arr1 ) // 4 ) * 4 ) : NEW_LINE INDENT summ += arr1 [ i ] NEW_LINE DEDENT print ( summ ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findSumLength ( arr , n ) ; NEW_LINE DEDENT
Maximum area rectangle by picking four sides from array | function for finding max area ; traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; return the product of dimensions ; Driver Code
def findArea ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE first = 0 NEW_LINE second = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] not in s : NEW_LINE INDENT s . append ( arr [ i ] ) NEW_LINE continue NEW_LINE DEDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT return ( first * second ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findArea ( arr , n ) ) NEW_LINE DEDENT