text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Calling sieve function | sieve ( ) NEW_LINE |
Given N | N = 7 NEW_LINE |
Function call | numberOfWays ( N ) NEW_LINE |
Function to find the maximum sum of squares of stack elements | def maxSumOfSquares ( N , S ) : NEW_LINE |
Stores the sum ofsquares of stack elements | res = 0 NEW_LINE |
Check if sum is valid | if ( S < N or S > 9 * N ) : NEW_LINE INDENT cout << - 1 ; NEW_LINE return NEW_LINE DEDENT |
Initialize all stack elements with 1 | S = S - N NEW_LINE |
Stores the count the number of stack elements not equal to 1 | c = 0 NEW_LINE |
Add the sum of squares of stack elements not equal to 1 | while ( S > 0 ) : NEW_LINE INDENT c += 1 NEW_LINE if ( S // 8 > 0 ) : NEW_LINE INDENT res += 9 * 9 NEW_LINE S -= 8 NEW_LINE DEDENT else : NEW_LINE INDENT res += ( S + 1 ) * ( S + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT |
Add 1 * 1 to res as the remaining stack elements are 1 | res = res + ( N - c ) NEW_LINE |
Print the result | print ( res ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE S = 12 NEW_LINE DEDENT |
Function call | maxSumOfSquares ( N , S ) NEW_LINE |
Function to return the value after unsetting K LSBs | def clearLastBit ( N , K ) : NEW_LINE |
Create a mask | mask = ( - 1 << K + 1 ) NEW_LINE |
Bitwise AND operation with the number and the mask | N = N & mask NEW_LINE return N NEW_LINE |
Given N and K | N = 730 NEW_LINE K = 3 NEW_LINE |
Function call | print ( clearLastBit ( N , K ) ) NEW_LINE |
Function to check whether it is possible to do the operation or not | def isPossible ( r , b , g ) : NEW_LINE |
Calculate modulo 3 of all the colors | r = r % 3 NEW_LINE b = b % 3 NEW_LINE g = g % 3 NEW_LINE |
Check for any equal pair | if ( r == b or b == g or g == r ) : NEW_LINE INDENT return True NEW_LINE DEDENT |
Otherwise | else : NEW_LINE INDENT return False NEW_LINE DEDENT |
Given colors | R = 1 NEW_LINE B = 3 NEW_LINE G = 6 NEW_LINE |
Function call | if ( isPossible ( R , B , G ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Function to calculate the minimum number of steps | def calculate_steps ( arr , n , minimum ) : NEW_LINE |
count stores number of operations required to make all elements equal to minimum value | count = 0 NEW_LINE |
Remark , the array should remain unchanged for further calculations with different minimum | for i in range ( n ) : NEW_LINE |
Storing the current value of arr [ i ] in val | val = arr [ i ] NEW_LINE if ( arr [ i ] > minimum ) : NEW_LINE |
Finds how much extra amount is to be removed | arr [ i ] = arr [ i ] - minimum NEW_LINE |
Subtract the maximum number of 5 and stores remaining | count += arr [ i ] // 5 NEW_LINE arr [ i ] = arr [ i ] % 5 NEW_LINE |
Subtract the maximum number of 2 and stores remaining | count += arr [ i ] // 2 NEW_LINE arr [ i ] = arr [ i ] % 2 NEW_LINE if ( arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT |
Restores the actual value of arr [ i ] | arr [ i ] = val NEW_LINE |
Return the count | return count NEW_LINE |
Function to find the minimum number of steps to make array elements same | def solve ( arr , n ) : NEW_LINE |
Sort the array in descending order | arr = sorted ( arr ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE |
Stores the minimum array element | minimum = arr [ n - 1 ] NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE count3 = 0 NEW_LINE |
Stores the operations required to make array elements equal to minimum | count1 = calculate_steps ( arr , n , minimum ) NEW_LINE |
Stores the operations required to make array elements equal to minimum - 1 | count2 = calculate_steps ( arr , n , minimum - 1 ) NEW_LINE |
Stores the operations required to make array elements equal to minimum - 2 | count3 = calculate_steps ( arr , n , minimum - 2 ) NEW_LINE |
Return minimum of the three counts | return min ( count1 , min ( count2 , count3 ) ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( solve ( arr , N ) ) NEW_LINE DEDENT |
Python3 program for the above approach | from collections import defaultdict NEW_LINE |
Function to find largest palindrome possible from S and P after rearranging characters to make palindromic string T | def mergePalindromes ( S , P ) : NEW_LINE |
Using unordered_map to store frequency of elements mapT will have freq of elements of T | mapS = defaultdict ( lambda : 0 ) NEW_LINE mapP = defaultdict ( lambda : 0 ) NEW_LINE mapT = defaultdict ( lambda : 0 ) NEW_LINE |
Size of both the strings | n = len ( S ) NEW_LINE m = len ( P ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapS [ ord ( S [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT mapP [ ord ( P [ i ] ) ] += 1 NEW_LINE DEDENT |
Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map | for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] % 2 == 0 ) : NEW_LINE INDENT mapT [ i ] += mapS [ i ] NEW_LINE mapS [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT mapT [ i ] += mapS [ i ] - 1 NEW_LINE mapS [ i ] = 1 NEW_LINE DEDENT if ( mapP [ i ] % 2 == 0 ) : NEW_LINE INDENT mapT [ i ] += mapP [ i ] NEW_LINE mapP [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT mapT [ i ] += mapP [ i ] - 1 NEW_LINE mapP [ i ] = 1 NEW_LINE DEDENT DEDENT |
Check if a unique character is present in both string S and P | check = 0 NEW_LINE for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] > 0 and mapP [ i ] > 0 ) : NEW_LINE INDENT mapT [ i ] += 2 NEW_LINE check = 1 NEW_LINE break NEW_LINE DEDENT DEDENT |
Making string T in two halves half1 - first half half2 - second half | half1 , half2 = " " , " " NEW_LINE for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( ( 2 * j ) < mapT [ i ] ) : NEW_LINE INDENT half1 += chr ( i ) NEW_LINE half2 += chr ( i ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT |
Reverse the half2 to attach with half1 to make palindrome | half2 = half2 [ : : - 1 ] NEW_LINE |
If same unique element is present in both S and P , then taking that only which is already taken through mapT | if ( check ) : NEW_LINE INDENT return half1 + half2 NEW_LINE DEDENT |
If same unique element is not present in S and P , then take characters that make string T lexicographically smallest | for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] > 0 or mapP [ i ] > 0 ) : NEW_LINE INDENT half1 += chr ( i ) NEW_LINE return half1 + half2 NEW_LINE DEDENT DEDENT |
If no unique element is present in both string S and P | return half1 + half2 NEW_LINE |
Given string S and P | S = " aeabb " NEW_LINE P = " dfedf " NEW_LINE |
Function call | print ( mergePalindromes ( S , P ) ) NEW_LINE |
Python3 program for the above approach | from collections import defaultdict NEW_LINE |
Function that counts the subarrays with sum of its elements as its length | def countOfSubarray ( arr , N ) : NEW_LINE |
Decrementing all the elements of the array by 1 | for i in range ( N ) : NEW_LINE INDENT arr [ i ] -= 1 NEW_LINE DEDENT |
Making prefix sum array | pref = [ 0 ] * N NEW_LINE pref [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + arr [ i ] NEW_LINE DEDENT |
Declare map to store count of elements upto current element | mp = defaultdict ( lambda : 0 ) NEW_LINE answer = 0 NEW_LINE |
To count all the subarrays whose prefix sum is 0 | mp [ 0 ] += 1 NEW_LINE |
Iterate the array | for i in range ( N ) : NEW_LINE |
Increment answer by count of current element of prefix array | answer += mp [ pref [ i ] ] NEW_LINE mp [ pref [ i ] ] += 1 NEW_LINE |
Return the answer | return answer NEW_LINE |
Given array arr [ ] | arr = [ 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE |
Function call | print ( countOfSubarray ( arr , N ) ) NEW_LINE |
Python3 program to print subset at the nth position ordered by the sum of the elements | import math NEW_LINE |
Function to print the elements of the subset at pos n | def printsubset ( n , k ) : NEW_LINE |
Initialize count = 0 and x = 0 | count = 0 NEW_LINE x = 0 NEW_LINE |
Create a vector for storing the elements of subsets | vec = [ ] NEW_LINE |
Doing until all the set bits of n are used | while ( n > 0 ) : NEW_LINE INDENT x = n & 1 NEW_LINE DEDENT |
This part is executed only when the last bit is set | if ( x ) : NEW_LINE INDENT vec . append ( pow ( k , count ) ) NEW_LINE DEDENT |
Right shift the bit by one position | n = n >> 1 NEW_LINE |
Increasing the count each time by one | count += 1 NEW_LINE |
Printing the values os elements | for item in vec : NEW_LINE INDENT print ( item , end = " β " ) NEW_LINE DEDENT |
Driver Code | n = 7 NEW_LINE k = 4 NEW_LINE printsubset ( n , k ) NEW_LINE |
Function to find longest possible subsequence of s beginning with x and y | def solve ( s , x , y ) : NEW_LINE INDENT res = 0 NEW_LINE DEDENT |
Iterate over the string | for c in s : NEW_LINE INDENT if ( ord ( c ) - ord ( '0' ) == x ) : NEW_LINE DEDENT |
Increment count | res += 1 NEW_LINE |
Swap the positions | x , y = y , x NEW_LINE if ( x != y and res % 2 == 1 ) : NEW_LINE res -= 1 NEW_LINE |
Return the result | return res NEW_LINE |
Function that finds all the possible pairs | def find_min ( s ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE DEDENT DEDENT |
Update count | count = max ( count , solve ( s , i , j ) ) NEW_LINE |
Return the answer | return count NEW_LINE |
Given string s | s = "100120013" NEW_LINE |
Find the size of the string | n = len ( s ) NEW_LINE |
Function call | answer = find_min ( s ) NEW_LINE |
This value is the count of minimum element to be removed | print ( n - answer ) NEW_LINE |
Function to count set bits in x | def countSetBitsUtil ( x ) : NEW_LINE |
Base Case | if ( x < 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT |
Recursive Call | if ( x % 2 == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + ( countSetBitsUtil ( x / 2 ) ) ; NEW_LINE DEDENT |
Function that returns count of set bits present in all numbers from 1 to N | def countSetBits ( L , R ) : NEW_LINE |
Initialize the result | bitCount = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT bitCount += countSetBitsUtil ( i ) ; NEW_LINE DEDENT |
Return the setbit count | return bitCount ; NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given L and R | L = 3 ; NEW_LINE R = 5 ; NEW_LINE |
Function Call | print ( " Total β set β bit β count β is β " , countSetBits ( L , R ) ) ; NEW_LINE |
Function to find all possible values of Q | def values_of_Q ( X ) : NEW_LINE |
Vector initialization to store all numbers satisfying the given condition | val_Q = [ ] NEW_LINE |
Iterate for all the values of X | for i in range ( 1 , X + 1 ) : NEW_LINE |
Check if condition satisfied then push the number | if ( ( ( ( X + i ) * X ) ) % i == 0 ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.