text
stringlengths
1
636
code
stringlengths
8
1.89k
Iterate over [ 1 , N ]
for i in range ( 1 , N ) : NEW_LINE
If gcd is 1
if ( gcd ( i , N ) == 1 ) : NEW_LINE
Update sum
sum += i ; NEW_LINE
Return the final sum
return sum ; NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given N
N = 5 ; NEW_LINE
Function Call
print ( findSum ( N ) ) ; NEW_LINE
Function to prthe required count of pairs excluding the current element
def solve ( arr , n ) : NEW_LINE
Store the frequency
mp = { } NEW_LINE for i in arr : NEW_LINE INDENT mp [ i ] = mp . get ( i , 0 ) + 1 NEW_LINE DEDENT
Find all the count
cnt = 0 NEW_LINE for x in mp : NEW_LINE INDENT cnt += ( ( mp [ x ] ) * ( mp [ x ] - 1 ) // 2 ) NEW_LINE DEDENT ans = [ 0 ] * n NEW_LINE
Delete the contribution of each element for equal pairs
for i in range ( n ) : NEW_LINE INDENT ans [ i ] = cnt - ( mp [ arr [ i ] ] - 1 ) NEW_LINE DEDENT
Print the answer
for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array arr [ ]
arr = [ 1 , 1 , 2 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE
Function call
solve ( arr , N ) NEW_LINE
Function that prints the Mode values
def findMode ( a , n ) : NEW_LINE
Map used to mp integers to its frequency
mp = { } NEW_LINE
To store the maximum frequency
max = 0 NEW_LINE
To store the element with the maximum frequency
mode = 0 NEW_LINE
Loop used to read the elements one by one
for i in range ( n ) : NEW_LINE
Updates the frequency of that element
if a [ i ] in mp : NEW_LINE INDENT mp [ a [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ a [ i ] ] = 1 NEW_LINE DEDENT
Checks for maximum Number of occurrence
if ( mp [ a [ i ] ] >= max ) : NEW_LINE
Updates the maximum frequency
max = mp [ a [ i ] ] NEW_LINE
Updates the Mode
mode = a [ i ] NEW_LINE print ( mode , end = " ▁ " ) NEW_LINE
Driver Code
arr = [ 2 , 7 , 3 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE
Function call
findMode ( arr , n ) NEW_LINE
Recursive function to return the value of ( x ^ n ) % m
def modexp ( x , n , m ) : NEW_LINE
Base Case
if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT
If N is even
else : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return modexp ( ( x * x ) % m , n / 2 , m ) ; NEW_LINE DEDENT DEDENT
Else N is odd
else : NEW_LINE INDENT return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 , m ) % m ) NEW_LINE DEDENT
Function to find modular inverse of a number x under modulo m
def modInverse ( x , m ) : NEW_LINE
Using Fermat 's little theorem
return modexp ( x , m - 2 , m ) NEW_LINE
Function to count of numbers formed by shuffling the digits of a large number N
def countNumbers ( N ) : NEW_LINE
Modulo value
m = 1000000007 NEW_LINE
Array to store the factorials upto the maximum value of N
factorial = [ 0 for x in range ( 100001 ) ] NEW_LINE
Store factorial of i at index i
factorial [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , 100001 ) : NEW_LINE INDENT factorial [ i ] = ( factorial [ i - 1 ] * i ) % m NEW_LINE DEDENT
To store count of occurrence of a digit
count = [ 0 for x in range ( 10 ) ] NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT count [ i ] = 0 NEW_LINE DEDENT length = len ( N ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE
Increment the count of digit occured
count [ int ( N [ i ] ) ] += 1 NEW_LINE
Assign the factorial of length of input
result = factorial [ int ( length ) ] NEW_LINE
Multiplying result with the modulo multiplicative inverse of factorial of count of i
for i in range ( 0 , 10 ) : NEW_LINE INDENT result = ( result * modInverse ( factorial [ int ( count [ i ] ) ] , m ) ) % m NEW_LINE DEDENT
Print the result
print ( result ) NEW_LINE
Given number as string
N = "0223" ; NEW_LINE
Function call
countNumbers ( N ) NEW_LINE
To store the smallest prime factor till 10 ^ 5
spf = [ 0 for i in range ( 10001 ) ] NEW_LINE
Function to compute smallest prime factor array
def spf_array ( spf ) : NEW_LINE
Initialize the spf array first element
spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , 1000 , 1 ) : NEW_LINE
Marking smallest prime factor for every number to be itself
spf [ i ] = i NEW_LINE
Separately marking smallest prime factor for every even number as 2
for i in range ( 4 , 1000 , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT i = 3 NEW_LINE while ( i * i < 1000 ) : NEW_LINE
Checking if i is prime
if ( spf [ i ] == i ) : NEW_LINE
Marking SPF for all numbers divisible by i
j = i * i NEW_LINE while ( j < 1000 ) : NEW_LINE
Marking spf [ j ] if it is not previously marked
if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT j += i NEW_LINE i += 1 NEW_LINE
Function that finds minimum operation
def frequent_prime ( arr , N , K ) : NEW_LINE
Create a spf [ ] array
spf_array ( spf ) NEW_LINE
Map created to store the unique prime numbers
Hmap = { } NEW_LINE
To store the result
result = [ ] NEW_LINE i = 0 NEW_LINE
To store minimum operations
c = 0 NEW_LINE
To store every unique prime number
for i in range ( N ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE while ( x != 1 ) : NEW_LINE INDENT Hmap [ spf [ x ] ] = Hmap . get ( spf [ x ] , 0 ) + 1 NEW_LINE x = x // spf [ x ] NEW_LINE DEDENT DEDENT
Erase 1 as a key because it is not a prime number
if ( 1 in Hmap ) : NEW_LINE Hmap . pop ( 1 ) NEW_LINE for key , value in Hmap . items ( ) : NEW_LINE
First Prime Number
primeNum = key NEW_LINE frequency = value NEW_LINE
Frequency is divisible by K then insert primeNum in the result [ ]
if ( frequency % K == 0 ) : NEW_LINE INDENT result . append ( primeNum ) NEW_LINE DEDENT
Print the elements if it exists
result = result [ : : - 1 ] NEW_LINE if ( len ( result ) > 0 ) : NEW_LINE INDENT for it in result : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " { } " ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array arr [ ]
arr = [ 1 , 4 , 6 ] NEW_LINE
Given K
K = 1 NEW_LINE N = len ( arr ) NEW_LINE
Function Call
frequent_prime ( arr , N , K ) NEW_LINE
Function to print the first K multiples of N
def Kmultiples ( n , k ) : NEW_LINE INDENT a = n NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE DEDENT
Print the value of N * i
print ( " { } ▁ * ▁ { } ▁ = ▁ { } " . format ( n , i , a ) ) NEW_LINE j = 0 NEW_LINE
Iterate each bit of N and add pow ( 2 , pos ) , where pos is the index of each set bit
while ( n >= ( 1 << j ) ) : NEW_LINE
Check if current bit at pos j is fixed or not
a += n & ( 1 << j ) NEW_LINE
For next set bit
j += 1 NEW_LINE
Driver Code
N = 16 NEW_LINE K = 7 NEW_LINE Kmultiples ( N , K ) NEW_LINE
Function to calculate b
def calculateB ( x , y , n ) : NEW_LINE
sum of array x
sx = sum ( x ) NEW_LINE
sum of array y
sy = sum ( y ) NEW_LINE
for sum of product of x and y
sxsy = 0 NEW_LINE
sum of square of x
sx2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sxsy += x [ i ] * y [ i ] NEW_LINE sx2 += x [ i ] * x [ i ] NEW_LINE DEDENT b = ( n * sxsy - sx * sy ) / ( n * sx2 - sx * sx ) NEW_LINE return b NEW_LINE
Function to find the least regression line
def leastRegLine ( X , Y , n ) : NEW_LINE
Finding b
b = calculateB ( X , Y , n ) NEW_LINE meanX = int ( sum ( X ) / n ) NEW_LINE meanY = int ( sum ( Y ) / n ) NEW_LINE
Calculating a
a = meanY - b * meanX NEW_LINE
Printing regression line
print ( " Regression ▁ line : " ) NEW_LINE print ( " Y ▁ = ▁ " , ' % .3f ' % a , " ▁ + ▁ " , ' % .3f ' % b , " * X " , sep = " " ) NEW_LINE
Statistical data
X = [ 95 , 85 , 80 , 70 , 60 ] NEW_LINE Y = [ 90 , 80 , 70 , 65 , 60 ] NEW_LINE n = len ( X ) NEW_LINE leastRegLine ( X , Y , n ) NEW_LINE
Function that returns the count of repeating digits of the given number
def countRepeatingDigits ( N ) : NEW_LINE
Initialize a variable to store count of Repeating digits
res = 0 NEW_LINE
Initialize cnt array to store digit count
cnt = [ 0 ] * 10 NEW_LINE
Iterate through the digits of N
while ( N > 0 ) : NEW_LINE
Retrieve the last digit of N
rem = N % 10 NEW_LINE
Increase the count of digit
cnt [ rem ] += 1 NEW_LINE
Remove the last digit of N
N = N // 10 NEW_LINE
Iterate through the cnt array
for i in range ( 10 ) : NEW_LINE
If frequency of digit is greater than 1
if ( cnt [ i ] > 1 ) : NEW_LINE
Increment the count of Repeating digits
res += 1 NEW_LINE
Return count of repeating digit
return res NEW_LINE
Given array arr [ ]
N = 12 NEW_LINE
Function call
print ( countRepeatingDigits ( N ) ) NEW_LINE
Function for finding the temperature
def findTemperature ( x , y , s ) : NEW_LINE
Store Day1 - Day2 in diff
diff = ( x - y ) * 6 NEW_LINE Day2 = ( diff + s ) // 2 NEW_LINE
Remaining from s will be Day1
Day1 = s - Day2 NEW_LINE
Print Day1 and Day2
print ( " Day1 ▁ : ▁ " , Day1 ) NEW_LINE print ( " Day2 ▁ : ▁ " , Day2 ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 NEW_LINE y = 10 NEW_LINE s = 40 NEW_LINE DEDENT
Functions
findTemperature ( x , y , s ) NEW_LINE