text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
pointer to tell whether we have reached the end of candy frequency array | k = 0 NEW_LINE |
Flag to tell if distribution is possible or not | Tf = True NEW_LINE for j in range ( mxage ) : NEW_LINE INDENT if ( fr1 [ j ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT |
Flag to tell if we can choose some candy packets for the students with age j | flag = False NEW_LINE while ( k < mxcandy ) : NEW_LINE |
If the quantity of packets is greater than or equal to the number of students of age j , then we can choose these packets for the students | if ( fr1 [ j ] <= fr2 [ k ] ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT k += 1 NEW_LINE |
Start searching from k + 1 in next operation | k = k + 1 NEW_LINE |
If we cannot choose any packets then the answer is NO | if ( flag == False ) : NEW_LINE INDENT Tf = False NEW_LINE break NEW_LINE DEDENT if ( Tf ) : NEW_LINE print ( " YES " ) NEW_LINE else : NEW_LINE print ( " NO " ) NEW_LINE |
Driver code | age = [ 5 , 15 , 10 ] NEW_LINE candy = [ 2 , 2 , 2 , 3 , 3 , 4 ] NEW_LINE n = len ( age ) NEW_LINE k = len ( candy ) NEW_LINE check_distribution ( n , k , age , candy ) NEW_LINE |
Function to find minimum number of 1 ' s β to β be β replaced β to β 0' s | def minChanges ( A , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( ( i - 1 >= 0 ) and A [ i - 1 ] == 1 and A [ i + 1 ] == 1 and A [ i ] == 0 ) : NEW_LINE INDENT A [ i + 1 ] = 0 NEW_LINE cnt = cnt + 1 NEW_LINE DEDENT DEDENT DEDENT |
return final answer | return cnt NEW_LINE |
Driver Code | A = [ 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 ] NEW_LINE n = len ( A ) NEW_LINE print ( minChanges ( A , n ) ) NEW_LINE |
Function to find number of closing brackets and complete a regular bracket sequence | def completeSequence ( s ) : NEW_LINE |
Finding the length of sequence | n = len ( s ) NEW_LINE open = 0 NEW_LINE close = 0 NEW_LINE for i in range ( n ) : NEW_LINE |
Counting opening brackets | if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT open += 1 NEW_LINE DEDENT else : NEW_LINE |
Counting closing brackets | close += 1 NEW_LINE |
Checking if at any position the number of closing bracket is more then answer is impossible | if ( close > open ) : NEW_LINE INDENT print ( " IMPOSSIBLE " ) NEW_LINE return NEW_LINE DEDENT |
If possible , print ' s ' and required closing brackets . | print ( s , end = " " ) NEW_LINE for i in range ( open - close ) : NEW_LINE INDENT print ( " ) " , end = " " ) NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ( ( ) ( ( ) ( " NEW_LINE completeSequence ( s ) NEW_LINE DEDENT |
Function to print the smallest permutation | def smallestPermute ( n ) : NEW_LINE INDENT res = [ " " ] * ( n + 1 ) NEW_LINE DEDENT |
when n is even | if ( n % 2 == 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res [ i ] = chr ( 48 + i + 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = chr ( 48 + i ) NEW_LINE DEDENT DEDENT DEDENT |
when n is odd | else : NEW_LINE INDENT for i in range ( n - 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res [ i ] = chr ( 48 + i + 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = chr ( 48 + i ) NEW_LINE DEDENT DEDENT DEDENT |
handling last 3 digit | res [ n - 1 ] = chr ( 48 + n - 2 ) NEW_LINE res [ n - 2 ] = chr ( 48 + n ) NEW_LINE res [ n - 3 ] = chr ( 48 + n - 1 ) NEW_LINE |
add EOL and print result | res = ' ' . join ( res ) NEW_LINE return res NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 NEW_LINE print ( smallestPermute ( n ) ) NEW_LINE DEDENT |
Python3 implementation of above approach | import math NEW_LINE |
Function to return minimum number of insertions required | def minInsertions ( H , n , K ) : NEW_LINE |
Initialize insertions to 0 | inser = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT diff = abs ( H [ i ] - H [ i - 1 ] ) ; NEW_LINE if ( diff <= K ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT else : NEW_LINE INDENT inser += math . ceil ( diff / K ) - 1 ; NEW_LINE DEDENT DEDENT |
return total insertions | return inser ; NEW_LINE |
Driver Code | H = [ 2 , 4 , 8 , 16 ] ; NEW_LINE K = 3 ; NEW_LINE n = len ( H ) ; NEW_LINE print ( minInsertions ( H , n , K ) ) ; NEW_LINE |
Function that returns the minimum number of operations to be performed to reduce the number to 1 | def count_minimum_operations ( n ) : NEW_LINE |
To stores the total number of operations to be performed | count = 0 NEW_LINE while ( n > 1 ) : NEW_LINE |
if n is divisible by 3 then reduce it to n / 3 | if ( n % 3 == 0 ) : NEW_LINE INDENT n //= 3 NEW_LINE DEDENT |
if n modulo 3 is 1 decrement it by 1 | elif ( n % 3 == 1 ) : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( n == 2 ) : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT DEDENT |
if n modulo 3 is 2 then increment it by 1 | else : NEW_LINE INDENT n += 1 NEW_LINE DEDENT |
update the counter | count += 1 NEW_LINE return count NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE ans = count_minimum_operations ( n ) NEW_LINE print ( ans ) NEW_LINE DEDENT |
Function that returns the minimum number of operations to be performed to reduce the number to 1 | def count_minimum_operations ( n ) : NEW_LINE |
Base cases | if ( n == 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n % 3 == 0 ) : NEW_LINE INDENT return 1 + count_minimum_operations ( n / 3 ) NEW_LINE DEDENT elif ( n % 3 == 1 ) : NEW_LINE INDENT return 1 + count_minimum_operations ( n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + count_minimum_operations ( n + 1 ) NEW_LINE DEDENT |
Driver Code | n = 4 NEW_LINE ans = count_minimum_operations ( n ) NEW_LINE print ( ans ) NEW_LINE |
Python implementation of the approach | def maxSum ( arr , n ) : NEW_LINE |
To store sum | s = 0 NEW_LINE |
To store ending indices of the chosen prefix arrays | l = [ ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE |
Adding the absolute value of a [ i ] | s += abs ( a [ i ] ) NEW_LINE if ( a [ i ] >= 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT |
If i == 0 then there is no index to be flipped in ( i - 1 ) position | if ( i == 0 ) : NEW_LINE INDENT l . append ( i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT l . append ( i + 1 ) NEW_LINE l . append ( i ) NEW_LINE DEDENT |
print the maximised sum | print ( s ) NEW_LINE |
print the ending indices of the chosen prefix arrays | print ( * l ) NEW_LINE |
Driver Code | n = 4 NEW_LINE a = [ 1 , - 2 , - 3 , 4 ] NEW_LINE maxSum ( a , n ) NEW_LINE |
Python program to find the longest common prefix between two strings after performing swaps on the second string | def LengthLCP ( x , y ) : NEW_LINE INDENT fr = [ 0 ] * 26 NEW_LINE DEDENT |
a = len ( x ) length of x b = len ( y ) length of y | for i in range ( b ) : NEW_LINE |
creating frequency array of characters of y | fr [ ord ( y [ i ] ) - 97 ] += 1 NEW_LINE |
storing the length of longest common prefix | c = 0 NEW_LINE for i in range ( a ) : NEW_LINE |
checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 | if ( fr [ ord ( x [ i ] ) - 97 ] > 0 ) : NEW_LINE INDENT c += 1 NEW_LINE fr [ ord ( x [ i ] ) - 97 ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT print ( c ) NEW_LINE |
Driver Code | x , y = " here " , " there " NEW_LINE LengthLCP ( x , y ) NEW_LINE |
Function to count possible pairs | def CountPair ( L , R ) : NEW_LINE |
total count of numbers in range | x = ( R - L + 1 ) NEW_LINE |
printing count of pairs | print ( x // 2 ) NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 1 , 8 NEW_LINE CountPair ( L , R ) NEW_LINE DEDENT |
Function to find problems not solved at the end of Nth day | def problemsLeft ( K , P , N ) : NEW_LINE INDENT if ( K <= P ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( K - P ) * N ) NEW_LINE DEDENT DEDENT |
Driver Code | K , P , N = 4 , 1 , 10 NEW_LINE print ( problemsLeft ( K , P , N ) ) NEW_LINE |
Find set of vertex i | def find ( i ) : NEW_LINE INDENT while parent [ i ] != i : NEW_LINE INDENT i = parent [ i ] NEW_LINE DEDENT return i NEW_LINE DEDENT |
Does union of i and j . It returns false if i and j are already in same set . | def union ( i , j ) : NEW_LINE INDENT a = find ( i ) NEW_LINE b = find ( j ) NEW_LINE parent [ a ] = b NEW_LINE DEDENT |
Finds MST using Kruskal 's algorithm | def kruskalMST ( cost ) : NEW_LINE |
Initialize sets of disjoint sets | for i in range ( V ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE DEDENT |
Include minimum weight edges one by one | edge_count = 0 NEW_LINE while edge_count < V - 1 : NEW_LINE INDENT min = INF NEW_LINE a = - 1 NEW_LINE b = - 1 NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT if find ( i ) != find ( j ) and cost [ i ] [ j ] < min : NEW_LINE INDENT min = cost [ i ] [ j ] NEW_LINE a = i NEW_LINE b = j NEW_LINE DEDENT DEDENT DEDENT union ( a , b ) NEW_LINE print ( ' Edge β { } : ( { } , β { } ) β cost : { } ' . format ( edge_count , a , b , min ) ) NEW_LINE edge_count += 1 NEW_LINE mincost += min NEW_LINE DEDENT print ( " Minimum β cost = β { } " . format ( mincost ) ) NEW_LINE |
Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 | V = 5 NEW_LINE parent = [ i for i in range ( V ) ] NEW_LINE INF = float ( ' inf ' ) NEW_LINE cost = [ [ INF , 2 , INF , 6 , INF ] , [ 2 , INF , 3 , 8 , 5 ] , [ INF , 3 , INF , INF , 7 ] , [ 6 , 8 , INF , INF , 9 ] , [ INF , 5 , 7 , 9 , INF ] ] NEW_LINE |
Print the solution | kruskalMST ( cost ) NEW_LINE |
Function to find the chocolates left | def results ( n , k ) : NEW_LINE INDENT return round ( pow ( n , ( 1.0 / pow ( 2 , k ) ) ) ) NEW_LINE DEDENT |
Driver code | k = 3 NEW_LINE n = 100000000 NEW_LINE print ( " Chocolates β left β after " ) , NEW_LINE print ( k ) , NEW_LINE print ( " iterations β are " ) , NEW_LINE print ( int ( results ( n , k ) ) ) NEW_LINE |
Python Code to find sub - array whose sum shows the minimum deviation | def getSubArray ( arr , n , K ) : NEW_LINE INDENT i = - 1 NEW_LINE j = - 1 NEW_LINE currSum = 0 NEW_LINE DEDENT |
starting index , ending index , Deviation | result = [ i , j , abs ( K - abs ( currSum ) ) ] NEW_LINE |
iterate i and j to get all subarrays | for i in range ( 0 , n ) : NEW_LINE INDENT currSum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT currSum += arr [ j ] NEW_LINE currDev = abs ( K - abs ( currSum ) ) NEW_LINE DEDENT DEDENT |
found sub - array with less sum | if ( currDev < result [ 2 ] ) : NEW_LINE INDENT result = [ i , j , currDev ] NEW_LINE DEDENT |
exactly same sum | if ( currDev == 0 ) : NEW_LINE INDENT return result NEW_LINE DEDENT return result NEW_LINE |
Driver Code | def main ( ) : NEW_LINE INDENT arr = [ 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 ] NEW_LINE n = len ( arr ) NEW_LINE K = 50 NEW_LINE DEDENT |
Array to store return values | [ i , j , minDev ] = getSubArray ( arr , n , K ) NEW_LINE if ( i == - 1 ) : NEW_LINE INDENT print ( " The β empty β array β shows β minimum β Deviation " ) NEW_LINE return 0 NEW_LINE DEDENT for i in range ( i , j + 1 ) : NEW_LINE INDENT print arr [ i ] , NEW_LINE DEDENT main ( ) NEW_LINE |
Python3 program to perform Q queries to find longest subsequence whose average is less than K | import bisect NEW_LINE |
Function to print the length for evey query | def longestSubsequence ( a , n , q , m ) : NEW_LINE |
sort array of N elements | a . sort ( ) NEW_LINE Sum = 0 NEW_LINE |
Array to store average from left | b = [ None ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE av = Sum // ( i + 1 ) NEW_LINE b [ i ] = av + 1 NEW_LINE DEDENT |
Sort array of average | b . sort ( ) NEW_LINE |
number of queries | for i in range ( 0 , m ) : NEW_LINE INDENT k = q [ i ] NEW_LINE DEDENT |
print answer to every query using binary search | longest = bisect . bisect_right ( b , k ) NEW_LINE print ( " Answer β to β Query " , i + 1 , " : " , longest ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 2 , 5 , 4 ] NEW_LINE n = len ( a ) NEW_LINE DEDENT |
4 queries | q = [ 4 , 2 , 1 , 5 ] NEW_LINE m = len ( q ) NEW_LINE longestSubsequence ( a , n , q , m ) NEW_LINE |
Returns the minimum steps required to make an array of N elements equal , where the first array element equals M | def steps ( N , M ) : NEW_LINE |
Corner Case 1 : When N = 1 | if ( N == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Corner Case 2 : When N = 2 | elif ( N == 2 ) : NEW_LINE INDENT return M NEW_LINE DEDENT return 2 * M + ( N - 3 ) NEW_LINE |
Driver Code | N = 4 NEW_LINE M = 4 NEW_LINE print ( steps ( N , M ) ) NEW_LINE |
Python3 code to count the change required to convert the array into non - increasing array | from queue import PriorityQueue NEW_LINE def DecreasingArray ( a , n ) : NEW_LINE INDENT ss , dif = ( 0 , 0 ) NEW_LINE DEDENT |
min heap | pq = PriorityQueue ( ) NEW_LINE |
Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference | for i in range ( n ) : NEW_LINE INDENT tmp = 0 NEW_LINE if not pq . empty ( ) : NEW_LINE INDENT tmp = pq . get ( ) NEW_LINE pq . put ( tmp ) NEW_LINE DEDENT if not pq . empty ( ) and tmp < a [ i ] : NEW_LINE INDENT dif = a [ i ] - tmp NEW_LINE ss += dif NEW_LINE pq . get ( ) NEW_LINE DEDENT pq . put ( a [ i ] ) NEW_LINE DEDENT return ss NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 1 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( DecreasingArray ( a , n ) ) NEW_LINE DEDENT |
Function to find new array a | def solve ( a , b , n ) : NEW_LINE INDENT s = 0 NEW_LINE DEDENT |
find sum S of both arrays a and b . | for i in range ( 0 , n ) : NEW_LINE INDENT s += a [ i ] + b [ i ] NEW_LINE DEDENT |
Single element case . | if n == 1 : NEW_LINE INDENT return a [ 0 ] + b [ 0 ] NEW_LINE DEDENT |
This checks whether sum s can be divided equally between all array elements . i . e . whether all elements can take equal value or not . | if s % n != 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
Compute possible value of new array elements . | x = s // n NEW_LINE for i in range ( 0 , n ) : NEW_LINE |
Possibility 1 | if a [ i ] > x : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
ensuring that all elements of array b are used . | if i > 0 : NEW_LINE INDENT a [ i ] += b [ i - 1 ] NEW_LINE b [ i - 1 ] = 0 NEW_LINE DEDENT |
If a ( i ) already updated to x move to next element in array a . | if a [ i ] == x : NEW_LINE INDENT continue NEW_LINE DEDENT |
Possibility 2 | y = a [ i ] + b [ i ] NEW_LINE if i + 1 < n : NEW_LINE INDENT y += b [ i + 1 ] NEW_LINE DEDENT if y == x : NEW_LINE INDENT a [ i ] = y NEW_LINE b [ i ] = 0 NEW_LINE if i + 1 < n : b [ i + 1 ] = 0 NEW_LINE continue NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.