text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Iterate while i is less than N | while i < N : NEW_LINE |
Add absolute difference of current element with k to ans | ans = ans + abs ( k - arr [ i ] ) NEW_LINE |
Increase i bye 1 | i += 1 NEW_LINE |
Return the value in ans2 | return ans // 2 NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given Input | arr = [ 5 , 4 , 1 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE |
Function Call | print ( find ( arr , N ) ) NEW_LINE |
Function to find minimum number of moves required to convert A into B | def minimumSteps ( a , b ) : NEW_LINE |
Stores the minimum number of moves required | cnt = 0 NEW_LINE |
Stores the absolute difference | a = abs ( a - b ) NEW_LINE |
FInd the number of moves | cnt = ( a // 5 ) + ( a % 5 ) // 2 + ( a % 5 ) % 2 NEW_LINE |
Return cnt | return cnt NEW_LINE |
Input | A = 3 NEW_LINE B = 9 NEW_LINE |
Function call | print ( minimumSteps ( A , B ) ) NEW_LINE |
Function to find lexicographically smallest string that is not a subsequence of S | def smallestNonSubsequence ( S , N ) : NEW_LINE |
Variable to store frequency of a | freq = 0 NEW_LINE |
Calculate frequency of a | for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' a ' ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT DEDENT |
Initialize string consisting of freq number of a | ans = " " NEW_LINE for i in range ( freq ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT |
Change the last digit to b | if ( freq == N ) : NEW_LINE INDENT ans = ans . replace ( ans [ freq - 1 ] , ' b ' ) NEW_LINE DEDENT |
Add another ' a ' to the string | else : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT |
Return tha answer string | return ans NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Input | S = " abcdefghijklmnopqrstuvwxyz " NEW_LINE N = len ( S ) NEW_LINE |
Function call | print ( smallestNonSubsequence ( S , N ) ) NEW_LINE |
Function to find all possible numbers that can be obtained by adding A or B to N exactly M times | def possibleNumbers ( N , M , A , B ) : NEW_LINE |
For maintaining increasing order | if ( A > B ) : NEW_LINE INDENT temp = A NEW_LINE A = B NEW_LINE B = temp NEW_LINE DEDENT |
Smallest number that can be obtained | number = N + M * A NEW_LINE print ( number , end = " β " ) NEW_LINE |
If A and B are equal , then only one number can be obtained , i . e . N + M * A | if ( A != B ) : NEW_LINE |
For finding others numbers , subtract A from number once and add B to number once | for i in range ( M ) : NEW_LINE INDENT number = number - A + B NEW_LINE print ( number , end = " β " ) NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given Input | N = 5 NEW_LINE M = 3 NEW_LINE A = 4 NEW_LINE B = 6 NEW_LINE |
Function Call | possibleNumbers ( N , M , A , B ) NEW_LINE |
Python3 program for the above approach | Pi = 3.141592 NEW_LINE |
Function to find the maximum number of buildings covered | def MaxBuildingsCovered ( arr , N , L ) : NEW_LINE |
Store the current sum | curr_sum = 0 NEW_LINE start = 0 NEW_LINE curr_count = 0 NEW_LINE max_count = 0 NEW_LINE |
Traverse the array | for i in range ( N ) : NEW_LINE |
Add the length of wire required for current building to cur_sum | curr_sum = curr_sum + ( arr [ i ] * Pi ) NEW_LINE |
Add extra unit distance 1 | if ( i != 0 ) : NEW_LINE INDENT curr_sum += 1 NEW_LINE DEDENT |
If curr_sum <= length of wire increment count by 1 | if ( curr_sum <= L ) : NEW_LINE INDENT curr_count += 1 NEW_LINE DEDENT |
If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum | elif ( curr_sum > L ) : NEW_LINE INDENT curr_sum = curr_sum - ( arr [ start ] * Pi ) NEW_LINE curr_sum -= 1 NEW_LINE start += 1 NEW_LINE curr_count -= 1 NEW_LINE DEDENT |
Update the max_count | max_count = max ( curr_count , max_count ) NEW_LINE |
Return the max_count | return max_count NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given Input | arr = [ 4 , 1 , 6 , 2 ] NEW_LINE L = 24 NEW_LINE |
Size of the array | N = len ( arr ) NEW_LINE |
Function Call | print ( MaxBuildingsCovered ( arr , N , L ) ) NEW_LINE |
Function to count number of unset bits in the integer N | def countUnsetBits ( N ) : NEW_LINE |
Stores the number of unset bits in N | c = 0 NEW_LINE while ( N ) : NEW_LINE |
Check if N is even | if ( N % 2 == 0 ) : NEW_LINE |
Increment the value of c | c += 1 NEW_LINE |
Right shift N by 1 | N = N >> 1 NEW_LINE |
Return the value of count of unset bits | return c NEW_LINE |
Function to count numbers whose Bitwise AND with N equal to 0 | def countBitwiseZero ( N ) : NEW_LINE |
Stores the number of unset bits in N | unsetBits = countUnsetBits ( N ) NEW_LINE |
Prthe value of 2 to the power of unsetBits | print ( ( 1 << unsetBits ) ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE countBitwiseZero ( N ) NEW_LINE DEDENT |
Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" | def isPossible ( s ) : NEW_LINE |
Store the size of the string | n = len ( s ) NEW_LINE |
Store the count of 0 s and 1 s | count_0 , count_1 = 0 , 0 NEW_LINE |
Traverse the given string in the forward direction | for i in range ( n ) : NEW_LINE |
If the character is '0' , increment count_0 by 1 | if ( s [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT |
If the character is '1' increment count_1 by 1 | else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT |
If at any point , count_1 > count_0 , return false | if ( count_1 > count_0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT |
If count_0 is not equal to twice count_1 , return false | if ( count_0 != ( 2 * count_1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT |
Reset the value of count_0 and count_1 | count_0 , count_1 = 0 , 0 NEW_LINE |
Traverse the string in the reverse direction | for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE |
If the character is '0' increment count_0 | if ( s [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT |
If the character is '1' increment count_1 | else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT |
If count_1 > count_0 , return false | if ( count_1 > count_0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given string | s = "010100" NEW_LINE |
Function Call | if ( isPossible ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Python 3 program for the above approach | from collections import defaultdict NEW_LINE |
Function to find a line across a wall such that it intersects minimum number of bricks | def leastBricks ( wall ) : NEW_LINE |
Declare a hashmap | map = defaultdict ( int ) NEW_LINE |
Store the maximum number of brick ending a point on x - axis | res = 0 NEW_LINE |
Iterate over all the rows | for list in wall : NEW_LINE |
Initialize width as 0 | width = 0 NEW_LINE |
Iterate over individual bricks | for i in range ( len ( list ) - 1 ) : NEW_LINE |
Add the width of the current brick to the total width | width += list [ i ] NEW_LINE |
Increment number of bricks ending at this width position | map [ width ] += 1 NEW_LINE |
Update the variable , res | res = max ( res , map [ width ] ) NEW_LINE |
Print the answer | print ( len ( wall ) - res ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given Input | arr = [ [ 1 , 2 , 2 , 1 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 2 ] , [ 2 , 4 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 1 , 1 ] ] NEW_LINE |
Function Call | leastBricks ( arr ) NEW_LINE |
Function to print the required permutation | def findPermutation ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( i , end = " β " ) NEW_LINE DEDENT print ( " " , β end β = β " " ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE findPermutation ( N ) NEW_LINE DEDENT |
Function to count distinct profits possible | def numberOfWays ( N , X , Y ) : NEW_LINE |
Stores the minimum total profit | S1 = ( N - 1 ) * X + Y NEW_LINE |
Stores the maximum total profit | S2 = ( N - 1 ) * Y + X NEW_LINE |
Return count of distinct profits | return ( S2 - S1 + 1 ) NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Input | N = 3 NEW_LINE X = 13 NEW_LINE Y = 15 NEW_LINE |
Function call | print ( numberOfWays ( N , X , Y ) ) NEW_LINE |
Function to find the minimum number of operations required to make all array elements equal | def MinimumOperations ( A , N , K ) : NEW_LINE |
Store the count of operations required | Count = 0 NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE |
Increment by K - 1 , as the last element will be used again for the next K consecutive elements | i = i + K - 1 NEW_LINE |
Increment count by 1 | Count += 1 NEW_LINE |
Return the result | return Count NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.