text
stringlengths
1
636
code
stringlengths
8
1.89k
Function to find two numbers whose sum is N and do not contain any digit as k
def findAandB ( n , k ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT
Check every number i and ( n - i )
for i in range ( 1 , n ) : NEW_LINE
Check if i and n - i doesn 't contain k in them print i and n-i
if str ( i ) . count ( chr ( k + 48 ) ) == 0 and str ( n - i ) . count ( chr ( k + 48 ) ) == 0 : NEW_LINE INDENT print ( i , n - i ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT
check if flag is 0 then print - 1
if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given N and K
N = 100 NEW_LINE K = 0 NEW_LINE
Function Call
findAandB ( N , K ) NEW_LINE
Function to find the value of P * Q ^ - 1 mod 998244353
def calculate ( p , q ) : NEW_LINE INDENT mod = 998244353 NEW_LINE expo = 0 NEW_LINE expo = mod - 2 NEW_LINE DEDENT
Loop to find the value until the expo is not zero
while ( expo ) : NEW_LINE
Multiply p with q if expo is odd
if ( expo & 1 ) : NEW_LINE INDENT p = ( p * q ) % mod NEW_LINE DEDENT q = ( q * q ) % mod NEW_LINE
Reduce the value of expo by 2
expo >>= 1 NEW_LINE return p NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 1 NEW_LINE q = 4 NEW_LINE DEDENT
Function call
print ( calculate ( p , q ) ) NEW_LINE
Function that print two numbers with the sum X and maximum possible LCM
def maxLCMWithGivenSum ( X ) : NEW_LINE
If X is odd
if X % 2 != 0 : NEW_LINE INDENT A = X / 2 NEW_LINE B = X / 2 + 1 NEW_LINE DEDENT
If X is even
else : NEW_LINE
If floor ( X / 2 ) is even
if ( X / 2 ) % 2 == 0 : NEW_LINE INDENT A = X / 2 - 1 NEW_LINE B = X / 2 + 1 NEW_LINE DEDENT
If floor ( X / 2 ) is odd
else : NEW_LINE INDENT A = X / 2 - 2 NEW_LINE B = X / 2 + 2 NEW_LINE DEDENT
Print the result
print ( int ( A ) , int ( B ) , end = " ▁ " ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given Number
X = 30 NEW_LINE
Function call
maxLCMWithGivenSum ( X ) NEW_LINE
Function to find the longest subarray with sum is not divisible by k
def MaxSubarrayLength ( arr , n , k ) : NEW_LINE
left is the index of the leftmost element that is not divisible by k
left = - 1 NEW_LINE
sum of the array
sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE
Find the element that is not multiple of k
if ( ( arr [ i ] % k ) != 0 ) : NEW_LINE
left = - 1 means we are finding the leftmost element that is not divisible by k
if ( left == - 1 ) : NEW_LINE INDENT left = i NEW_LINE DEDENT
Updating the rightmost element
right = i NEW_LINE
Update the sum of the array up to the index i
sum += arr [ i ] NEW_LINE
Check if the sum of the array is not divisible by k , then return the size of array
if ( ( sum % k ) != 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT
All elements of array are divisible by k , then no such subarray possible so return - 1
elif ( left == - 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE
length of prefix elements that can be removed
prefix_length = left + 1 NEW_LINE
length of suffix elements that can be removed
suffix_length = n - right NEW_LINE
Return the length of subarray after removing the elements which have lesser number of elements
return n - min ( prefix_length , suffix_length ) NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 3 , 12 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( MaxSubarrayLength ( arr , n , K ) ) NEW_LINE DEDENT
Python3 implementation to find minimum steps to convert X to Y by repeated division and multiplication
def solve ( X , Y ) : NEW_LINE
Check if X is greater than Y then swap the elements
if ( X > Y ) : NEW_LINE INDENT temp = X NEW_LINE X = Y NEW_LINE Y = temp NEW_LINE DEDENT
Check if X equals Y
if ( X == Y ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT elif ( Y % X == 0 ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 ) NEW_LINE DEDENT
Driver code
X = 8 NEW_LINE Y = 13 NEW_LINE solve ( X , Y ) NEW_LINE
Python3 program for the above approach
from collections import defaultdict NEW_LINE
Function to count the quadruples
def countQuadraples ( N ) : NEW_LINE
Counter variable
cnt = 0 NEW_LINE
Map to store the sum of pair ( a ^ 2 + b ^ 2 )
m = defaultdict ( int ) NEW_LINE
Iterate till N
for a in range ( 1 , N + 1 ) : NEW_LINE INDENT for b in range ( 1 , N + 1 ) : NEW_LINE DEDENT
Calculate a ^ 2 + b ^ 2
x = a * a + b * b NEW_LINE
Increment the value in map
m [ x ] += 1 NEW_LINE for c in range ( 1 , N + 1 ) : NEW_LINE for d in range ( 1 , N + 1 ) : NEW_LINE x = c * c + d * d NEW_LINE
Check if this sum was also in a ^ 2 + b ^ 2
if x in m : NEW_LINE INDENT cnt += m [ x ] NEW_LINE DEDENT
Return the count
return cnt NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Given N
N = 2 NEW_LINE
Function Call
print ( countQuadraples ( N ) ) NEW_LINE
function to find the number of pairs satisfying the given cond .
def count_pairs ( a , b , N ) : NEW_LINE
count variable to store the count of possible pairs
count = 0 ; NEW_LINE
Nested loop to find out the possible pairs
for i in range ( 0 , N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE DEDENT
Check if the given condition is satisfied or not . If yes then increment the count .
if ( ( a [ i ] + a [ j ] ) > ( b [ i ] + b [ j ] ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT
Return the count value
return count ; NEW_LINE
Size of the arrays
N = 5 ; NEW_LINE
Initialise the arrays
a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE b = [ 2 , 5 , 6 , 1 , 9 ] ; NEW_LINE
Python3 program of the above approach
from bisect import bisect_left NEW_LINE
Function to find the number of pairs .
def numberOfPairs ( a , b , n ) : NEW_LINE
Array c [ ] where c [ i ] = a [ i ] - b [ i ]
c = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = a [ i ] - b [ i ] NEW_LINE DEDENT
Sort the array c
c = sorted ( c ) NEW_LINE
Initialise answer as 0
answer = 0 NEW_LINE
Iterate from index 0 to n - 1
for i in range ( 1 , n ) : NEW_LINE
If c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i
if ( c [ i ] <= 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT
Find the minimum index such that c [ i ] + c [ j ] > 0 which is equivalent to c [ j ] >= - c [ i ] + 1
pos = bisect_left ( c , - c [ i ] + 1 ) NEW_LINE
Add ( i - pos ) to answer
answer += ( i - pos ) NEW_LINE
Return the answer
return answer NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Number of elements in a and b
n = 5 NEW_LINE
Array a
a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE
Array b
b = [ 2 , 5 , 6 , 1 , 9 ] NEW_LINE print ( numberOfPairs ( a , b , n ) ) NEW_LINE
Function to find the K - value for every index in the array
def print_h_index ( arr , N ) : NEW_LINE
Multiset to store the array in the form of red - black tree
ms = [ ] NEW_LINE
Iterating over the array
for i in range ( N ) : NEW_LINE
Inserting the current value in the multiset
ms . append ( arr [ i ] ) NEW_LINE ms . sort ( ) NEW_LINE
Condition to check if the smallest value in the set is less than it 's size
if ( ms [ 0 ] < len ( ms ) ) : NEW_LINE
Erase the smallest value
ms . pop ( 0 ) NEW_LINE
h - index value will be the size of the multiset
print ( len ( ms ) , end = ' ▁ ' ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Array
arr = [ 9 , 10 , 7 , 5 , 0 , 10 , 2 , 0 ] NEW_LINE
Size of the array
N = len ( arr ) NEW_LINE
Function call
print_h_index ( arr , N ) NEW_LINE
Function to find count of prime
def findPrimes ( arr , n ) : NEW_LINE
Find maximum value in the array
max_val = max ( arr ) NEW_LINE
Find and store all prime numbers up to max_val using Sieve Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true .
prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE
Remaining part of SIEVE
prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while ( p * p <= max_val ) : NEW_LINE
If prime [ p ] is not changed , then it is a prime
if ( prime [ p ] == True ) : NEW_LINE
Update all multiples of p
for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT p += 1 NEW_LINE return prime ; NEW_LINE
Function to print Non - repeating primes
def nonRepeatingPrimes ( arr , n ) : NEW_LINE
Precompute primes using Sieve
prime = findPrimes ( arr , n ) ; NEW_LINE
Create HashMap to store frequency of prime numbers
mp = dict ( ) NEW_LINE
Traverse through array elements and Count frequencies of all primes
for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( arr [ i ] in mp ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT
Traverse through map and print non repeating primes
for entry in mp . keys ( ) : NEW_LINE INDENT if ( mp [ entry ] == 1 ) : NEW_LINE INDENT print ( entry ) ; NEW_LINE DEDENT DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 6 , 7 , 9 , 7 , 23 , 21 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE nonRepeatingPrimes ( arr , n ) ; NEW_LINE DEDENT
Function to generate prefix product array
def prefixProduct ( a , n ) : NEW_LINE
Update the array with the product of prefixes
for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i ] * a [ i - 1 ] ; NEW_LINE DEDENT
Print the array
for j in range ( 0 , n ) : NEW_LINE INDENT print ( a [ j ] , end = " , ▁ " ) ; NEW_LINE DEDENT return 0 ; NEW_LINE
Driver Code
arr = [ 2 , 4 , 6 , 5 , 10 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE prefixProduct ( arr , N ) ; NEW_LINE
Function to find the number of ways to distribute N items among 3 people
def countWays ( N ) : NEW_LINE