codeparrot/xlcost-text-to-code huggingface dataset:
Ways to choose three points with distance between the most distant points <= L |  ; def countTripletsLessThanL ( n , L , arr ) : ; arr = sorted ( arr ) ; ways = 0 ; for i in range ( n ) : ; indexGreater = upper_bound ( arr , 0 , n , arr [ i ] + L ) ; ; numberOfElements = indexGreater - ( i + 1 ) ; ; if ( numberOfElements >= 2 ) : ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; return ways ; def upper_bound ( a , low , high , element ) : while ( low < high ) : middle = int ( low + ( high - low ) / 2 ) ; if ( a [ middle ] > element ) : high = middle ; else : low = middle + 1 ; return low ; ; if __name__ == ' _ _ main _ _ ' : set of n points on the X axis
Returns the number of triplets with the NEW_LINE distance between farthest points <= L NEW_LINE INDENT sort the array NEW_LINE INDENT find index of element greater than arr [ i ] + L NEW_LINE find Number of elements between the ith NEW_LINE index and indexGreater since the Numbers NEW_LINE are sorted and the elements are distinct NEW_LINE from the points btw these indices represent NEW_LINE points within range ( a [ i ] + 1 and a [ i ] + L ) NEW_LINE both inclusive NEW_LINE if there are at least two elements in between NEW_LINE i and indexGreater find the Number of ways NEW_LINE to select two points out of these NEW_LINE DEDENT DEDENT Driver Code NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE L = 4 ; NEW_LINE ans = countTripletsLessThanL ( n , L , arr ) ; NEW_LINE print ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " , ans ) ; NEW_LINE DEDENT
Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
def minSum ( arr , n ) : NEW_LINE INDENT sm = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == arr [ i - 1 ] : NEW_LINE INDENT j = i NEW_LINE while j < n and arr [ j ] <= arr [ j - 1 ] : NEW_LINE INDENT arr [ j ] = arr [ j ] + 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT sm = sm + arr [ i ] NEW_LINE DEDENT return sm NEW_LINE DEDENT arr = [ 2 , 2 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSum ( arr , n ) ) NEW_LINE
Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Drivers code
def minSum ( arr , n ) : NEW_LINE INDENT sum = arr [ 0 ] ; prev = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] <= prev : NEW_LINE INDENT prev = prev + 1 NEW_LINE sum = sum + prev NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE prev = arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ 2 , 2 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSum ( arr , n ) ) NEW_LINE
Randomized Binary Search Algorithm | Python3 program to implement recursive randomized algorithm . To generate random number between x and y ie . . [ x , y ] ; A recursive randomized binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code
import random NEW_LINE def getRandom ( x , y ) : NEW_LINE INDENT tmp = ( x + random . randint ( 0 , 100000 ) % ( y - x + 1 ) ) NEW_LINE return tmp NEW_LINE DEDENT def randomizedBinarySearch ( arr , l , r , x ) : NEW_LINE INDENT if r >= l : NEW_LINE INDENT mid = getRandom ( l , r ) NEW_LINE if arr [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT if arr [ mid ] > x : NEW_LINE INDENT return randomizedBinarySearch ( arr , l , mid - 1 , x ) NEW_LINE DEDENT return randomizedBinarySearch ( arr , mid + 1 , r , x ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 10 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE x = 10 NEW_LINE result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) NEW_LINE if result == - 1 : NEW_LINE INDENT print ( ' Element ▁ is ▁ not ▁ present ▁ in ▁ array ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' Element ▁ is ▁ present ▁ at ▁ index ▁ ' , result ) NEW_LINE DEDENT DEDENT
Program to remove vowels from a String | Python program to remove vowels from a string Function to remove vowels ; Driver program
def rem_vowel ( string ) : NEW_LINE INDENT vowels = [ ' a ' , ' e ' , ' i ' , ' o ' , ' u ' ] NEW_LINE result = [ letter for letter in string if letter . lower ( ) not in vowels ] NEW_LINE result = ' ' . join ( result ) NEW_LINE print ( result ) NEW_LINE DEDENT string = " GeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " NEW_LINE rem_vowel ( string ) NEW_LINE string = " Loving ▁ Python ▁ LOL " NEW_LINE rem_vowel ( string ) NEW_LINE
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver code
def minCostToMakeElementEqual ( a ) : NEW_LINE INDENT l = len ( a ) NEW_LINE if ( l % 2 == 1 ) : NEW_LINE INDENT y = a [ l // 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT y = ( a [ l // 2 ] + a [ ( l - 2 ) // 2 ] ) // 2 NEW_LINE DEDENT s = 0 NEW_LINE for i in range ( l ) : NEW_LINE INDENT s += abs ( a [ i ] - y ) NEW_LINE DEDENT return s NEW_LINE DEDENT a = [ 1 , 100 , 101 ] NEW_LINE print ( minCostToMakeElementEqual ( a ) ) NEW_LINE
Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Given array
def isSorted ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT def sortPoss ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT idx = - 1 NEW_LINE minVar = arr [ i ] NEW_LINE for j in range ( i , len ( arr ) , 2 ) : NEW_LINE if arr [ j ] < minVar : NEW_LINE INDENT minVar = arr [ j ] NEW_LINE idx = j NEW_LINE DEDENT if idx != - 1 : NEW_LINE arr [ i ] , arr [ idx ] = arr [ idx ] , arr [ i ] NEW_LINE DEDENT if isSorted ( arr ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT arr = [ 3 , 5 , 1 , 2 , 6 ] NEW_LINE print ( sortPoss ( arr ) ) NEW_LINE
Print all array elements appearing more than N / K times | Function to + find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to prall array elements whose frequency is greater than N / K ; Sort the array arr ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Driver Code ; Given array arr ; Size of array ; Function Call
def upperBound ( arr , N , K ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = N ; NEW_LINE while ( l < r ) : NEW_LINE INDENT mid = ( l + r ) // 2 ; NEW_LINE if ( arr [ mid ] <= K ) : NEW_LINE INDENT l = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT r = mid ; NEW_LINE DEDENT DEDENT return l ; NEW_LINE DEDENT def NDivKWithFreq ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE while ( i < N ) : NEW_LINE INDENT X = upperBound ( arr , N , arr [ i ] ) ; NEW_LINE if ( ( X - i ) > N // 4 ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) ; NEW_LINE DEDENT i = X ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 4 ; NEW_LINE NDivKWithFreq ( arr , N , K ) ; NEW_LINE DEDENT
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | Function to find maximum possible sum after changing the array elements as per the given constraints ; Stores the frequency of elements in given array ; Update frequency ; stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def maxSum ( a , n ) : NEW_LINE INDENT count = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT count [ min ( a [ i ] , n ) ] += 1 NEW_LINE DEDENT size = 0 NEW_LINE ans = 0 NEW_LINE for k in range ( 1 , n + 1 ) : NEW_LINE INDENT while ( count [ k ] > 0 and size < k ) : NEW_LINE INDENT size += 1 NEW_LINE ans += size NEW_LINE count [ k ] -= 1 NEW_LINE DEDENT ans += k * count [ k ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n ) ) NEW_LINE DEDENT
Find sum of product of every number and its frequency in given range | Function to solve queries ; Calculating answer for every query ; The end points of the ith query ; Map for storing frequency ; Incrementing the frequency ; Iterating over map to find answer ; Adding the contribution of ith number ; Print answer ; Driver code
def answerQueries ( arr , n , queries ) : NEW_LINE INDENT for i in range ( len ( queries ) ) : NEW_LINE INDENT ans = 0 NEW_LINE l = queries [ i ] [ 0 ] - 1 NEW_LINE r = queries [ i ] [ 1 ] - 1 NEW_LINE freq = dict ( ) NEW_LINE for j in range ( l , r + 1 ) : NEW_LINE INDENT freq [ arr [ j ] ] = freq . get ( arr [ j ] , 0 ) + 1 NEW_LINE DEDENT for i in freq : NEW_LINE INDENT ans += ( i * freq [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE queries = [ [ 1 , 2 ] , [ 1 , 3 ] ] NEW_LINE answerQueries ( arr , n , queries ) NEW_LINE DEDENT
Pandigital Product | Calculate the multiplicand , multiplier , and product eligible for pandigital ; To check the string formed from multiplicand multiplier and product is pandigital ; Driver code
def PandigitalProduct_1_9 ( n ) : NEW_LINE INDENT i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( ( n % i == 0 ) and bool ( isPandigital ( str ( n ) + str ( i ) + str ( n // i ) ) ) ) : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return bool ( False ) NEW_LINE DEDENT def isPandigital ( Str ) : NEW_LINE INDENT if ( len ( Str ) != 9 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT ch = " " . join ( sorted ( Str ) ) NEW_LINE if ( ch == "123456789" ) : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT DEDENT n = 6952 NEW_LINE if ( bool ( PandigitalProduct_1_9 ( n ) ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Median and Mode using Counting Sort | Function that sort input array a [ ] and calculate mode and median using counting sort ; The output array b [ ] will have sorted array ; Variable to store max of input array which will to have size of count array ; Auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; Mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver Code
def printModeMedian ( a , n ) : NEW_LINE INDENT b = [ 0 ] * n NEW_LINE Max = max ( a ) NEW_LINE t = Max + 1 NEW_LINE count = [ 0 ] * t NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ a [ i ] ] += 1 NEW_LINE DEDENT mode = 0 NEW_LINE k = count [ 0 ] NEW_LINE for i in range ( 1 , t ) : NEW_LINE INDENT if ( count [ i ] > k ) : NEW_LINE INDENT k = count [ i ] NEW_LINE mode = i NEW_LINE DEDENT DEDENT for i in range ( 1 , t ) : NEW_LINE INDENT count [ i ] = count [ i ] + count [ i - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT b [ count [ a [ i ] ] - 1 ] = a [ i ] NEW_LINE count [ a [ i ] ] -= 1 NEW_LINE DEDENT median = 0.0 NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT median = b [ n // 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT median = ( ( b [ ( n - 1 ) // 2 ] + b [ n // 2 ] ) / 2.0 ) NEW_LINE DEDENT print ( " median ▁ = " , median ) NEW_LINE print ( " mode ▁ = " , mode ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printModeMedian ( arr , n ) NEW_LINE DEDENT
Check if both halves of the string have at least one different character | Python implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function
MAX = 26 NEW_LINE def function ( st ) : NEW_LINE INDENT global MAX NEW_LINE l = len ( st ) NEW_LINE counter1 , counter2 = [ 0 ] * MAX , [ 0 ] * MAX NEW_LINE for i in range ( l // 2 ) : NEW_LINE INDENT counter1 [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l // 2 , l ) : NEW_LINE INDENT counter2 [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( counter2 [ i ] != counter1 [ i ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT st = " abcasdsabcae " NEW_LINE if function ( st ) : print ( " Yes , ▁ both ▁ halves ▁ differ ▁ " , " by ▁ at ▁ least ▁ one ▁ character " ) NEW_LINE else : print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " ) NEW_LINE
Check if both halves of the string have at least one different character | Python3 implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
MAX = 26 NEW_LINE def function ( st ) : NEW_LINE INDENT global MAX NEW_LINE l = len ( st ) NEW_LINE counter = [ 0 ] * MAX NEW_LINE for i in range ( l // 2 ) : NEW_LINE INDENT counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l // 2 , l ) : NEW_LINE INDENT counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( counter [ i ] != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT st = " abcasdsabcae " NEW_LINE if function ( st ) : NEW_LINE INDENT print ( " Yes , ▁ both ▁ halves ▁ differ ▁ by ▁ at ▁ " , " least ▁ one ▁ character " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " ) NEW_LINE DEDENT
Minimum difference between max and min of all K | Returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver Code
def minDifferenceAmongMaxMin ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE res = 2147483647 NEW_LINE for i in range ( ( N - K ) + 1 ) : NEW_LINE INDENT curSeqDiff = arr [ i + K - 1 ] - arr [ i ] NEW_LINE res = min ( res , curSeqDiff ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 10 , 20 , 30 , 100 , 101 , 102 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( minDifferenceAmongMaxMin ( arr , N , K ) ) NEW_LINE
Position of an element after stable sort | Python program to get index of array element in sorted array Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver code to test above methods
def getIndexInSortedArray ( arr , n , idx ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ idx ] ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT if ( arr [ i ] == arr [ idx ] and i < idx ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE idxOfEle = 5 NEW_LINE print getIndexInSortedArray ( arr , n , idxOfEle ) NEW_LINE
Find permutation of numbers upto N with a specific sum in a specific range | Function to check if sum is possible with remaining numbers ; Stores the minimum sum possible with x numbers ; Stores the maximum sum possible with x numbers ; If S lies in the range [ minSum , maxSum ] ; Function to find the resultant permutation ; Stores the count of numbers in the given segment ; If the sum is not possible with numbers in the segment ; Output - 1 ; Stores the numbers present within the given segment ; Iterate over the numbers from 1 to N ; If ( S - i ) is a positive non - zero sum and if it is possible to obtain ( S - i ) remaining numbers ; Update sum S ; Update required numbers in the segement ; Push i in vector v ; If sum has been obtained ; Break from the loop ; If sum is not obtained ; Output - 1 ; Stores the numbers which are not present in given segment ; Loop to check the numbers not present in the segment ; Pointer to check if i is present in vector v or not ; If i is not present in v ; Push i in vector v1 ; Point to the first elements of v1 and v respectively ; Print the required permutation ; Driver Code
def possible ( x , S , N ) : NEW_LINE INDENT minSum = ( x * ( x + 1 ) ) // 2 NEW_LINE maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) // 2 NEW_LINE if ( S < minSum or S > maxSum ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def findPermutation ( N , L , R , S ) : NEW_LINE INDENT x = R - L + 1 NEW_LINE if ( not possible ( x , S , N ) ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( N , 0 , - 1 ) : NEW_LINE INDENT if ( ( S - i ) >= 0 and possible ( x - 1 , S - i , i - 1 ) ) : NEW_LINE INDENT S = S - i NEW_LINE x -= 1 NEW_LINE v . append ( i ) NEW_LINE DEDENT if ( S == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( S != 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT v1 = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT it = i in v NEW_LINE if ( not it ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE DEDENT DEDENT j = 0 NEW_LINE f = 0 NEW_LINE for i in range ( 1 , L ) : NEW_LINE INDENT print ( v1 [ j ] , end = " ▁ " ) NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( L , R + 1 ) : NEW_LINE INDENT print ( v [ f ] , end = " ▁ " ) NEW_LINE f += 1 NEW_LINE DEDENT for i in range ( R + 1 , N + 1 ) : NEW_LINE INDENT print ( v1 [ j ] , end = " ▁ " ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE L = 3 NEW_LINE R = 5 NEW_LINE S = 8 NEW_LINE findPermutation ( N , L , R , S ) NEW_LINE DEDENT
Minimum count of indices to be skipped for every index of Array to keep sum till that index at most T | Function to calculate minimum indices to be skipped so that sum till i remains smaller than T ; Store the sum of all indices before i ; Store the elements that can be skipped ; Traverse the array , A [ ] ; Store the total sum of elements that needs to be skipped ; Store the number of elements need to be removed ; Traverse from the back of map so as to take bigger elements first ; Update sum ; Update map with the current element ; Driver code ; Given Input ; Function Call
def skipIndices ( N , T , arr ) : NEW_LINE INDENT sum = 0 NEW_LINE count = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT d = sum + arr [ i ] - T NEW_LINE k = 0 NEW_LINE if ( d > 0 ) : NEW_LINE INDENT for u in list ( count . keys ( ) ) [ : : - 1 ] : NEW_LINE INDENT j = u NEW_LINE x = j * count [ j ] NEW_LINE if ( d <= x ) : NEW_LINE INDENT k += ( d + j - 1 ) // j NEW_LINE break NEW_LINE DEDENT k += count [ j ] NEW_LINE d -= x NEW_LINE DEDENT DEDENT sum += arr [ i ] NEW_LINE count [ arr [ i ] ] = count . get ( arr [ i ] , 0 ) + 1 NEW_LINE print ( k , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE T = 15 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE skipIndices ( N , T , arr ) NEW_LINE DEDENT
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code
def countTriplets ( N , K ) : NEW_LINE INDENT if ( K % 2 == 0 ) : NEW_LINE INDENT x = N // K NEW_LINE y = ( N + ( K // 2 ) ) // K NEW_LINE return x * x * x + y * y * y NEW_LINE DEDENT else : NEW_LINE INDENT x = N // K NEW_LINE return x * x * x NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE K = 2 NEW_LINE print ( countTriplets ( N , K ) ) NEW_LINE DEDENT
Maximize count of 3 | Function to count the maximum number of palindrome subsequences of length 3 considering the same index only once ; Index of the S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value ; Driver Code
def maxNumPalindrome ( S ) : NEW_LINE INDENT i = 0 NEW_LINE freq = [ 0 ] * 26 NEW_LINE freqPair = 0 NEW_LINE ln = len ( S ) // 3 NEW_LINE while ( i < len ( S ) ) : NEW_LINE INDENT freq [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE i += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT freqPair += ( freq [ i ] // 2 ) NEW_LINE DEDENT return min ( freqPair , ln ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforg " NEW_LINE print ( maxNumPalindrome ( S ) ) NEW_LINE DEDENT