text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Store frequencies of arr [ ] | subsets = findSubsets ( arr ) NEW_LINE |
If size of arr [ ] is odd then print " Yes " | if ( len ( arr ) % 2 == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT |
Check if answer is true or not | isPossible = subsetSum ( subsets , len ( arr ) // 2 ) NEW_LINE |
Print the result | if ( isPossible ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given array arr | arr = [ 2 , 1 , 2 , 3 ] NEW_LINE |
Function Call | divideInto2Subset ( arr ) NEW_LINE |
Python3 program for the above approach | MOD = 1000000007 NEW_LINE |
Function to find number of way to distribute coins giving exactly one coin to each person | def solve ( values , salary ) : NEW_LINE INDENT ret = 1 NEW_LINE amt = 0 NEW_LINE DEDENT |
Sort the given arrays | values = sorted ( values ) NEW_LINE salary = sorted ( salary ) NEW_LINE |
Start from bigger salary | while ( len ( salary ) > 0 ) : NEW_LINE INDENT while ( ( len ( values ) and values [ - 1 ] >= salary [ - 1 ] ) ) : NEW_LINE DEDENT |
Increment the amount | amt += 1 NEW_LINE del values [ - 1 ] NEW_LINE if ( amt == 0 ) : NEW_LINE return 0 NEW_LINE |
Reduce amount of valid coins by one each time | ret *= amt NEW_LINE amt -= 1 NEW_LINE ret %= MOD NEW_LINE del salary [ - 1 ] NEW_LINE |
Return the result | return ret NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given two arrays | values = [ 1 , 2 ] NEW_LINE salary = [ 2 ] NEW_LINE |
Function call | print ( solve ( values , salary ) ) NEW_LINE |
Function that counts the pair in the array arr [ ] | def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE DEDENT |
Sort the array | arr . sort ( ) NEW_LINE |
Initialize two pointers | left = 0 NEW_LINE right = 1 ; NEW_LINE while ( right < n ) : NEW_LINE INDENT if ( arr [ left ] == arr [ right ] ) : NEW_LINE DEDENT |
Add all valid pairs to answer | ans += right - left ; NEW_LINE else : NEW_LINE left = right ; NEW_LINE right += 1 NEW_LINE |
Return the answer | return ans NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given array arr [ ] | arr = [ 2 , 2 , 3 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE |
Function call | print ( countPairs ( arr , N ) ) NEW_LINE |
Comparator to sort the array in ascending order | def compare ( p1 , p2 ) : NEW_LINE INDENT return p1 [ 0 ] > p2 [ 0 ] NEW_LINE DEDENT |
Function to maximise the sum of the given array | def maximiseScore ( A , B , K , N ) : NEW_LINE |
Stores { A [ i ] , B [ i ] } pairs | pairs = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pairs . append ( [ A [ i ] , B [ i ] ] ) NEW_LINE DEDENT |
Sort in descending order of the values in the array A [ ] | pairs . sort ( key = lambda x : x [ 0 ] , reverse = True ) NEW_LINE |
Stores the maximum sum | Sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE |
If B [ i ] is equal to 0 | if ( pairs [ i ] [ 1 ] == 0 ) : NEW_LINE |
Simply add A [ i ] to the sum | Sum += pairs [ i ] [ 0 ] NEW_LINE elif ( pairs [ i ] [ 1 ] == 1 ) : NEW_LINE |
Add the highest K numbers | if ( K > 0 ) : NEW_LINE INDENT Sum += pairs [ i ] [ 0 ] NEW_LINE K -= 1 NEW_LINE DEDENT |
Subtract from the sum | else : NEW_LINE INDENT Sum -= pairs [ i ] [ 0 ] NEW_LINE DEDENT |
Return the sum | return Sum NEW_LINE |
Driver Code | A = [ 5 , 4 , 6 , 2 , 8 ] NEW_LINE B = [ 1 , 0 , 1 , 1 , 0 ] NEW_LINE K = 2 NEW_LINE N = len ( A ) NEW_LINE print ( maximiseScore ( A , B , K , N ) ) NEW_LINE |
Python3 program to implement the above approach | def sortString ( S ) : NEW_LINE INDENT sorted1 = [ ] NEW_LINE original = [ ] NEW_LINE insert = False NEW_LINE DEDENT |
For a single character | if ( len ( S ) == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT |
Stores count of repetitions of a character | curr = 1 NEW_LINE for i in range ( len ( S ) - 1 ) : NEW_LINE |
If repeating character | if ( S [ i ] == S [ i + 1 ] ) : NEW_LINE INDENT curr += 1 NEW_LINE insert = False NEW_LINE DEDENT |
Otherwise | else : NEW_LINE |
Store frequency | sorted1 . append ( curr ) NEW_LINE original . append ( curr ) NEW_LINE |
Reset count | curr = 1 NEW_LINE insert = True NEW_LINE |
Insert the last character block | if ( ( S [ ( len ( S ) - 1 ) ] != S [ ( len ( S ) - 2 ) ] ) or insert == False ) : NEW_LINE INDENT sorted1 . append ( curr ) NEW_LINE original . append ( curr ) NEW_LINE DEDENT |
Sort the frequencies | sorted1 . sort ( ) NEW_LINE |
Stores the minimum cost of all operations | t_cost = 0 NEW_LINE for i in range ( len ( sorted1 ) ) : NEW_LINE |
Store the absolute difference of i - th frequencies of ordered and unordered sequences | t_cost += abs ( sorted1 [ i ] - original [ i ] ) NEW_LINE |
Return the minimum cost | return ( t_cost // 2 ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aabbcccdeffffggghhhhhii " NEW_LINE print ( sortString ( S ) ) NEW_LINE DEDENT |
letters [ i ] stores the count of letters required to represent the digit i | letters = [ 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 ] NEW_LINE |
Function to return the sum of letters required to represent N | def sumOfLetters ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += letters [ n % 10 ] NEW_LINE n = n // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT |
Function to sort the array according to the sum of letters to represent n | def sortArr ( arr , n ) : NEW_LINE |
List to store the digit sum with respective elements | vp = [ ] NEW_LINE |
Inserting digit sum with elements in the list pair | for i in range ( n ) : NEW_LINE |
Making the vector pair | vp . append ( [ sumOfLetters ( arr [ i ] ) , arr [ i ] ] ) NEW_LINE |
Sort the list , this will sort the pair according to the sum of letters to represent n | vp . sort ( key = lambda x : x [ 0 ] ) NEW_LINE |
Print the sorted list content | for i in vp : NEW_LINE INDENT print ( i [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 10 , 31 , 18 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT |
Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays | def calculate_minimum_split ( a , k ) : NEW_LINE |
Array to store the differences between two adjacent elements | p = [ ] NEW_LINE n = len ( a ) NEW_LINE |
Iterating through the array | for i in range ( 1 , n ) : NEW_LINE |
Appending differences to p | p . append ( a [ i ] - a [ i - 1 ] ) NEW_LINE |
Sorting p in descending order | p . sort ( reverse = True ) NEW_LINE |
Sum of the first k - 1 values of p | min_sum = sum ( p [ : k - 1 ] ) NEW_LINE |
Computing the result | res = a [ n - 1 ] - a [ 0 ] - min_sum NEW_LINE return res NEW_LINE |
Python3 implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator | from functools import cmp_to_key NEW_LINE |
Comparator to sort the array of dates | def myCompare ( date1 , date2 ) : NEW_LINE INDENT day1 = date1 [ 0 : 2 ] NEW_LINE month1 = date1 [ 3 : 3 + 2 ] NEW_LINE year1 = date1 [ 6 : 6 + 4 ] NEW_LINE day2 = date2 [ 0 : 2 ] NEW_LINE month2 = date2 [ 3 : 3 + 2 ] NEW_LINE year2 = date2 [ 6 : 6 + 4 ] NEW_LINE DEDENT |
Condition to check the year | if ( year1 < year2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( year1 > year2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT |
Condition to check the month | if ( month1 < month2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( month1 > month2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT |
Condition to check the day | if ( day1 < day2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( day1 > day2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT |
Function that prints the dates in ascensding order | def printDatesAscending ( arr ) : NEW_LINE |
Sort the dates using library sort function with custom Comparator | arr = sorted ( arr , key = cmp_to_key ( lambda a , b : myCompare ( a , b ) ) ) NEW_LINE |
Loop to print the dates | for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE DEDENT |
Driver Code | arr = [ ] NEW_LINE arr . append ( "25-08-1996" ) NEW_LINE arr . append ( "03-08-1970" ) NEW_LINE arr . append ( "09-04-1994" ) NEW_LINE arr . append ( "29-08-1996" ) NEW_LINE arr . append ( "14-02-1972" ) NEW_LINE printDatesAscending ( arr ) NEW_LINE |
Function to check the Vowel | def isVowel ( ch ) : NEW_LINE INDENT ch = ch . upper ( ) ; NEW_LINE return ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) ; NEW_LINE DEDENT |
Returns count of vowels in str | def countVowels ( string ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE DEDENT |
Check for vowel | if ( isVowel ( string [ i ] ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT return count ; NEW_LINE |
Function to sort the array according to the number of the vowels | def sortArr ( arr , n ) : NEW_LINE |
Vector to store the number of vowels with respective elements | vp = [ ] ; NEW_LINE |
Inserting number of vowels with respective strings in the vector pair | for i in range ( n ) : NEW_LINE INDENT vp . append ( ( countVowels ( arr [ i ] ) , arr [ i ] ) ) ; NEW_LINE DEDENT |
Sort the vector , this will sort the pair according to the number of vowels | vp . sort ( ) NEW_LINE |
Print the sorted vector content | for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " lmno " , " pqrst " , " aeiou " , " xyz " ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sortArr ( arr , n ) ; NEW_LINE DEDENT |
Function to return the minimum cost | def minimumCost ( price , n ) : NEW_LINE |
Sort the price array | price = sorted ( price ) NEW_LINE totalCost = 0 NEW_LINE |
Calculate minimum price of n - 2 most costly person | for i in range ( n - 1 , 1 , - 2 ) : NEW_LINE INDENT if ( i == 2 ) : NEW_LINE INDENT totalCost += price [ 2 ] + price [ 0 ] NEW_LINE DEDENT else : NEW_LINE DEDENT |
Both the ways as discussed above | price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] NEW_LINE price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] NEW_LINE totalCost += min ( price_first , price_second ) NEW_LINE |
Calculate the minimum price of the two cheapest person | if ( n == 1 ) : NEW_LINE INDENT totalCost += price [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT totalCost += price [ 1 ] NEW_LINE DEDENT return totalCost NEW_LINE |
Driver code | price = [ 30 , 40 , 60 , 70 ] NEW_LINE n = len ( price ) NEW_LINE print ( minimumCost ( price , n ) ) NEW_LINE |
Python3 implementation of the above approach . | import numpy as np NEW_LINE N = 100 NEW_LINE INF = 1000000 NEW_LINE |
states of DP | dp = np . zeros ( ( N , N ) ) ; NEW_LINE vis = np . zeros ( ( N , N ) ) ; NEW_LINE |
function to find minimum sum | def findSum ( arr , n , k , l , r ) : NEW_LINE |
base - case | if ( ( l ) + ( n - 1 - r ) == k ) : NEW_LINE INDENT return arr [ r ] - arr [ l ] ; NEW_LINE DEDENT |
if state is solved before , return | if ( vis [ l ] [ r ] ) : NEW_LINE INDENT return dp [ l ] [ r ] ; NEW_LINE DEDENT |
marking the state as solved | vis [ l ] [ r ] = 1 ; NEW_LINE |
recurrence relation | dp [ l ] [ r ] = min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; NEW_LINE return dp [ l ] [ r ] NEW_LINE |
driver function | if __name__ == " _ _ main _ _ " : NEW_LINE |
input values | arr = [ 1 , 2 , 100 , 120 , 140 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE |
calling the required function ; | print ( findSum ( arr , n , k , 0 , n - 1 ) ) ; NEW_LINE |
Function to return the kth element in the modified array | def getNumber ( n , k ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.