text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Stores largest element of the array | max_value = - sys . maxsize - 1 NEW_LINE |
Traverse the array , arr [ ] | for i in range ( 1 , N ) : NEW_LINE |
Update max_value | max_value = max ( arr ) NEW_LINE |
Stores GCD of array | GCDArr = arr [ 0 ] NEW_LINE |
Update GCDArr | GCDArr = gcd ( GCDArr , arr [ i ] ) NEW_LINE |
Stores distinct elements in the array by repeatedely inserting absolute difference of all possible pairs | answer = max_value // GCDArr NEW_LINE return answer + 1 NEW_LINE |
Given array arr [ ] | arr = [ 4 , 12 , 16 , 24 ] NEW_LINE N = len ( arr ) NEW_LINE print ( DistinctValues ( arr , N ) ) NEW_LINE |
Function to return number of moves to convert matrix into chessboard | def minSwaps ( b ) : NEW_LINE |
Size of the matrix | n = len ( b ) NEW_LINE |
Traverse the matrix | for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( b [ 0 ] [ 0 ] ^ b [ 0 ] [ j ] ^ b [ i ] [ 0 ] ^ b [ i ] [ j ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT DEDENT |
Initialize rowSum to count 1 s in row | rowSum = 0 NEW_LINE |
Initialize colSum to count 1 s in column | colSum = 0 NEW_LINE |
To store no . of rows to be corrected | rowSwap = 0 NEW_LINE |
To store no . of columns to be corrected | colSwap = 0 NEW_LINE |
Traverse in the range [ 0 , N - 1 ] | for i in range ( n ) : NEW_LINE INDENT rowSum += b [ i ] [ 0 ] NEW_LINE colSum += b [ 0 ] [ i ] NEW_LINE rowSwap += b [ i ] [ 0 ] == i % 2 NEW_LINE colSwap += b [ 0 ] [ i ] == i % 2 NEW_LINE DEDENT |
Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 | if ( rowSum != n // 2 and rowSum != ( n + 1 ) // 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 | if ( colSum != n // 2 and colSum != ( n + 1 ) // 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
Check if N is odd | if ( n % 2 == 1 ) : NEW_LINE |
Check if column required to be corrected is odd and then assign N - colSwap to colSwap | if ( colSwap % 2 ) : NEW_LINE INDENT colSwap = n - colSwap NEW_LINE DEDENT |
Check if rows required to be corrected is odd and then assign N - rowSwap to rowSwap | if ( rowSwap % 2 ) : NEW_LINE INDENT rowSwap = n - rowSwap NEW_LINE DEDENT else : NEW_LINE |
Take min of colSwap and N - colSwap | colSwap = min ( colSwap , n - colSwap ) NEW_LINE |
Take min of rowSwap and N - rowSwap | rowSwap = min ( rowSwap , n - rowSwap ) NEW_LINE |
Finally return answer | return ( rowSwap + colSwap ) // 2 NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given matrix | M = [ [ 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 ] , [ 1 , 0 , 0 , 1 ] , [ 1 , 0 , 0 , 1 ] ] NEW_LINE |
Function Call | ans = minSwaps ( M ) NEW_LINE |
Print answer | print ( ans ) NEW_LINE |
Function to count of set bit in N | def count_setbit ( N ) : NEW_LINE |
Stores count of set bit in N | result = 0 NEW_LINE |
Iterate over the range [ 0 , 31 ] | for i in range ( 32 ) : NEW_LINE |
If current bit is set | if ( ( 1 << i ) & N ) : NEW_LINE |
Update result | result = result + 1 NEW_LINE print ( result ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 43 NEW_LINE count_setbit ( N ) NEW_LINE DEDENT |
Python 3 program to implement the above approach | mod = 1000000007 NEW_LINE |
Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . | def ValOfTheExpression ( n ) : NEW_LINE INDENT global mod NEW_LINE DEDENT |
factorial [ i ] : Stores factorial of i | factorial = [ 0 for i in range ( n + 1 ) ] NEW_LINE |
Base Case for factorial | factorial [ 0 ] = 1 NEW_LINE factorial [ 1 ] = 1 NEW_LINE |
Precompute the factorial | for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod NEW_LINE DEDENT |
dp [ N ] : Stores the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . | dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE |
Update dp [ i ] | dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod NEW_LINE |
Return the answer . | return dp [ n ] NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE DEDENT |
Function call | print ( ValOfTheExpression ( n ) ) NEW_LINE |
Function to print minimum number of candies required | def minChocolates ( A , N ) : NEW_LINE |
Distribute 1 chocolate to each | B = [ 1 for i in range ( N ) ] NEW_LINE |
Traverse from left to right | for i in range ( 1 , N ) : NEW_LINE INDENT if ( A [ i ] > A [ i - 1 ] ) : NEW_LINE INDENT B [ i ] = B [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT B [ i ] = 1 NEW_LINE DEDENT DEDENT |
Traverse from right to left | for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT B [ i ] = max ( B [ i + 1 ] + 1 , B [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT B [ i ] = max ( B [ i ] , 1 ) NEW_LINE DEDENT DEDENT |
Initialize sum | sum = 0 NEW_LINE |
Find total sum | for i in range ( N ) : NEW_LINE INDENT sum += B [ i ] NEW_LINE DEDENT |
Return sum | print ( sum ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given array | A = [ 23 , 14 , 15 , 14 , 56 , 29 , 14 ] NEW_LINE |
Size of the given array | N = len ( A ) NEW_LINE minChocolates ( A , N ) NEW_LINE |
Python3 program to implement the above approach | from math import sqrt , ceil , floor NEW_LINE |
Function to construct an array of unique elements whose LCM is N | def constructArrayWithGivenLCM ( N ) : NEW_LINE |
Stores array elements whose LCM is N | newArr = [ ] NEW_LINE |
Iterate over the range [ 1 , sqrt ( N ) ] | for i in range ( 1 , ceil ( sqrt ( N + 1 ) ) ) : NEW_LINE |
If N is divisible by i | if ( N % i == 0 ) : NEW_LINE |
Insert i into newArr [ ] | newArr . append ( i ) NEW_LINE |
If N is not perfect square | if ( N // i != i ) : NEW_LINE INDENT newArr . append ( N // i ) NEW_LINE DEDENT |
Sort the array newArr [ ] | newArr = sorted ( newArr ) NEW_LINE |
Print array elements | for i in newArr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given N | N = 12 NEW_LINE |
Function Call | constructArrayWithGivenLCM ( N ) NEW_LINE |
Function to calculate 5 ^ p | def getPower ( p ) : NEW_LINE |
Stores the result | res = 1 NEW_LINE |
Multiply 5 p times | while ( p ) : NEW_LINE INDENT res *= 5 NEW_LINE p -= 1 NEW_LINE DEDENT |
Return the result | return res NEW_LINE |
Function to count anumbers upto N having odd digits at odd places and even digits at even places | def countNumbersUtil ( N ) : NEW_LINE |
Stores the count | count = 0 NEW_LINE |
Stores the digits of N | digits = [ ] NEW_LINE |
Insert the digits of N | while ( N ) : NEW_LINE INDENT digits . append ( N % 10 ) NEW_LINE N //= 10 NEW_LINE DEDENT |
Reverse the vector to arrange the digits from first to last | digits . reverse ( ) NEW_LINE |
Stores count of digits of n | D = len ( digits ) NEW_LINE for i in range ( 1 , D + 1 , 1 ) : NEW_LINE |
Stores the count of numbers with i digits | res = getPower ( i ) NEW_LINE |
If the last digit is reached , subtract numbers eceeding range | if ( i == D ) : NEW_LINE |
Iterate over athe places | for p in range ( 1 , D + 1 , 1 ) : NEW_LINE |
Stores the digit in the pth place | x = digits [ p - 1 ] NEW_LINE |
Stores the count of numbers having a digit greater than x in the p - th position | tmp = 0 NEW_LINE |
Calculate the count of numbers exceeding the range if p is even | if ( p % 2 == 0 ) : NEW_LINE INDENT tmp = ( ( 5 - ( x // 2 + 1 ) ) * getPower ( D - p ) ) NEW_LINE DEDENT |
Calculate the count of numbers exceeding the range if p is odd | else : NEW_LINE INDENT tmp = ( ( 5 - ( x + 1 ) // 2 ) * getPower ( D - p ) ) NEW_LINE DEDENT |
Subtract the count of numbers exceeding the range from total count | res -= tmp NEW_LINE |
If the parity of p and the parity of x are not same | if ( p % 2 != x % 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT |
Add count of numbers having i digits and satisfies the given conditions | count += res NEW_LINE |
Return the total count of numbers tin | return count NEW_LINE |
Function to calculate the count of numbers from given range having odd digits places and even digits at even places | def countNumbers ( L , R ) : NEW_LINE |
Driver Code | L = 128 NEW_LINE R = 162 NEW_LINE countNumbers ( L , R ) NEW_LINE |
Function to find the sum of First N natural numbers with alternate signs | def alternatingSumOfFirst_N ( N ) : NEW_LINE |
Stores sum of alternate sign of First N natural numbers | alternateSum = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE |
If is an even number | if ( i % 2 == 0 ) : NEW_LINE |
Update alternateSum | alternateSum += - i NEW_LINE |
If i is an odd number | else : NEW_LINE |
Update alternateSum | alternateSum += i NEW_LINE return alternateSum NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( alternatingSumOfFirst_N ( N ) ) NEW_LINE DEDENT |
Function to return gcd of a and b | def gcd ( a , b ) : NEW_LINE |
Base Case | if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT |
Recursive GCD | return gcd ( b % a , a ) ; NEW_LINE |
Function to calculate the sum of all numbers till N that are coprime with N | def findSum ( N ) : NEW_LINE |
Stores the resultant sum | sum = 0 ; NEW_LINE |