text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
If container gets filled | if ( cont [ i ] [ j ] >= 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT |
Dividing the liquid equally in two halves | cont [ i + 1 ] [ j ] += ( cont [ i ] [ j ] - 1 ) / 2 NEW_LINE cont [ i + 1 ] [ j + 1 ] += ( cont [ i ] [ j ] - 1 ) / 2 NEW_LINE print ( count ) NEW_LINE |
Driver code | n = 3 NEW_LINE x = 5 NEW_LINE num_of_containers ( n , x ) NEW_LINE |
Python3 program for the above approach | import sys NEW_LINE |
Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] | def findTriplet ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE st = [ ] NEW_LINE DEDENT |
Initialize the heights of h1 and h3 to INT_MAX and INT_MIN respectively | h3 = - sys . maxsize - 1 NEW_LINE h1 = sys . maxsize NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE |
Store the current element as h1 | h1 = arr [ i ] NEW_LINE |
If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 | while ( len ( st ) > 0 and st [ - 1 ] < arr [ i ] ) : NEW_LINE INDENT h3 = st [ - 1 ] NEW_LINE del st [ - 1 ] NEW_LINE DEDENT |
Push the current element on the stack | st . append ( arr [ i ] ) NEW_LINE |
If current element is less than h3 , then we found such triplet and return true | if ( h1 < h3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT |
No triplet found , hence return false | return False NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given array | arr = [ 4 , 7 , 5 , 6 ] NEW_LINE |
Function Call | if ( findTriplet ( arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Function to return minimum distinct character after M removals | def distinctNumbers ( arr , m , n ) : NEW_LINE INDENT count = { } NEW_LINE DEDENT |
Count the occurences of number and store in count | for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] = count . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT |
Count the occurences of the frequencies | fre_arr = [ 0 ] * ( n + 1 ) NEW_LINE for it in count : NEW_LINE INDENT fre_arr [ count [ it ] ] += 1 NEW_LINE DEDENT |
Take answer as total unique numbers and remove the frequency and subtract the answer | ans = len ( count ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = fre_arr [ i ] NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT |
Remove the minimum number of times | t = min ( temp , m // i ) NEW_LINE ans -= t NEW_LINE m -= i * t NEW_LINE |
Return the answer | return ans NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Initialize array | arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] NEW_LINE |
Size of array | n = len ( arr ) NEW_LINE m = 2 NEW_LINE |
Function call | print ( distinctNumbers ( arr , m , n ) ) NEW_LINE |
Python3 implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix | M = 4 NEW_LINE N = 5 NEW_LINE |
Function to find the minimum number of moves to bring all elements in one cell of matrix | def no_of_moves ( Matrix , x , y ) : NEW_LINE |
Moves variable to store the sum of number of moves | moves = 0 NEW_LINE |
Loop to count the number of the moves | for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE DEDENT |
Condition to check that the current cell is a non - zero element | if ( Matrix [ i ] [ j ] != 0 ) : NEW_LINE INDENT moves += abs ( x - i ) NEW_LINE moves += abs ( y - j ) NEW_LINE DEDENT print ( moves ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Coordinates of given cell | x = 3 NEW_LINE y = 2 NEW_LINE |
Given Matrix | Matrix = [ [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] ] NEW_LINE |
Element to be moved | num = 1 NEW_LINE |
Function call | no_of_moves ( Matrix , x , y ) NEW_LINE |
Function to check if it is possible to remove all array elements | def removeAll ( arr , n ) : NEW_LINE |
Condition if we can remove all elements from the array | INDENT if arr [ 0 ] < arr [ n - 1 ] : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT |
Driver code | arr = [ 10 , 4 , 7 , 1 , 3 , 6 ] NEW_LINE removeAll ( arr , len ( arr ) ) NEW_LINE |
Python3 program for the above approach | import sys NEW_LINE |
Function to find subarray | def FindSubarray ( arr , n ) : NEW_LINE |
If the array has only one element , then there is no answer . | if ( n == 1 ) : NEW_LINE INDENT print ( " No β such β subarray ! " ) NEW_LINE DEDENT |
Array to store the last occurrences of the elements of the array . | vis = [ - 1 ] * ( n + 1 ) NEW_LINE vis [ arr [ 0 ] ] = 0 NEW_LINE |
To maintain the length | length = sys . maxsize NEW_LINE |
Variables to store start and end indices | flag = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT t = arr [ i ] NEW_LINE DEDENT |
Check if element is occurring for the second time in the array | if ( vis [ t ] != - 1 ) : NEW_LINE |
Find distance between last and current index of the element . | distance = i - vis [ t ] + 1 NEW_LINE |
If the current distance is less then len update len and set ' start ' and 'end | ' NEW_LINE INDENT if ( distance < length ) : NEW_LINE INDENT length = distance NEW_LINE start = vis [ t ] NEW_LINE end = i NEW_LINE DEDENT flag = 1 NEW_LINE DEDENT |
Set the last occurrence of current element to be ' i ' . | vis [ t ] = i NEW_LINE |
If flag is equal to 0 , it means there is no answer . | if ( flag == 0 ) : NEW_LINE INDENT print ( " No β such β subarray ! " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT DEDENT |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 2 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE FindSubarray ( arr , n ) NEW_LINE DEDENT |
Function to find the substrings in binary string such that every character belongs to a palindrome | def countSubstrings ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT |
Total substrings | answer = ( n * ( n - 1 ) ) // 2 NEW_LINE cnt = 1 NEW_LINE v = [ ] NEW_LINE |
Loop to store the count of continuous characters in the given string | for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( cnt ) NEW_LINE cnt = 1 NEW_LINE DEDENT DEDENT if ( cnt > 0 ) : NEW_LINE INDENT v . append ( cnt ) NEW_LINE DEDENT |
Subtract non special strings from answer | for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT answer -= ( v [ i ] + v [ i + 1 ] - 1 ) NEW_LINE DEDENT return answer NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given string | s = "00111" NEW_LINE |
Function call | print ( countSubstrings ( s ) ) NEW_LINE |
Function to get the required minutes | def get_palindrome_time ( str ) : NEW_LINE |
Storing hour and minute value in integral form | hh = ( ( ord ( str [ 0 ] ) - 48 ) * 10 + ( ord ( str [ 1 ] ) - 48 ) ) NEW_LINE mm = ( ( ord ( str [ 3 ] ) - 48 ) * 10 + ( ord ( str [ 4 ] ) - 48 ) ) NEW_LINE requiredTime = 0 NEW_LINE |
Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute | while ( hh % 10 != mm // 10 or hh // 10 != mm % 10 ) : NEW_LINE INDENT mm += 1 NEW_LINE DEDENT |
If mins is 60 , increase hour , and reinitilialized to 0 | if ( mm == 60 ) : NEW_LINE INDENT mm = 0 NEW_LINE hh += 1 NEW_LINE DEDENT |
If hours is 60 , reinitialized to 0 | if ( hh == 24 ) : NEW_LINE INDENT hh = 0 NEW_LINE DEDENT requiredTime += 1 ; NEW_LINE |
Return the required time | return requiredTime NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE |
Given Time as a string | str = "05:39" ; NEW_LINE |
Function call | print ( get_palindrome_time ( str ) ) ; NEW_LINE |
Function to find the maximum sum of all subarrays | def maximumSubarraySum ( a , n , subarrays ) : NEW_LINE |
Initialize maxsum and prefixArray | maxsum = 0 NEW_LINE prefixArray = [ 0 ] * n NEW_LINE |
Find the frequency using prefix Array | for i in range ( len ( subarrays ) ) : NEW_LINE INDENT prefixArray [ subarrays [ i ] [ 0 ] - 1 ] += 1 NEW_LINE prefixArray [ subarrays [ i ] [ 1 ] ] -= 1 NEW_LINE DEDENT |
Perform prefix sum | for i in range ( 1 , n ) : NEW_LINE INDENT prefixArray [ i ] += prefixArray [ i - 1 ] NEW_LINE DEDENT |
Sort both arrays to get a greedy result | prefixArray . sort ( ) NEW_LINE prefixArray . reverse ( ) NEW_LINE a . sort ( ) NEW_LINE a . reverse ( ) NEW_LINE |
Finally multiply largest frequency with largest array element . | for i in range ( n ) : NEW_LINE INDENT maxsum += a [ i ] * prefixArray [ i ] NEW_LINE DEDENT |
Return the answer | return maxsum NEW_LINE |
Driver Code | n = 6 NEW_LINE |
Initial Array | a = [ 4 , 1 , 2 , 1 , 9 , 2 ] NEW_LINE |
Subarrays | subarrays = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 3 , 4 ] ] NEW_LINE |
Function Call | print ( maximumSubarraySum ( a , n , subarrays ) ) NEW_LINE |
Function to find the maximum profit from the given values | def maxProfit ( value , N , K ) : NEW_LINE INDENT value . sort ( ) NEW_LINE maxval = value [ N - 1 ] NEW_LINE maxProfit = 0 NEW_LINE DEDENT |
Iterating over every possible permutation | while True : NEW_LINE INDENT curr_val = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr_val += value [ i ] NEW_LINE if ( curr_val <= K ) : NEW_LINE INDENT maxProfit = max ( curr_val + maxval * ( i + 1 ) , maxProfit ) NEW_LINE DEDENT DEDENT if not next_permutation ( value ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return maxProfit NEW_LINE def next_permutation ( p ) : NEW_LINE for a in range ( len ( p ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT if p [ a ] < p [ a + 1 ] : NEW_LINE INDENT b = len ( p ) - 1 NEW_LINE while True : NEW_LINE INDENT if p [ b ] > p [ a ] : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b = len ( p ) - 1 NEW_LINE while a < b : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT b -= 1 NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE |
Driver Code | N , K = 4 , 6 NEW_LINE values = [ 5 , 2 , 7 , 3 ] NEW_LINE |
Function Call | print ( maxProfit ( values , N , K ) ) NEW_LINE |
Python3 program for the above approach | import math NEW_LINE |
Function to reduce N to its minimum possible value by the given operations | def MinValue ( n ) : NEW_LINE |
Keep replacing n until is an integer | while ( int ( math . sqrt ( n ) ) == math . sqrt ( n ) and n > 1 ) : NEW_LINE INDENT n = math . sqrt ( n ) NEW_LINE DEDENT |
Keep replacing n until n is divisible by i * i | for i in range ( int ( math . sqrt ( n ) ) , 1 , - 1 ) : NEW_LINE INDENT while ( n % ( i * i ) == 0 ) : NEW_LINE INDENT n /= i NEW_LINE DEDENT DEDENT |
Print the answer | print ( n ) NEW_LINE |
Given N | n = 20 NEW_LINE |
Function call | MinValue ( n ) NEW_LINE |
Function to check whether the substring from l to r is palindrome or not | def isPalindrome ( l , r , s ) : NEW_LINE INDENT while ( l <= r ) : NEW_LINE DEDENT |
If characters at l and r differ | if ( s [ l ] != s [ r ] ) : NEW_LINE |
Not a palindrome | return bool ( False ) NEW_LINE l += 1 NEW_LINE r -= 1 NEW_LINE |
If the string is a palindrome | return bool ( True ) NEW_LINE |
Function to count and return the number of possible splits | def numWays ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT |
Stores the count of splits | ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE |
Check if the two substrings after the split are palindromic or not | if ( isPalindrome ( 0 , i , s ) and isPalindrome ( i + 1 , n - 1 , s ) ) : NEW_LINE |
If both are palindromes | ans += 1 NEW_LINE |
Print the final count | return ans NEW_LINE |
Driver Code | S = " aaaaa " NEW_LINE print ( numWays ( S ) ) NEW_LINE |
Function to find the direction when stopped moving | def findDirection ( n , m ) : NEW_LINE INDENT if ( n > m ) : NEW_LINE INDENT if ( m % 2 == 0 ) : NEW_LINE INDENT print ( " Up " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Down " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " Left " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Right " ) ; NEW_LINE DEDENT DEDENT DEDENT |
Given size of NxM grid | n = 3 ; m = 3 ; NEW_LINE |
Function Call | findDirection ( n , m ) ; NEW_LINE |
Function to return the maximum sum of modulus with every array element | def maxModulosum ( a , n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.