text
stringlengths
1
636
code
stringlengths
8
1.89k
If z is an integer
if ( floor ( z ) == ceil ( z ) ) : NEW_LINE INDENT x = i // a NEW_LINE y = j // b NEW_LINE maxVal = max ( maxVal , x + y + int ( z ) ) NEW_LINE DEDENT return maxVal NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE a = 5 NEW_LINE b = 3 NEW_LINE c = 4 NEW_LINE DEDENT
Function Call
print ( maxResult ( n , a , b , c ) ) NEW_LINE
Function that returns true if all the array elements can be made equal with the given operation
def EqualNumbers ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE DEDENT
Divide number by 2
while a [ i ] % 2 == 0 : NEW_LINE INDENT a [ i ] //= 2 NEW_LINE DEDENT
Divide number by 3
while a [ i ] % 3 == 0 : NEW_LINE INDENT a [ i ] //= 3 NEW_LINE DEDENT if a [ i ] != a [ 0 ] : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 50 , 75 , 150 ] NEW_LINE n = len ( a ) NEW_LINE if EqualNumbers ( a , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Python3 implementation of the approach
import math NEW_LINE
Function to return the required gcd
def max_gcd ( n , p ) : NEW_LINE INDENT count = 0 ; NEW_LINE gcd = 1 ; NEW_LINE DEDENT
Count the number of times 2 divides p
while ( p % 2 == 0 ) : NEW_LINE
Equivalent to p = p / 2 ;
p >>= 1 ; NEW_LINE count = count + 1 ; NEW_LINE
If 2 divides p
if ( count > 0 ) : NEW_LINE INDENT gcd = gcd * pow ( 2 , count // n ) ; NEW_LINE DEDENT
Check all the possible numbers that can divide p
for i in range ( 3 , ( int ) ( math . sqrt ( p ) ) , 2 ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( p % i == 0 ) : NEW_LINE INDENT count = count + 1 ; NEW_LINE p = p // i ; NEW_LINE DEDENT if ( count > 0 ) : NEW_LINE INDENT gcd = gcd * pow ( i , count // n ) ; NEW_LINE DEDENT DEDENT
If n in the end is a prime number
if ( p > 2 ) : NEW_LINE INDENT gcd = gcd * pow ( p , 1 // n ) ; NEW_LINE DEDENT
Return the required gcd
return gcd ; NEW_LINE
Driver code
n = 3 ; NEW_LINE p = 80 ; NEW_LINE print ( max_gcd ( n , p ) ) ; NEW_LINE
Function to return the required number
def getMinNum ( a , b , c ) : NEW_LINE
If doesn 't belong to the range then c is the required number
if ( c < a or c > b ) : NEW_LINE INDENT return c NEW_LINE DEDENT
Else get the next multiple of c starting from b + 1
x = ( ( b // c ) * c ) + c NEW_LINE return x NEW_LINE
Driver code
a , b , c = 2 , 4 , 4 NEW_LINE print ( getMinNum ( a , b , c ) ) NEW_LINE
Function to return the count of required pairs
def countPairs ( n ) : NEW_LINE
Special case
if ( n == 2 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT
Number which will give the max value for ( ( n % i ) % j ) % n
num = ( ( n // 2 ) + 1 ) ; NEW_LINE
To store the maximum possible value of ( ( n % i ) % j ) % n
max = n % num ; NEW_LINE
Count of possible pairs
count = n - max ; NEW_LINE return count NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE DEDENT print ( countPairs ( n ) ) ; NEW_LINE
Python3 program to remove digits from a numeric string such that the number becomes divisible by 8
import math as mt NEW_LINE
Function that return true if sub is a sub - sequence in s
def checkSub ( sub , s ) : NEW_LINE INDENT j = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( sub [ j ] == s [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT if j == int ( len ( sub ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT
Function to return a multiple of 8 formed after removing 0 or more characters from the given string
def getMultiple ( s ) : NEW_LINE
Iterate over all multiples of 8
for i in range ( 0 , 10 ** 3 , 8 ) : NEW_LINE
If current multiple exists as a subsequence in the given string
if ( checkSub ( str ( i ) , s ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT return - 1 NEW_LINE
Driver Code
s = "3454" NEW_LINE print ( getMultiple ( s ) ) NEW_LINE
Python implementation of above approach
def getResult ( n ) : NEW_LINE
Converting integer to string
st = str ( n ) NEW_LINE
Traversing the string
for i in st : NEW_LINE
If the number is divisible by digits then return yes
if ( n % int ( i ) == 0 ) : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT
If no digits are dividing the number then return no
return ' No ' NEW_LINE
Driver Code
n = 9876543 NEW_LINE
passing this number to get result function
print ( getResult ( n ) ) NEW_LINE
Python program to find sum of harmonic series using recursion
def sum ( n ) : NEW_LINE
Base condition
if n < 2 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 / n + ( sum ( n - 1 ) ) NEW_LINE DEDENT
Driven Code
print ( sum ( 8 ) ) NEW_LINE print ( sum ( 10 ) ) NEW_LINE
Python3 implementation of the above approach
import math as mt NEW_LINE
Function to calculate the value of the
def findingValues ( m , n , mth , nth ) : NEW_LINE
Calculate value of d using formula
d = ( ( abs ( mth - nth ) ) / abs ( ( m - 1 ) - ( n - 1 ) ) ) NEW_LINE
Calculate value of a using formula
a = mth - ( ( m - 1 ) * d ) NEW_LINE
Return pair
return a , d NEW_LINE
Function to calculate value sum of first p numbers of the series
def findSum ( m , n , mth , nth , p ) : NEW_LINE
First calculate value of a and d
a , d = findingValues ( m , n , mth , nth ) NEW_LINE
Calculate the sum by using formula
Sum = ( p * ( 2 * a + ( p - 1 ) * d ) ) / 2 NEW_LINE
Return the Sum
return Sum NEW_LINE
Driver Code
m = 6 NEW_LINE n = 10 NEW_LINE mTerm = 12 NEW_LINE nTerm = 20 NEW_LINE p = 5 NEW_LINE print ( findSum ( m , n , mTerm , nTerm , p ) ) NEW_LINE
Function to print powerful integers
def powerfulIntegers ( x , y , bound ) : NEW_LINE
Set is used to store distinct numbers in sorted order
s = set ( ) NEW_LINE powersOfY = [ ] NEW_LINE
Store all the powers of y < bound in a vector to avoid calculating them again and again
powersOfY . append ( 1 ) NEW_LINE i = y NEW_LINE while i < bound and y != 1 : NEW_LINE INDENT powersOfY . append ( i ) NEW_LINE i *= y NEW_LINE DEDENT i = 0 NEW_LINE while ( True ) : NEW_LINE
x ^ i
xPowI = pow ( x , i ) NEW_LINE for j in powersOfY : NEW_LINE INDENT num = xPowI + j NEW_LINE DEDENT
If num is within limits insert it into the set
if ( num <= bound ) : NEW_LINE INDENT s . add ( num ) NEW_LINE DEDENT
Break out of the inner loop
else : NEW_LINE INDENT break NEW_LINE DEDENT
Adding any number to it will be out of bounds
if ( xPowI >= bound or x == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT
Increment i
i += 1 NEW_LINE
Print the contents of the set
for itr in s : NEW_LINE INDENT print ( itr , end = " ▁ " ) NEW_LINE DEDENT
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE bound = 10 NEW_LINE DEDENT
Print powerful integers
powerfulIntegers ( x , y , bound ) NEW_LINE
Python3 code for better approach to distribute candies
import math as mt NEW_LINE
Function to find out the number of candies every person received
def candies ( n , k ) : NEW_LINE
Count number of complete turns
count = 0 NEW_LINE
Get the last term
ind = 1 NEW_LINE
Stores the number of candies
arr = [ 0 for i in range ( k ) ] NEW_LINE while n > 0 : NEW_LINE
Last term of last and current series
f1 = ( ind - 1 ) * k NEW_LINE f2 = ind * k NEW_LINE
Sum of current and last series
sum1 = ( f1 * ( f1 + 1 ) ) // 2 NEW_LINE sum2 = ( f2 * ( f2 + 1 ) ) // 2 NEW_LINE
Sum of current series only
res = sum2 - sum1 NEW_LINE
If sum of current is less than N
if ( res <= n ) : NEW_LINE INDENT count += 1 NEW_LINE n -= res NEW_LINE ind += 1 NEW_LINE DEDENT
else : Individually distribute
i = 0 NEW_LINE
First term
term = ( ( ind - 1 ) * k ) + 1 NEW_LINE
Distribute candies till there
while ( n > 0 ) : NEW_LINE
Candies available
if ( term <= n ) : NEW_LINE INDENT arr [ i ] = term NEW_LINE i += 1 NEW_LINE n -= term NEW_LINE term += 1 NEW_LINE DEDENT
Not available
else : NEW_LINE INDENT arr [ i ] = n NEW_LINE i += 1 NEW_LINE n = 0 NEW_LINE DEDENT
Count the total candies
for i in range ( k ) : NEW_LINE INDENT arr [ i ] += ( ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) // 2 ) ) NEW_LINE DEDENT
Print the total candies
for i in range ( k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Driver Code
n , k = 10 , 3 NEW_LINE candies ( n , k ) NEW_LINE
Function to find out the number of candies every person received
def candies ( n , k ) : NEW_LINE
Count number of complete turns
count = 0 ; NEW_LINE
Get the last term
ind = 1 ; NEW_LINE
Stores the number of candies
arr = [ 0 ] * k ; NEW_LINE low = 0 ; NEW_LINE high = n ; NEW_LINE
Do a binary search to find the number whose sum is less than N .
while ( low <= high ) : NEW_LINE
Get mide
mid = ( low + high ) >> 1 ; NEW_LINE sum = ( mid * ( mid + 1 ) ) >> 1 ; NEW_LINE
If sum is below N
if ( sum <= n ) : NEW_LINE
Find number of complete turns
count = int ( mid / k ) ; NEW_LINE
Right halve
low = mid + 1 ; NEW_LINE else : NEW_LINE
Left halve
high = mid - 1 ; NEW_LINE
Last term of last complete series
last = ( count * k ) ; NEW_LINE
Subtract the sum till
n -= int ( ( last * ( last + 1 ) ) / 2 ) ; NEW_LINE i = 0 ; NEW_LINE
First term of incomplete series
term = ( count * k ) + 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( term <= n ) : NEW_LINE INDENT arr [ i ] = term ; NEW_LINE i += 1 ; NEW_LINE n -= term ; NEW_LINE term += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] += n ; NEW_LINE n = 0 ; NEW_LINE DEDENT DEDENT
Count the total candies
for i in range ( k ) : NEW_LINE INDENT arr [ i ] += ( ( count * ( i + 1 ) ) + int ( k * ( count * ( count - 1 ) ) / 2 ) ) ; NEW_LINE DEDENT
Print the total candies
for i in range ( k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT
Driver Code
n = 7 ; NEW_LINE k = 4 ; NEW_LINE candies ( n , k ) ; NEW_LINE
Function to return the minimum number divisible by 3 formed by the given digits
def printSmallest ( a , n ) : NEW_LINE INDENT sum0 , sum1 = 0 , 0 NEW_LINE DEDENT
Sort the given array in ascending
a = sorted ( a ) NEW_LINE
Check if any single digit is divisible by 3
for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 3 == 0 ) : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT
Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum
for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE DEDENT