text
stringlengths
1
636
code
stringlengths
8
1.89k
Driver Code
N = 5 NEW_LINE arr = [ 2 , 4 , 4 , 0 , 2 ] NEW_LINE print ( totalways ( arr , N ) ) NEW_LINE
Function to implement proizvolov 's identity
def proizvolov ( a , b , n ) : NEW_LINE
According to proizvolov 's identity
return n * n NEW_LINE
Driver code
a = [ 1 , 5 , 6 , 8 , 10 ] NEW_LINE b = [ 9 , 7 , 4 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE
Function call
print ( proizvolov ( a , b , n , ) ) NEW_LINE
Function to calculate ln x using expansion
from math import pow NEW_LINE def calculateLnx ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE num = ( n - 1 ) / ( n + 1 ) NEW_LINE DEDENT
terminating value of the loop can be increased to improve the precision
for i in range ( 1 , 1001 , 1 ) : NEW_LINE INDENT mul = ( 2 * i ) - 1 NEW_LINE cal = pow ( num , mul ) NEW_LINE cal = cal / mul NEW_LINE sum = sum + cal NEW_LINE DEDENT sum = 2 * sum NEW_LINE return sum NEW_LINE
Function to calculate log10 x
def calculateLogx ( lnx ) : NEW_LINE INDENT return ( lnx / 2.303 ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE lnx = calculateLnx ( n ) NEW_LINE logx = calculateLogx ( lnx ) NEW_LINE DEDENT
setprecision ( 3 ) is used to display the output up to 3 decimal places
print ( " ln " , " { 0 : . 3f } " . format ( n ) , " = " , " { 0 : . 3f } " . format ( lnx ) ) NEW_LINE print ( " log10" , " { 0 : . 3f } " . format ( n ) , " = " , " { 0 : . 3f } " . format ( logx ) ) NEW_LINE
Function to return the required ssum
def Sum ( A , B , R ) : NEW_LINE
To store the ssum
ssum = 0 NEW_LINE
For every row
for i in range ( 1 , R + 1 ) : NEW_LINE
Update the ssum as A appears i number of times in the current row
ssum = ssum + ( i * A ) NEW_LINE
Update A for the next row
A = A + B NEW_LINE
Return the ssum
return ssum NEW_LINE
Driver code
A , B , R = 5 , 3 , 3 NEW_LINE print ( Sum ( A , B , R ) ) NEW_LINE
Function to return the smallest prime factor of n
def smallestPrimeFactor ( n ) : NEW_LINE
Initialize i = 2
i = 2 NEW_LINE
While i <= sqrt ( n )
while ( i * i ) <= n : NEW_LINE
If n is divisible by i
if n % i == 0 : NEW_LINE INDENT return i NEW_LINE DEDENT
Increment i
i += 1 NEW_LINE return n NEW_LINE
Function to print the first n terms of the required sequence
def solve ( n ) : NEW_LINE
To store the product of the previous terms
product = 1 NEW_LINE
Traverse the prime numbers
i = 0 NEW_LINE while i < n : NEW_LINE
Current term will be smallest prime factor of ( 1 + product of all previous terms )
num = smallestPrimeFactor ( product + 1 ) NEW_LINE
Print the current term
print ( num , end = ' ▁ ' ) NEW_LINE
Update the product
product = product * num NEW_LINE i += 1 NEW_LINE
Find the first 14 terms of the sequence
b = 14 NEW_LINE solve ( b ) NEW_LINE
Function to return the sum of the count of set bits in the integers from 1 to n
def countSetBits ( n ) : NEW_LINE
Ignore 0 as all the bits are unset
n += 1 ; NEW_LINE
To store the powers of 2
powerOf2 = 2 ; NEW_LINE
To store the result , it is initialized with n / 2 because the count of set least significant bits in the integers from 1 to n is n / 2
cnt = n // 2 ; NEW_LINE
Loop for every bit required to represent n
while ( powerOf2 <= n ) : NEW_LINE
Total count of pairs of 0 s and 1 s
totalPairs = n // powerOf2 ; NEW_LINE
totalPairs / 2 gives the complete count of the pairs of 1 s Multiplying it with the current power of 2 will give the count of 1 s in the current bit
cnt += ( totalPairs // 2 ) * powerOf2 ; NEW_LINE
If the count of pairs was odd then add the remaining 1 s which could not be groupped together
if ( totalPairs & 1 ) : NEW_LINE INDENT cnt += ( n % powerOf2 ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 0 NEW_LINE DEDENT
Next power of 2
powerOf2 <<= 1 ; NEW_LINE
Return the result
return cnt ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 14 ; NEW_LINE print ( countSetBits ( n ) ) ; NEW_LINE DEDENT
Function to return the height of the right - angled triangle whose area is X times its base
def getHeight ( X ) : NEW_LINE INDENT return ( 2 * X ) NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 35 NEW_LINE print ( getHeight ( X ) ) NEW_LINE DEDENT
Function to return the sum of inverse of divisors
def SumofInverseDivisors ( N , Sum ) : NEW_LINE
Calculating the answer
ans = float ( Sum ) * 1.0 / float ( N ) ; NEW_LINE
Return the answer
return round ( ans , 2 ) ; NEW_LINE
Driver code
N = 9 ; NEW_LINE Sum = 13 ; NEW_LINE
Function call
print SumofInverseDivisors ( N , Sum ) ; NEW_LINE
Function to return the number of triplets
def NoofTriplets ( N , K ) : NEW_LINE
Initializing the count array
cnt = [ 0 ] * K ; NEW_LINE
Storing the frequency of each modulo class
for i in range ( 1 , N + 1 ) : NEW_LINE INDENT cnt [ i % K ] += 1 ; NEW_LINE DEDENT
If K is odd
if ( K & 1 ) : NEW_LINE INDENT rslt = cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; NEW_LINE return rslt NEW_LINE DEDENT
If K is even
else : NEW_LINE INDENT rslt = ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K // 2 ] * cnt [ K // 2 ] * cnt [ K // 2 ] ) ; NEW_LINE return rslt NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 ; K = 2 ; NEW_LINE DEDENT
Function Call
print ( NoofTriplets ( N , K ) ) ; NEW_LINE
Function to compute number using our deduced formula
def findNumber ( n ) : NEW_LINE
Initialize num to n - 1
num = n - 1 ; NEW_LINE num = 2 * ( 4 ** num ) ; NEW_LINE num = num // 3 ; NEW_LINE return num ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( findNumber ( n ) ) ; NEW_LINE DEDENT
Python3 implementation of the approach
from operator import xor NEW_LINE
Function to return the XOR of elements from the range [ 1 , n ]
def findXOR ( n ) : NEW_LINE INDENT mod = n % 4 ; NEW_LINE DEDENT
If n is a multiple of 4
if ( mod == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT
If n % 4 gives remainder 1
elif ( mod == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT
If n % 4 gives remainder 2
elif ( mod == 2 ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT
If n % 4 gives remainder 3
elif ( mod == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT
Function to return the XOR of elements from the range [ l , r ]
def findXORFun ( l , r ) : NEW_LINE INDENT return ( xor ( findXOR ( l - 1 ) , findXOR ( r ) ) ) ; NEW_LINE DEDENT
Driver code
l = 4 ; r = 8 ; NEW_LINE print ( findXORFun ( l , r ) ) ; NEW_LINE
Function to return the GCD of a and b
def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return GCD ( b , a % b ) ; NEW_LINE DEDENT
Function to return the count of reachable integers from the given array
def findReachable ( arr , D , A , B , n ) : NEW_LINE
GCD of A and B
gcd_AB = GCD ( A , B ) ; NEW_LINE
To store the count of reachable integers
count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE
If current element can be reached
if ( ( arr [ i ] - D ) % gcd_AB == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT
Return the count
return count ; NEW_LINE
Driver code
arr = [ 4 , 5 , 6 , 7 , 8 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE D = 4 ; A = 4 ; B = 6 ; NEW_LINE print ( findReachable ( arr , D , A , B , n ) ) ; NEW_LINE
Iterative Function to calculate ( x ^ y ) in O ( log y )
def power ( x , y ) : NEW_LINE
Initialize result
res = 1 ; NEW_LINE while ( y > 0 ) : NEW_LINE
If y is odd , multiply x with result
if ( y % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) ; NEW_LINE DEDENT
y must be even now y = y / 2
y = int ( y ) >> 1 ; NEW_LINE x = ( x * x ) ; NEW_LINE return res ; NEW_LINE
Function to return the count of required trees
def solve ( L ) : NEW_LINE
number of nodes
n = L / 2 + 1 ; NEW_LINE ans = power ( n , n - 2 ) ; NEW_LINE
Return the result
return int ( ans ) ; NEW_LINE
Driver code
L = 6 ; NEW_LINE print ( solve ( L ) ) ; NEW_LINE
Python program to change one element of an array such that the resulting array is in arithmetic progression .
def makeAP ( arr , n ) : NEW_LINE INDENT initial_term , common_difference = 0 , 0 NEW_LINE DEDENT
Finds the initial term and common difference and prints the resulting array .
if ( n == 3 ) : NEW_LINE INDENT common_difference = arr [ 2 ] - arr [ 1 ] NEW_LINE initial_term = arr [ 1 ] - common_difference NEW_LINE DEDENT elif ( ( arr [ 1 ] - arr [ 0 ] ) == arr [ 2 ] - arr [ 1 ] ) : NEW_LINE
Check if the first three elements are in arithmetic progression
initial_term = arr [ 0 ] NEW_LINE common_difference = arr [ 1 ] - arr [ 0 ] NEW_LINE elif ( ( arr [ 2 ] - arr [ 1 ] ) == ( arr [ 3 ] - arr [ 2 ] ) ) : NEW_LINE
Check if the first element is not in arithmetic progression
common_difference = arr [ 2 ] - arr [ 1 ] NEW_LINE initial_term = arr [ 1 ] - common_difference NEW_LINE else : NEW_LINE
The first and fourth element are in arithmetic progression
common_difference = ( arr [ 3 ] - arr [ 0 ] ) / 3 NEW_LINE initial_term = arr [ 0 ] NEW_LINE
Print the arithmetic progression
for i in range ( n ) : NEW_LINE INDENT print ( int ( initial_term + ( i * common_difference ) ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE
Driver code
arr = [ 1 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE makeAP ( arr , n ) NEW_LINE
Function to return the highest power of p that divides n ! implementing Legendre Formula
def getfactor ( n , p ) : NEW_LINE INDENT pw = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT n //= p ; NEW_LINE pw += n ; NEW_LINE DEDENT DEDENT
Return the highest power of p which divides n !
return pw ; NEW_LINE
Function that returns true if nCr is divisible by p
def isDivisible ( n , r , p ) : NEW_LINE
Find the highest powers of p that divide n ! , r ! and ( n - r ) !
x1 = getfactor ( n , p ) ; NEW_LINE x2 = getfactor ( r , p ) ; NEW_LINE x3 = getfactor ( n - r , p ) ; NEW_LINE
If nCr is divisible by p
if ( x1 > x2 + x3 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 ; r = 2 ; p = 7 ; NEW_LINE if ( isDivisible ( n , r , p ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Function that returns true if the number represented by arr [ ] is even in base r
def isEven ( arr , n , r ) : NEW_LINE
If the base is even , then the last digit is checked
if ( r % 2 == 0 ) : NEW_LINE INDENT if ( arr [ n - 1 ] % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT
If base is odd , then the number of odd digits are checked
else : NEW_LINE
To store the count of odd digits
oddCount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( oddCount % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT
Number is odd
return False NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE r = 2 NEW_LINE if ( isEven ( arr , n , r ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT DEDENT
Python implementation of the approach
import sys NEW_LINE