text
stringlengths
1
636
code
stringlengths
8
1.89k
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l1 = 1 ; r1 = 400 ; NEW_LINE leapNum ( l1 , r1 ) ; NEW_LINE l2 = 400 ; r2 = 2000 ; NEW_LINE leapNum ( l2 , r2 ) ; NEW_LINE DEDENT
Python3 implementation of the approach
N = 100005 NEW_LINE mod = ( 10 ** 9 + 7 ) NEW_LINE
To store the factorial and the factorial mod inverse of a number
factorial = [ 0 ] * N NEW_LINE modinverse = [ 0 ] * N NEW_LINE
Function to find factorial of all the numbers
def factorialfun ( ) : NEW_LINE INDENT factorial [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod NEW_LINE DEDENT DEDENT
Function to find the factorial mod inverse of all the numbers
def modinversefun ( ) : NEW_LINE INDENT modinverse [ N - 1 ] = pow ( factorial [ N - 1 ] , mod - 2 , mod ) % mod NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod NEW_LINE DEDENT DEDENT
Function to return nCr
def binomial ( n , r ) : NEW_LINE INDENT if ( r > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT a = ( factorial [ n ] * modinverse [ n - r ] ) % mod NEW_LINE a = ( a * modinverse [ r ] ) % mod NEW_LINE return a NEW_LINE DEDENT
Function to find sum of f ( s ) for all the chosen sets from the given array
def max_min ( a , n , k ) : NEW_LINE
Sort the given array
a = sorted ( a ) NEW_LINE
Calculate the factorial and modinverse of all elements
factorialfun ( ) NEW_LINE modinversefun ( ) NEW_LINE ans = 0 NEW_LINE k -= 1 NEW_LINE
For all the possible sets Calculate max ( S ) and min ( S )
for i in range ( n ) : NEW_LINE INDENT x = n - i - 1 NEW_LINE if ( x >= k ) : NEW_LINE INDENT ans -= ( binomial ( x , k ) * a [ i ] ) % mod NEW_LINE DEDENT y = i NEW_LINE if ( y >= k ) : NEW_LINE INDENT ans += ( binomial ( y , k ) * a [ i ] ) % mod NEW_LINE DEDENT ans = ( ans + mod ) % mod NEW_LINE DEDENT return ans NEW_LINE
Driver code
a = [ 1 , 1 , 3 , 4 ] NEW_LINE k = 2 NEW_LINE n = len ( a ) NEW_LINE print ( max_min ( a , n , k ) ) NEW_LINE
Function to return the count of minimum elements such that the sum of those elements is > S .
def countNumber ( N , S ) : NEW_LINE INDENT countElements = 0 ; NEW_LINE DEDENT
Initialize currentSum = 0
currSum = 0 ; NEW_LINE
Loop from N to 1 to add the numbers and check the condition .
while ( currSum <= S ) : NEW_LINE INDENT currSum += N ; NEW_LINE N = N - 1 ; NEW_LINE countElements = countElements + 1 ; NEW_LINE DEDENT return countElements ; NEW_LINE
Driver code
N = 5 ; NEW_LINE S = 11 ; NEW_LINE count = countNumber ( N , S ) ; NEW_LINE print ( count ) ; NEW_LINE
Python3 program to find next consecutive Number with all distinct digits
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE
Function to count distinct digits in a number
def countDistinct ( n ) : NEW_LINE
To count the occurrence of digits in number from 0 to 9
arr = [ 0 ] * 10 ; NEW_LINE count = 0 ; NEW_LINE
Iterate over the digits of the number Flag those digits as found in the array
while ( n != 0 ) : NEW_LINE INDENT r = int ( n % 10 ) ; NEW_LINE arr [ r ] = 1 ; NEW_LINE n //= 10 ; NEW_LINE DEDENT
Traverse the array arr and count the distinct digits in the array
for i in range ( 10 ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE
Function to return the total number of digits in the number
def countDigit ( n ) : NEW_LINE INDENT c = 0 ; NEW_LINE DEDENT
Iterate over the digits of the number
while ( n != 0 ) : NEW_LINE INDENT r = n % 10 ; NEW_LINE c += 1 ; NEW_LINE n //= 10 ; NEW_LINE DEDENT return c ; NEW_LINE
Function to return the next number with distinct digits
def nextNumberDistinctDigit ( n ) : NEW_LINE INDENT while ( n < INT_MAX ) : NEW_LINE DEDENT
Count the distinct digits in N + 1
distinct_digits = countDistinct ( n + 1 ) ; NEW_LINE
Count the total number of digits in N + 1
total_digits = countDigit ( n + 1 ) ; NEW_LINE if ( distinct_digits == total_digits ) : NEW_LINE
Return the next consecutive number
return n + 1 ; NEW_LINE else : NEW_LINE
Increment Number by 1
n += 1 ; NEW_LINE return - 1 ; NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2019 ; NEW_LINE print ( nextNumberDistinctDigit ( n ) ) ; NEW_LINE DEDENT
Python3 implementation of the approach
mod = 10 ** 9 + 7 NEW_LINE N = 1000005 NEW_LINE
To store least prime factors of all the numbers
lpf = [ 0 for i in range ( N ) ] NEW_LINE
Function to find the least prime factor of all the numbers
def least_prime_factor ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT lpf [ i ] = i NEW_LINE DEDENT for i in range ( 2 , N ) : NEW_LINE INDENT if ( lpf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * 2 , N , i ) : NEW_LINE INDENT if ( lpf [ j ] == j ) : NEW_LINE INDENT lpf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT
Function to return the sum of elements of array B
def sum_of_elements ( a , n ) : NEW_LINE
Find the prime factors of all the numbers
least_prime_factor ( ) NEW_LINE
To store each prime count in lcm
prime_factor = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE
Current number
temp = a [ i ] NEW_LINE
Map to store the prime count of a single number
single_number = dict ( ) NEW_LINE
Basic way to calculate all prime factors
while ( temp > 1 ) : NEW_LINE INDENT x = lpf [ temp ] NEW_LINE single_number [ x ] = single_number . get ( x , 0 ) + 1 NEW_LINE temp //= x NEW_LINE DEDENT
If it is the first number in the array
if ( i == 0 ) : NEW_LINE INDENT prime_factor = single_number NEW_LINE DEDENT
Take the maximum count of prime in a number
else : NEW_LINE INDENT for x in single_number : NEW_LINE INDENT if x in prime_factor : NEW_LINE INDENT prime_factor [ x ] = max ( prime_factor [ x ] , single_number [ x ] ) NEW_LINE DEDENT else : NEW_LINE INDENT prime_factor [ x ] = single_number [ x ] NEW_LINE DEDENT DEDENT DEDENT ans , lcm = 0 , 1 NEW_LINE
Calculate lcm of given array
for x in prime_factor : NEW_LINE INDENT lcm = ( lcm * pow ( x , prime_factor [ x ] , mod ) ) % mod NEW_LINE DEDENT
Calculate sum of elements of array B
for i in range ( n ) : NEW_LINE INDENT ans = ( ans + ( lcm * pow ( a [ i ] , mod - 2 , mod ) ) % mod ) % mod NEW_LINE DEDENT return ans NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE print ( sum_of_elements ( a , n ) ) NEW_LINE DEDENT
Function to find the number of even cell in a 2D matrix
def findNumberOfEvenCells ( n , q , size ) : NEW_LINE
Maintain two arrays , one for rows operation and one for column operation
row = [ 0 ] * n ; NEW_LINE col = [ 0 ] * n NEW_LINE for i in range ( size ) : NEW_LINE INDENT x = q [ i ] [ 0 ] ; NEW_LINE y = q [ i ] [ 1 ] ; NEW_LINE DEDENT
Increment operation on row [ i ]
row [ x - 1 ] += 1 ; NEW_LINE
Increment operation on col [ i ]
col [ y - 1 ] += 1 ; NEW_LINE r1 = 0 ; NEW_LINE r2 = 0 ; NEW_LINE c1 = 0 ; NEW_LINE c2 = 0 ; NEW_LINE
Count odd and even values in both arrays and multiply them
for i in range ( n ) : NEW_LINE
Count of rows having even numbers
if ( row [ i ] % 2 == 0 ) : NEW_LINE INDENT r1 += 1 ; NEW_LINE DEDENT
Count of rows having odd numbers
if ( row [ i ] % 2 == 1 ) : NEW_LINE INDENT r2 += 1 ; NEW_LINE DEDENT
Count of columns having even numbers
if ( col [ i ] % 2 == 0 ) : NEW_LINE INDENT c1 += 1 ; NEW_LINE DEDENT
Count of columns having odd numbers
if ( col [ i ] % 2 == 1 ) : NEW_LINE INDENT c2 += 1 ; NEW_LINE DEDENT count = r1 * c1 + r2 * c2 ; NEW_LINE return count ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE q = [ [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 1 ] ] ; NEW_LINE size = len ( q ) ; NEW_LINE print ( findNumberOfEvenCells ( n , q , size ) ) ; NEW_LINE DEDENT
Function to return the maximum height which can 't be reached
def maxHeight ( h1 , h2 ) : NEW_LINE INDENT return ( ( h1 * h2 ) - h1 - h2 ) NEW_LINE DEDENT
Driver code
h1 = 7 NEW_LINE h2 = 5 NEW_LINE print ( max ( 0 , maxHeight ( h1 , h2 ) ) ) NEW_LINE
Python 3 implementation of fermat 's factorization
from math import ceil , sqrt NEW_LINE
This function finds the value of a and b and returns a + b and a - b
def FermatFactors ( n ) : NEW_LINE
since fermat 's factorization applicable for odd positive integers only
if ( n <= 0 ) : NEW_LINE INDENT return [ n ] NEW_LINE DEDENT
check if n is a even number
if ( n & 1 ) == 0 : NEW_LINE INDENT return [ n / 2 , 2 ] NEW_LINE DEDENT a = ceil ( sqrt ( n ) ) NEW_LINE
if n is a perfect root , then both its square roots are its factors
if ( a * a == n ) : NEW_LINE INDENT return [ a , a ] NEW_LINE DEDENT while ( True ) : NEW_LINE INDENT b1 = a * a - n NEW_LINE b = int ( sqrt ( b1 ) ) NEW_LINE if ( b * b == b1 ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT a += 1 NEW_LINE DEDENT DEDENT return [ a - b , a + b ] NEW_LINE
Driver Code
print ( FermatFactors ( 6557 ) ) NEW_LINE
Function to find the required numbers
def findNums ( arr , n ) : NEW_LINE
Find the sum and xor
S = 0 ; X = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT S += arr [ i ] ; NEW_LINE X ^= arr [ i ] ; NEW_LINE DEDENT
Print the required elements
print ( X , X + S ) ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findNums ( arr , n ) ; NEW_LINE DEDENT
Function to find the required values
def solve ( A , B ) : NEW_LINE INDENT p = B / 2 NEW_LINE M = int ( 4 * p ) NEW_LINE N = 1 NEW_LINE O = - 2 * A NEW_LINE Q = int ( A * A + 4 * p * p ) NEW_LINE return [ M , N , O , Q ] NEW_LINE DEDENT
Driver code
a = 1 NEW_LINE b = 1 NEW_LINE print ( * solve ( a , b ) ) NEW_LINE
Python3 implementation of the approach
from math import gcd as __gcd NEW_LINE
Function to return the largest number that divides the maximum elements from the given array
def findLargest ( arr , n ) : NEW_LINE
Finding gcd of all the numbers in the array
gcd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT gcd = __gcd ( arr [ i ] , gcd ) NEW_LINE DEDENT return gcd NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findLargest ( arr , n ) ) NEW_LINE DEDENT
Function to return the sum of digits of n
def digitSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += ( n % 10 ) ; NEW_LINE n //= 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT
Function that returns true if n is palindrome
def isPalindrome ( n ) : NEW_LINE
Find the appropriate divisor to extract the leading digit
divisor = 1 ; NEW_LINE while ( n // divisor >= 10 ) : NEW_LINE INDENT divisor *= 10 ; NEW_LINE DEDENT while ( n != 0 ) : NEW_LINE INDENT leading = n // divisor ; NEW_LINE trailing = n % 10 ; NEW_LINE DEDENT
If first and last digit not same return false
if ( leading != trailing ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT
Removing the leading and trailing digit from number
n = ( n % divisor ) // 10 ; NEW_LINE
Reducing divisor by a factor of 2 as 2 digits are dropped
divisor = divisor // 100 ; NEW_LINE return True ; NEW_LINE
Function that returns true if the digit sum of n is palindrome
def isDigitSumPalindrome ( n ) : NEW_LINE
Sum of the digits of n
sum = digitSum ( n ) ; NEW_LINE
If the digit sum is palindrome
if ( isPalindrome ( sum ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 56 ; NEW_LINE if ( isDigitSumPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Function to return n ^ n ^ ... k times
def xorK ( n , k ) : NEW_LINE
If k is odd the answer is the number itself
if ( k % 2 == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT
Else the answer is 0
return 0 NEW_LINE
Driver code
n = 123 NEW_LINE k = 3 NEW_LINE print ( xorK ( n , k ) ) NEW_LINE
Python3 implementation of the approach
N = 100005 NEW_LINE mod = ( int ) ( 1e9 + 7 ) NEW_LINE
To store the factorials and factorial mod inverse of the numbers
factorial = [ 0 ] * N ; NEW_LINE modinverse = [ 0 ] * N ; NEW_LINE
Function to return ( a ^ m1 ) % mod
def power ( a , m1 ) : NEW_LINE INDENT if ( m1 == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( m1 == 1 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT elif ( m1 == 2 ) : NEW_LINE INDENT return ( a * a ) % mod ; NEW_LINE DEDENT elif ( m1 & 1 ) : NEW_LINE INDENT return ( a * power ( power ( a , m1 // 2 ) , 2 ) ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return power ( power ( a , m1 // 2 ) , 2 ) % mod ; NEW_LINE DEDENT DEDENT
Function to find the factorials of all the numbers
def factorialfun ( ) : NEW_LINE INDENT factorial [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; NEW_LINE DEDENT DEDENT
Function to find factorial mod inverse of all the numbers
def modinversefun ( ) : NEW_LINE INDENT modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; NEW_LINE DEDENT DEDENT
Function to return nCr
def binomial ( n , r ) : NEW_LINE INDENT if ( r > n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; NEW_LINE a = ( a * modinverse [ r ] ) % mod ; NEW_LINE return a ; NEW_LINE DEDENT
Function to return the sum of the costs of all the possible arrangements of the cells
def arrange ( n , m , k ) : NEW_LINE INDENT factorialfun ( ) ; NEW_LINE modinversefun ( ) ; NEW_LINE ans = 0 ; NEW_LINE DEDENT
For all possible X 's
for i in range ( 1 , n ) : NEW_LINE INDENT ans += ( i * ( n - i ) * m * m ) % mod ; NEW_LINE DEDENT
For all possible Y 's
for i in range ( 1 , m ) : NEW_LINE INDENT ans += ( i * ( m - i ) * n * n ) % mod ; NEW_LINE DEDENT ans = ( ans * binomial ( n * m - 2 , k - 2 ) ) % mod ; NEW_LINE return int ( ans ) ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; m = 2 ; k = 2 ; NEW_LINE print ( arrange ( n , m , k ) ) ; NEW_LINE DEDENT
Function to print the Nth digit in the fraction ( p / q )
def findNthDigit ( p , q , N ) : NEW_LINE
While N > 0 compute the Nth digit by dividing p and q and store the result into variable res and go to next digit
while ( N > 0 ) : NEW_LINE INDENT N -= 1 ; NEW_LINE p *= 10 ; NEW_LINE res = p // q ; NEW_LINE p %= q ; NEW_LINE DEDENT return res ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = 1 ; q = 2 ; N = 1 ; NEW_LINE print ( findNthDigit ( p , q , N ) ) ; NEW_LINE DEDENT
Utility function to return the sum of the array
def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT
Function to return the sum of the modified array
def sumModArr ( arr , n ) : NEW_LINE INDENT subSum = arr [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT curr = arr [ i ] ; NEW_LINE DEDENT DEDENT
Subtract the subarray sum
arr [ i ] -= subSum ; NEW_LINE