text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Generate the two digit number
|
num = ( a [ i ] * 10 ) + a [ j ] NEW_LINE if ( num % 3 == 0 ) : NEW_LINE INDENT return num NEW_LINE DEDENT
|
If none of the above is true , we can form three digit number by taking a [ 0 ] three times .
|
return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] NEW_LINE
|
Driver code
|
arr = [ 7 , 7 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( printSmallest ( arr , n ) ) NEW_LINE
|
Function to update and print the matrix after performing queries
|
def updateMatrix ( n , q , mat ) : NEW_LINE INDENT for i in range ( 0 , len ( q ) ) : NEW_LINE INDENT X1 = q [ i ] [ 0 ] ; NEW_LINE Y1 = q [ i ] [ 1 ] ; NEW_LINE X2 = q [ i ] [ 2 ] ; NEW_LINE Y2 = q [ i ] [ 3 ] ; NEW_LINE DEDENT DEDENT
|
Add 1 to the first element of the sub - matrix
|
mat [ X1 ] [ Y1 ] = mat [ X1 ] [ Y1 ] + 1 ; NEW_LINE
|
If there is an element after the last element of the sub - matrix then decrement it by 1
|
if ( Y2 + 1 < n ) : NEW_LINE INDENT mat [ X2 ] [ Y2 + 1 ] = mat [ X2 ] [ Y2 + 1 ] - 1 ; NEW_LINE DEDENT elif ( X2 + 1 < n ) : NEW_LINE INDENT mat [ X2 + 1 ] [ 0 ] = mat [ X2 + 1 ] [ 0 ] - 1 ; NEW_LINE DEDENT
|
Calculate the running sum
|
sum = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT sum = sum + mat [ i ] [ j ] ; NEW_LINE DEDENT DEDENT
|
Print the updated element
|
print ( sum , end = ' β ' ) ; NEW_LINE
|
Next line
|
print ( " β " ) ; NEW_LINE
|
Size of the matrix
|
n = 5 ; NEW_LINE mat = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] ; NEW_LINE
|
Queries
|
q = [ [ 0 , 0 , 1 , 2 ] , [ 1 , 2 , 3 , 4 ] , [ 1 , 4 , 3 , 4 ] ] ; NEW_LINE updateMatrix ( n , q , mat ) ; NEW_LINE
|
Utility function to print the contents of the array
|
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT DEDENT
|
Function to replace the maximum element from the array with the coefficient of range of the array
|
def replaceMax ( arr , n ) : NEW_LINE
|
Maximum element from the array
|
max_element = max ( arr ) NEW_LINE
|
Minimum element from the array
|
min_element = min ( arr ) NEW_LINE
|
Calculate the coefficient of range for the array
|
ranges = max_element - min_element NEW_LINE coeffOfRange = ranges / ( max_element + min_element ) NEW_LINE
|
Assuming all the array elements are distinct . Replace the maximum element with the coefficient of range of the array
|
for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == max_element ) : NEW_LINE INDENT arr [ i ] = coeffOfRange NEW_LINE break NEW_LINE DEDENT DEDENT
|
Print the updated array
|
printArr ( arr , n ) NEW_LINE
|
Driver code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 15 , 16 , 10 , 9 , 6 , 7 , 17 ] NEW_LINE n = len ( arr ) NEW_LINE replaceMax ( arr , n ) NEW_LINE DEDENT
|
print the numbers after dividing them by their common factors
|
def divide ( a , b ) : NEW_LINE
|
iterate from 1 to minimum of a and b
|
for i in range ( 2 , min ( a , b ) + 1 ) : NEW_LINE
|
if i is the common factor of both the numbers
|
while ( a % i == 0 and b % i == 0 ) : NEW_LINE INDENT a = a // i NEW_LINE b = b // i NEW_LINE DEDENT print ( " A β = " , a , " , β B β = " , b ) NEW_LINE
|
Driver code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , B = 10 , 15 NEW_LINE DEDENT
|
divide A and B by their common factors
|
divide ( A , B ) NEW_LINE
|
Function to calculate gcd of two numbers
|
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT
|
Function to calculate all common divisors of two given numbers a , b -- > input eger numbers
|
def commDiv ( a , b ) : NEW_LINE
|
find gcd of a , b
|
n = gcd ( a , b ) NEW_LINE a = a // n NEW_LINE b = b // n NEW_LINE print ( " A β = " , a , " , β B β = " , b ) NEW_LINE
|
Driver code
|
a , b = 10 , 15 NEW_LINE commDiv ( a , b ) NEW_LINE
|
Python3 implementation of the above approach
|
import math NEW_LINE
|
Function to return the minimum difference between N and a power of 2
|
def minAbsDiff ( n ) : NEW_LINE
|
Power of 2 closest to n on its left
|
left = 1 << ( int ) ( math . floor ( math . log2 ( n ) ) ) NEW_LINE
|
Power of 2 closest to n on its right
|
right = left * 2 NEW_LINE
|
Return the minimum abs difference
|
return min ( ( n - left ) , ( right - n ) ) NEW_LINE
|
Driver code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 NEW_LINE print ( minAbsDiff ( n ) ) NEW_LINE DEDENT
|
Function to return the probability of the winner
|
def find_probability ( p , q , r , s ) : NEW_LINE INDENT t = ( 1 - p / q ) * ( 1 - r / s ) NEW_LINE ans = ( p / q ) / ( 1 - t ) ; NEW_LINE return round ( ans , 9 ) NEW_LINE DEDENT
|
Driver Code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p , q , r , s = 1 , 2 , 1 , 2 NEW_LINE DEDENT
|
Will print 9 digits after the decimal point
|
print ( find_probability ( p , q , r , s ) ) NEW_LINE
|
Function to print k numbers which are powers of two and whose sum is equal to n
|
def FindAllElements ( n , k ) : NEW_LINE
|
Initialising the sum with k
|
sum = k NEW_LINE
|
Initialising an array A with k elements and filling all elements with 1
|
A = [ 1 for i in range ( k ) ] NEW_LINE i = k - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE
|
Iterating A [ ] from k - 1 to 0
|
while ( sum + A [ i ] <= n ) : NEW_LINE
|
Update sum and A [ i ] till sum + A [ i ] is less than equal to n
|
sum += A [ i ] NEW_LINE A [ i ] *= 2 NEW_LINE i -= 1 NEW_LINE
|
Impossible to find the combination
|
if ( sum != n ) : NEW_LINE INDENT print ( " Impossible " ) NEW_LINE DEDENT
|
Possible solution is stored in A [ ]
|
else : NEW_LINE INDENT for i in range ( 0 , k , 1 ) : NEW_LINE INDENT print ( A [ i ] , end = ' β ' ) NEW_LINE DEDENT DEDENT
|
Driver code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE k = 6 NEW_LINE FindAllElements ( n , k ) NEW_LINE DEDENT
|
Function to remove zeroes
|
def removeZero ( n ) : NEW_LINE
|
Initialize result to zero holds the Result after removing zeroes from no
|
res = 0 NEW_LINE
|
Initialize variable d to 1 that holds digits of no
|
d = 1 NEW_LINE
|
Loop while n is greater then zero
|
while ( n > 0 ) : NEW_LINE
|
Check if n mod 10 is not equal to zero
|
if ( n % 10 != 0 ) : NEW_LINE
|
store the result by removing zeroes And increment d by 10
|
res += ( n % 10 ) * d NEW_LINE d *= 10 NEW_LINE
|
Go to the next digit
|
n //= 10 NEW_LINE
|
Return the result
|
return res NEW_LINE
|
Function to check if sum is true after Removing all zeroes .
|
def isEqual ( a , b ) : NEW_LINE
|
Call removeZero ( ) for both sides and check whether they are equal After removing zeroes .
|
if ( removeZero ( a ) + removeZero ( b ) == removeZero ( a + b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE
|
Driver code
|
a = 105 NEW_LINE b = 106 NEW_LINE if ( isEqual ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
Python3 implementation of above approach
|
def sumArray ( arr , n ) : NEW_LINE
|
Allocate memory for temporary arrays leftSum [ ] , rightSum [ ] and Sum [ ]
|
leftSum = [ 0 for i in range ( n ) ] NEW_LINE rightSum = [ 0 for i in range ( n ) ] NEW_LINE Sum = [ 0 for i in range ( n ) ] NEW_LINE i , j = 0 , 0 NEW_LINE
|
Left most element of left array is always 0
|
leftSum [ 0 ] = 0 NEW_LINE
|
Rightmost most element of right array is always 0
|
rightSum [ n - 1 ] = 0 NEW_LINE
|
Construct the left array
|
for i in range ( 1 , n ) : NEW_LINE INDENT leftSum [ i ] = arr [ i - 1 ] + leftSum [ i - 1 ] NEW_LINE DEDENT
|
Construct the right array
|
for j in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT rightSum [ j ] = arr [ j + 1 ] + rightSum [ j + 1 ] NEW_LINE DEDENT
|
Construct the sum array using left [ ] and right [ ]
|
for i in range ( 0 , n ) : NEW_LINE INDENT Sum [ i ] = leftSum [ i ] + rightSum [ i ] NEW_LINE DEDENT
|
print the constructed prod array
|
for i in range ( n ) : NEW_LINE INDENT print ( Sum [ i ] , end = " β " ) NEW_LINE DEDENT
|
Driver Code
|
arr = [ 3 , 6 , 4 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE sumArray ( arr , n ) NEW_LINE
|
Python3 program to find the minimum positive X such that the given equation holds true
|
import sys NEW_LINE
|
This function gives the required answer
|
def minimumX ( n , k ) : NEW_LINE INDENT mini = sys . maxsize NEW_LINE DEDENT
|
Iterate for all the factors
|
i = 1 NEW_LINE while i * i <= n : NEW_LINE
|
Check if i is a factor
|
if ( n % i == 0 ) : NEW_LINE INDENT fir = i NEW_LINE sec = n // i NEW_LINE num1 = fir * k + sec NEW_LINE DEDENT
|
Consider i to be A and n / i to be B
|
res = ( num1 // k ) * ( num1 % k ) NEW_LINE if ( res == n ) : NEW_LINE INDENT mini = min ( num1 , mini ) NEW_LINE DEDENT num2 = sec * k + fir NEW_LINE res = ( num2 // k ) * ( num2 % k ) NEW_LINE
|
Consider i to be B and n / i to be A
|
if ( res == n ) : NEW_LINE INDENT mini = min ( num2 , mini ) NEW_LINE DEDENT i += 1 NEW_LINE return mini NEW_LINE
|
Driver Code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE k = 6 NEW_LINE print ( minimumX ( n , k ) ) NEW_LINE n = 5 NEW_LINE k = 5 NEW_LINE print ( minimumX ( n , k ) ) NEW_LINE DEDENT
|
This function gives the required answer
|
def minimumX ( n , k ) : NEW_LINE INDENT ans = 10 ** 18 NEW_LINE DEDENT
|
Iterate over all possible remainders
|
for i in range ( k - 1 , 0 , - 1 ) : NEW_LINE
|
it must divide n
|
if n % i == 0 : NEW_LINE INDENT ans = min ( ans , i + ( n / i ) * k ) NEW_LINE DEDENT return ans NEW_LINE
|
Driver Code
|
n , k = 4 , 6 NEW_LINE print ( minimumX ( n , k ) ) NEW_LINE n , k = 5 , 5 NEW_LINE print ( minimumX ( n , k ) ) NEW_LINE
|
Function to return nth Hermite number
|
def getHermiteNumber ( n ) : NEW_LINE
|
Base conditions
|
if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( - 2 * ( n - 1 ) * getHermiteNumber ( n - 2 ) ) NEW_LINE DEDENT
|
Driver Code
|
n = 6 NEW_LINE
|
Print nth Hermite number
|
print ( getHermiteNumber ( n ) ) ; NEW_LINE
|
Function to print the required numbers
|
def find ( n ) : NEW_LINE
|
Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 )
|
b = n NEW_LINE a = b * ( n - 1 ) NEW_LINE
|
Special case if n = 1 we get a = 0 so ( a * b ) < n
|
if a * b > n and a // b < n : NEW_LINE INDENT print ( " a β = β { } , β b β = β { } " . format ( a , b ) ) NEW_LINE DEDENT
|
If no pair satisfies the conditions
|
else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
|
Driver Code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE find ( n ) NEW_LINE DEDENT
|
Function to check if a number is perfect square or not
|
from math import sqrt , floor NEW_LINE def isPerfect ( N ) : NEW_LINE INDENT if ( sqrt ( N ) - floor ( sqrt ( N ) ) != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT
|
Function to find the closest perfect square taking minimum steps to reach from a number
|
def getClosestPerfectSquare ( N ) : NEW_LINE INDENT if ( isPerfect ( N ) ) : NEW_LINE INDENT print ( N , "0" ) NEW_LINE return NEW_LINE DEDENT DEDENT
|
Variables to store first perfect square number above and below N
|
aboveN = - 1 NEW_LINE belowN = - 1 NEW_LINE n1 = 0 NEW_LINE
|
Finding first perfect square number greater than N
|
n1 = N + 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPerfect ( n1 ) ) : NEW_LINE INDENT aboveN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 += 1 NEW_LINE DEDENT DEDENT
|
Finding first perfect square number less than N
|
n1 = N - 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPerfect ( n1 ) ) : NEW_LINE INDENT belowN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 -= 1 NEW_LINE DEDENT DEDENT
|
Variables to store the differences
|
diff1 = aboveN - N NEW_LINE diff2 = N - belowN NEW_LINE if ( diff1 > diff2 ) : NEW_LINE INDENT print ( belowN , diff2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( aboveN , diff1 ) NEW_LINE DEDENT
|
Driver code
|
N = 1500 NEW_LINE getClosestPerfectSquare ( N ) NEW_LINE
|
Function to return gcd of a and b
|
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT
|
Function to convert the obtained fraction into it 's simplest form
|
def lowest ( den3 , num3 ) : NEW_LINE
|
Finding gcd of both terms
|
common_factor = gcd ( num3 , den3 ) NEW_LINE
|
Converting both terms into simpler terms by dividing them by common factor
|
den3 = int ( den3 / common_factor ) NEW_LINE num3 = int ( num3 / common_factor ) NEW_LINE print ( num3 , " / " , den3 ) NEW_LINE
|
Function to add two fractions
|
def addFraction ( num1 , den1 , num2 , den2 ) : NEW_LINE
|
Finding gcd of den1 and den2
|
den3 = gcd ( den1 , den2 ) NEW_LINE
|
Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b
|
den3 = ( den1 * den2 ) / den3 NEW_LINE
|
Changing the fractions to have same denominator Numerator of the final fraction obtained
|
num3 = ( ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ) NEW_LINE
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.