text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Average of Cubes of first N natural numbers | Function to find average of cubes ; Storing sum of cubes of numbers in sum ; Calculate sum of cubes ; Return average ; Driver Code ; Given Number ; Function Call
def findAverageOfCube ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += i * i * i NEW_LINE DEDENT return round ( sum / n , 6 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( findAverageOfCube ( n ) ) NEW_LINE DEDENT
Smallest and Largest N | Python3 program for the above approach ; Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting ...
import math NEW_LINE def findNumberL ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return "1" NEW_LINE DEDENT result = " " NEW_LINE length = math . floor ( math . log10 ( n ) + 1 ) NEW_LINE for i in range ( 1 , n - ( 2 * length ) + 1 ) : NEW_LINE INDENT result += '9' NEW_LINE DEDENT result = ( str ( n ) + res...
Maximum number of 0 s that can be flipped such that Array has no adjacent 1 s | Maximum number of 0 s that can be replaced by 1 ; Check for three consecutive 0 s ; Flip the bit ; Increase the count ; Driver code
def canReplace ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE count = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == 0 and ( i == 0 or arr [ i - 1 ] == 0 ) and ( i == n - 1 or arr [ i + 1 ] == 0 ) ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE count += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW...
Sum of first N Star Numbers | Function to find the sum of the first N star number ; Variable to store the sum ; Driver code
def sum_star_num ( n ) : NEW_LINE INDENT summ = 2 * n * ( n + 1 ) * ( n - 1 ) + n NEW_LINE return summ NEW_LINE DEDENT n = 3 NEW_LINE print ( sum_star_num ( n ) ) NEW_LINE
Count distinct median possible for an Array using given ranges of elements | Function to count the number of distinct medians of an array where each array elements are given by a range ; Loop to store the starting and end range in the array ; Condition to check if the length of the array is odd ; Driver Code ; Function...
def solve ( n , vec ) : NEW_LINE INDENT a = [ ] NEW_LINE b = [ ] NEW_LINE for pr in vec : NEW_LINE INDENT a . append ( pr [ 0 ] ) NEW_LINE b . append ( pr [ 1 ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE if ( ( n & 1 ) ) : NEW_LINE INDENT left = a [ n // 2 ] NEW_LINE right = b [ n // 2 ] NEW_LINE ans...
Count of pairs from Array with sum equal to twice their bitwise AND | Python3 implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the fr...
from collections import defaultdict NEW_LINE mp = defaultdict ( int ) NEW_LINE def find_pairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in mp . values ( ) : NEW_LINE INDENT count = i NEW_LINE if ( count > 1 ) : NEW_LINE INDENT ans +...
360 | Function to find the nth 360 - gon Number ; Driver Code
def gonNum360 ( n ) : NEW_LINE INDENT return ( 358 * n * n - 356 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( gonNum360 ( n ) ) ; NEW_LINE
120 | Function to find the nth 120 - gon Number ; Driver Code
def gonNum120 ( n ) : NEW_LINE INDENT return ( 118 * n * n - 116 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( gonNum120 ( n ) ) ; NEW_LINE
Tetracontaoctagonal Number | Function to find the nth Tetracontaoctagonal Number ; Driver Code
def TetracontaoctagonalNum ( n ) : NEW_LINE INDENT return ( 46 * n * n - 44 * n ) / 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( TetracontaoctagonalNum ( n ) ) ; NEW_LINE
257 | Function to find the nth 257 - gon Number ; Driver Code
def gonNum257 ( n ) : NEW_LINE INDENT return ( 255 * n * n - 253 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( gonNum257 ( n ) ) ; NEW_LINE
Tetracontadigonal Number | Function to find the nth tetracontadigonal number ; Driver Code
def TetracontadigonalNum ( n ) : NEW_LINE INDENT return int ( ( 40 * n * n - 38 * n ) / 2 ) NEW_LINE DEDENT n = 3 NEW_LINE print ( TetracontadigonalNum ( n ) ) NEW_LINE
Index of smallest triangular number with N digits | Python3 implementation of the above approach ; Function to return index of smallest triangular no n digits ; Driver Code
import math NEW_LINE def findIndex ( n ) : NEW_LINE INDENT x = math . sqrt ( 2 * math . pow ( 10 , ( n - 1 ) ) ) ; NEW_LINE return round ( x ) ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( findIndex ( n ) ) ; NEW_LINE
Program to find the LCM of two prime numbers | Function to return the LCM of two prime numbers ; If the two numbers are equal then return any one of a and b ; Else return product of the numbers ; Driver code ; Given two numbers ; Function Call
def findLCMPrime ( a , b ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return a * b ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; b = 5 ; NEW_LINE print ( findLCMPrime ( a , b ) ) ; NEW_LINE DEDENT
Smallest multiple of N with exactly N digits in its Binary number representation | Python3 program to find smallest multiple of n with exactly N digits in Binary number System . ; Function to find smallest multiple of n with exactly n digits in Binary number representation . ; Driver code
from math import ceil NEW_LINE def smallestNumber ( N ) : NEW_LINE INDENT print ( N * ceil ( pow ( 2 , ( N - 1 ) ) / N ) ) NEW_LINE DEDENT N = 3 NEW_LINE smallestNumber ( N ) NEW_LINE
Find the largest N digit multiple of N | Python3 program to find largest multiple of N containing N digits ; Function to find the largest N digit multiple of N ; Driver code
from math import floor NEW_LINE def smallestNumber ( N ) : NEW_LINE INDENT print ( N * floor ( ( pow ( 10 , N ) - 1 ) / N ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE smallestNumber ( N ) NEW_LINE DEDENT
Icosikaiheptagonal Number | Function to find the nth icosikaiheptagonal Number ; Driver code
def icosikaiheptagonalNum ( n ) : NEW_LINE INDENT return ( 25 * n * n - 23 * n ) // 2 ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( "3rd ▁ icosikaiheptagonal ▁ Number ▁ is ▁ " , icosikaiheptagonalNum ( n ) ) ; NEW_LINE
Program to check if N is a triacontagonal number | Python3 program to check whether a number is an triacontagonal number or not ; Function to check whether a number is an triacontagonal number or not ; Condition to check whether a number is an triacontagonal number or not ; Given number ; Function call
import math ; NEW_LINE def istriacontagonal ( N ) : NEW_LINE INDENT n = ( 26 + math . sqrt ( 224 * N + 676 ) ) // 56 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT i = 30 ; NEW_LINE if ( istriacontagonal ( i ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; ...
Check whether one root of the Quadratic Equation is twice of other or not | Function to find the required answer ; Driver code
def checkSolution ( a , b , c ) : NEW_LINE INDENT if ( 2 * b * b == 9 * a * c ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT a = 1 ; b = 3 ; c = 2 ; NEW_LINE checkSolution ( a , b , c ) ; NEW_LINE
Parity of the given mathematical expression using given N numbers | Python3 program to determine the parity of the given mathematical expression ; Iterating through the given integers ; If any odd number is present , then S is even parity ; Else , S is odd parity ; Driver code
def getParity ( n , A ) : NEW_LINE INDENT for x in A : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Odd " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE A = [ 2 , 3 , 1 ] NEW_LINE getParity ( N , A ) NEW_LINE DEDEN...
Count of digits after concatenation of first N positive integers | Python3 program to find the number of digits after concatenating the first N positive integers ; Function to find the number of digits after concatenating the first N positive integers ; Driver code
from math import log10 , floor NEW_LINE def numberOfDigits ( N ) : NEW_LINE INDENT nod = floor ( log10 ( N ) + 1 ) ; NEW_LINE toDecrease = ( pow ( 10 , nod ) - 1 ) // 9 NEW_LINE print ( ( N + 1 ) * nod - toDecrease ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 NEW_LINE numberOfDigits ( N ) ...
Length of maximum product subarray | function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; if zeroindex list is empty then Maxlength is as size of array ; if zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before in...
def Maxlength ( arr , N ) : NEW_LINE INDENT zeroindex = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT zeroindex . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( zeroindex ) == 0 ) : NEW_LINE INDENT maxlen = N NEW_LINE DEDENT else : NEW_LINE INDENT maxlen = zeroindex [ 0 ] N...
Check if sum of exactly K elements of the Array can be odd or not | Function returns true if it is possible to have odd sum ; Counting number of odd and even elements ; Driver code
def isPossible ( arr , N , K ) : NEW_LINE INDENT oddCount = 0 NEW_LINE evenCount = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT evenCount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( evenCount == N or ( oddCount == N and K % 2 == ...
Find the last two digits of Factorial of a given Number | Function to print the last two digits of N ! ; For N >= 10 , N ! % 100 will always be 0 ; Calculating N ! % 100 ; Driver code
def lastTwoDigits ( N ) : NEW_LINE INDENT if ( N >= 10 ) : NEW_LINE INDENT print ( "00" , end = " " ) NEW_LINE return NEW_LINE DEDENT fac = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fac = ( fac * i ) % 100 NEW_LINE DEDENT print ( fac ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N ...
Product of N terms of a given Geometric series | Function to calculate product of geometric series ; Return the final product with the above formula ; Given first term and common ratio ; Number of terms ; Function Call
def productOfGP ( a , r , n ) : NEW_LINE INDENT return pow ( a , n ) * pow ( r , n * ( n - 1 ) // 2 ) ; NEW_LINE DEDENT a = 1 ; r = 2 ; NEW_LINE N = 4 ; NEW_LINE print ( productOfGP ( a , r , N ) ) ; NEW_LINE
Product of N terms of a given Geometric series | Python3 program for the above approach ; Function to calculate product of N terms of geometric series ; Find the product of first and the last term ; Return the sqrt of the above expression to find the product ; Given first term and common ratio ; Number of terms ; Funct...
import math NEW_LINE def productOfGP ( a , r , n ) : NEW_LINE INDENT an = a * pow ( r , n - 1 ) ; NEW_LINE return ( math . sqrt ( pow ( a * an , n ) ) ) NEW_LINE DEDENT a = 1 NEW_LINE r = 2 ; NEW_LINE N = 4 ; NEW_LINE print ( productOfGP ( a , r , N ) ) NEW_LINE
Find two numbers such that difference of their squares equal to N | Python3 Program to find two numbers with difference of their squares equal to N ; Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ;...
from math import sqrt NEW_LINE def solve ( n ) : NEW_LINE INDENT for x in range ( 1 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % x == 0 ) : NEW_LINE INDENT small = x ; NEW_LINE big = n // x ; NEW_LINE if ( small % 2 == big % 2 ) : NEW_LINE INDENT a = ( small + big ) // 2 ; NEW_LINE b = ( big - small ) // 2 ; N...
Probability that an arbitrary positive divisor of 10 ^ X is an integral multiple of 10 ^ Y | Python3 program to find the probability of an arbitrary positive divisor of Xth power of 10 to be a multiple of Yth power of 10 ; Function to calculate and print the required probability ; Count of potential divisors of X - th ...
from math import * NEW_LINE def prob ( x , y ) : NEW_LINE INDENT num = abs ( x - y + 1 ) * abs ( x - y + 1 ) NEW_LINE den = ( x + 1 ) * ( x + 1 ) NEW_LINE gcd1 = gcd ( num , den ) NEW_LINE print ( num // gcd1 , end = " " ) NEW_LINE print ( " / " , end = " " ) NEW_LINE print ( den // gcd1 ) NEW_LINE DEDENT if __name__ =...
Program to check if N is a Chiliagon Number | Python3 for the above approach ; Function to check that if N is Chiliagon Number or not ; Condition to check if N is a Chiliagon Number ; Given Number ; Function call
import math ; NEW_LINE def is_Chiliagon ( N ) : NEW_LINE INDENT n = ( 996 + math . sqrt ( 7984 * N + 992016 ) ) // 1996 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT N = 1000 ; NEW_LINE if ( is_Chiliagon ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ;...
Count of common subarrays in two different permutations of 1 to N | Python3 implementation of above approach ; Initialising Map for Index Mapping ; Mapping elements of A ; Modify elements of B according to Map ; Changing B [ i ] as the index of B [ i ] in A ; Count of common subarrays ; Traversing array B ; While conse...
def commonSubarrays ( A , B , N ) : NEW_LINE INDENT Map = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT Map [ A [ i ] ] = i NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT B [ i ] = Map [ B [ i ] ] NEW_LINE DEDENT count = 0 NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT K ...
Represent N as sum of K even or K odd numbers with repetitions allowed | Function to find the array with all the even / odd elements ; First let 's check kth is odd or even ; if last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element...
def getArrayOfSizeK ( n , k ) : NEW_LINE INDENT ans = [ ] NEW_LINE odd = n - ( ( k - 1 ) * 1 ) NEW_LINE if ( odd > 0 and odd % 2 != 0 ) : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT ans . append ( 1 ) NEW_LINE DEDENT ans . append ( odd ) NEW_LINE DEDENT even = n - ( ( k - 1 ) * 2 ) NEW_LINE if ( even > 0...
Check whether a given number N is a Nude Number or not | Check if all digits of num divide num ; Array to store all digits of the given number ; If any of the condition is true for any digit then N is not a nude number ; Driver code
def checkDivisbility ( num ) : NEW_LINE INDENT digit = 0 NEW_LINE N = num NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 NEW_LINE num = num // 10 NEW_LINE if ( digit == 0 or N % digit != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ '...
Hensel 's Lemma | Function to find the modular inverse of a modulo m ; Apply the Euclidean algorithm , to find the modular inverse ; Function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; Function to find the image of x in f ( x ) = x ^ 3 - k . ; Function to find the next power of the number ; Next power...
def inv ( a , m ) : NEW_LINE INDENT m0 = m NEW_LINE x0 = 0 NEW_LINE x1 = 1 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m = a % m NEW_LINE a = t NEW_LINE t = x0 NEW_LINE x0 = x1 - q * x0 NEW_LINE x1 = t NEW_LINE DEDENT if ( x1 < 0...
Count of numbers with all digits same in a given range | Python3 program to count the total numbers in the range L and R which have all the digit same ; Function that count the total numbersProgram between L and R which have all the digit same ; length of R ; tmp has all digits as 1 ; For each multiple of tmp in range ...
import math NEW_LINE def count_same_digit ( L , R ) : NEW_LINE INDENT tmp = 0 ; ans = 0 ; NEW_LINE n = int ( math . log10 ( R ) + 1 ) ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tmp = tmp * 10 + 1 ; NEW_LINE for j in range ( 1 , 9 ) : NEW_LINE INDENT if ( L <= ( tmp * j ) and ( tmp * j ) <= R ) : NEW_LINE IND...
Minimum LCM of all pairs in a given array | Python3 program to find the pair having minimum LCM ; function that return pair having minimum LCM ; find max element in the array as the gcd of two elements from the array can 't greater than max element. ; created a 2D array to store minimum two multiple of any particular i...
import sys NEW_LINE def minLCM ( arr , n ) : NEW_LINE INDENT mx = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) NEW_LINE DEDENT mul = [ [ ] for i in range ( mx + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( mul [ arr [ i ] ] ) > 1 ) : NEW_LINE INDENT continue NEW_LINE...
Count of triplets of numbers 1 to N such that middle element is always largest | Function to find Number of triplets for given Number N such that middle element is always greater than left and right side element . ; Check if arrangement is possible or Not ; Else return total ways ; Driver code .
def findArrangement ( N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) // 3 ; NEW_LINE DEDENT N = 10 ; NEW_LINE print ( findArrangement ( N ) ) ; NEW_LINE
Lexicographically smallest array formed by at most one swap for every pair of adjacent indices | Function to find the lexicographically smallest array ; Maximum swaps possible ; Hash to store swaps performed ; Let current element be the minimum possible ; Find actual position of the minimum element ; Update minimum ele...
def findSmallestArray ( A , n ) : NEW_LINE INDENT count = n - 1 NEW_LINE mp = { ' ' } NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( count <= 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT mn = A [ i ] NEW_LINE pos = i NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( A [ j ] < mn ) : NEW_LINE INDEN...
Sum of all perfect square divisors of numbers from 1 to N | Python3 program to find the sum of all perfect square divisors of numbers from 1 to N ; Function for finding inverse of a number iteratively Here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to N ; Sto...
from math import * NEW_LINE MOD = 1000000007 NEW_LINE def inv ( a ) : NEW_LINE INDENT o = 1 NEW_LINE p = MOD - 2 NEW_LINE while ( p > 0 ) : NEW_LINE INDENT if ( p % 2 == 1 ) : NEW_LINE INDENT o = ( o * a ) % MOD NEW_LINE DEDENT a = ( a * a ) % MOD NEW_LINE p >>= 1 NEW_LINE DEDENT return o NEW_LINE DEDENT inv6 = inv ( 6...
Check whether the binary equivalent of a number ends with "001" or not | Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
def isSuffix ( s1 , s2 ) : NEW_LINE INDENT n1 = len ( s1 ) ; NEW_LINE n2 = len ( s2 ) ; NEW_LINE if ( n1 > n2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( n1 ) : NEW_LINE INDENT if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW...
Check whether the binary equivalent of a number ends with "001" or not | Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
def CheckBinaryEquivalent ( N ) : NEW_LINE INDENT return ( N - 1 ) % 8 == 0 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; NEW_LINE if ( CheckBinaryEquivalent ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Panarithmic numbers within a given range | Python3 program to print Practical Numbers in given range ; Function to compute divisors of a number ; Vector to store divisors ; 1 will always be a divisor ; Check if i is squareroot of A then only one time insert it in ans ; Function to check that a number can be represented...
import math NEW_LINE def get_divisors ( A ) : NEW_LINE INDENT ans = [ ] NEW_LINE ans . append ( 1 ) NEW_LINE for i in range ( 2 , math . floor ( math . sqrt ( A ) ) + 1 ) : NEW_LINE INDENT if ( A % i == 0 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE if ( ( i * i ) != A ) : NEW_LINE INDENT ans . append ( A // i ) NEW...
Maximize the division result of Array using given operations | Function to find the max result ; Sort the array in descending order ; Loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; Return the final result ; Driver code
def maxDivision ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE mxdiv = arr [ 1 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT mxdiv = mxdiv / arr [ i ] NEW_LINE DEDENT return arr [ 0 ] / mxdiv NEW_LINE DEDENT arr = [ 100 , 1000 , 10 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxDivisio...
Print all distinct Coprime sets possible from 1 to N | Function to prall co - prime sets ; Check if n is less than 4 then simply prall values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; If n is odd then every set will contain 2 adjacent element e...
def coPrimeSet ( n ) : NEW_LINE INDENT firstadj = 0 ; NEW_LINE secadj = 0 ; NEW_LINE if ( n < 4 ) : NEW_LINE INDENT print ( " ( ▁ " ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( i + " , ▁ " ) ; NEW_LINE DEDENT print ( " ) " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE IN...
Find the sequence number of a triangular number | Python3 code to print sequence number of a triangular number ; if N is not tringular number
import math NEW_LINE N = 21 NEW_LINE A = math . sqrt ( 2 * N + 0.25 ) - 0.5 NEW_LINE B = int ( A ) NEW_LINE if B != A : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT
Count subarrays with sum equal to its XOR value | Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will b...
def operation ( arr , N ) : NEW_LINE INDENT right = 0 ; ans = 0 ; NEW_LINE num = 0 ; NEW_LINE for left in range ( 0 , N ) : NEW_LINE INDENT while ( right < N and num + arr [ right ] == ( num ^ arr [ right ] ) ) : NEW_LINE INDENT num += arr [ right ] ; NEW_LINE right += 1 ; NEW_LINE DEDENT ans += right - left ; NEW_LINE...
Count of pairs in an Array whose sum is Prime | Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver code
def sieveOfEratosthenes ( N ) : NEW_LINE INDENT isPrime = [ True for i in range ( N + 1 ) ] NEW_LINE isPrime [ 0 ] = False NEW_LINE isPrime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while ( ( i * i ) <= N ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT j = 2 NEW_LINE while ( i * j <= N ) : NEW_LINE INDENT isPrim...
Count of numbers in Array ending with digits of number N | Array to keep the track of digits occurred Initially all are 0 ( false ) ; Function to initialize true if the digit is present ; Variable to store the last digit ; Loop to iterate through every digit of the number N ; Updating the array according to the presenc...
digit = [ 0 ] * 10 NEW_LINE def digitsPresent ( n ) : NEW_LINE INDENT lastDigit = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT lastDigit = n % 10 ; NEW_LINE digit [ int ( lastDigit ) ] = 1 ; NEW_LINE n /= 10 ; NEW_LINE DEDENT DEDENT def checkLastDigit ( num ) : NEW_LINE INDENT count = 0 ; NEW_LINE lastDigit = 0 ; NE...
Check if count of even divisors of N is equal to count of odd divisors | Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Given Number ; Function Call
def divisorsSame ( n ) : NEW_LINE INDENT return ( n - 2 ) % 4 == 0 ; NEW_LINE DEDENT N = 6 ; NEW_LINE if ( divisorsSame ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | Function to find integer ; Because 1 st integer is K itself ; Initialize min_d and max_d ; Updating min_d and max_d ; Break if min digit is 0 ; Driver code
def find ( K , N ) : NEW_LINE INDENT N = N - 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT curr_term = K NEW_LINE min_d = 9 NEW_LINE max_d = 0 NEW_LINE while curr_term > 0 : NEW_LINE INDENT r = int ( curr_term % 10 ) NEW_LINE min_d = min ( min_d , r ) NEW_LINE max_d = max ( max_d , r ) NEW_LINE curr_term = int ...
Count of subarrays whose sum is a perfect square | Python3 code for the above approach . ; Function to find count of subarrays whose sum is a perfect square . ; To search for index with ( current prefix sum - j * j ) ; Storing the prefix sum ; Used to track the minimum value in prefixSum ; Calculating the prefixSum and...
from collections import defaultdict NEW_LINE def countSubarrays ( arr , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE prefixSum = [ 0 ] * n NEW_LINE prefixMin = 0 NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE prefixMin = min ( prefixMin , prefixSum [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE I...
Find if two given Quadratic equations have common roots or not | Function to check if 2 quadratic equations have common roots or not . ; Driver code
def checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) : NEW_LINE INDENT return ( ( a1 / a2 ) == ( b1 / b2 ) and ( b1 / b2 ) == ( c1 / c2 ) ) NEW_LINE DEDENT a1 , b1 , c1 = 1 , - 5 , 6 NEW_LINE a2 , b2 , c2 = 2 , - 10 , 12 NEW_LINE if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_...
Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | Function to find the required answer ; Driver code
def checkSolution ( a , b , c ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT a = 2 NEW_LINE b = 0 NEW_LINE c = 2 NEW_LINE checkSolution ( a , b , c ) NEW_LINE
Count of index pairs in array whose range product is a positive integer | Python3 program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition...
def positiveProduct ( arr , n ) : NEW_LINE INDENT even_count = 0 NEW_LINE odd_count = 0 NEW_LINE total_count = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( total_count % 2 == 0 ) : NEW_LINE INDENT even_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd_count += 1 NEW_LINE DEDENT if ( arr [...
Pair of integers having difference of their fifth power as X | Python3 implementation to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Function to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Loop to choose every ...
import math NEW_LINE def findPair ( x ) : NEW_LINE INDENT lim = 120 NEW_LINE for i in range ( - lim , lim + 1 ) : NEW_LINE INDENT for j in range ( - lim , lim + 1 ) : NEW_LINE INDENT if ( math . pow ( i , 5 ) - math . pow ( j , 5 ) == x ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE print ( j , end = ' ' ) NE...
Sum of GCD of all numbers upto N with N itself | Function to Find Sum of GCD of each numbers ; Consider all prime factors of no . and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update no and result ; If no has a prime factor greater than Math . sqrt ( n ) then at - most one such...
def getCount ( d , n ) : NEW_LINE INDENT no = n // d ; NEW_LINE result = no ; NEW_LINE for p in range ( 2 , int ( pow ( no , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( no % p == 0 ) : NEW_LINE INDENT while ( no % p == 0 ) : NEW_LINE INDENT no //= p ; NEW_LINE DEDENT result -= result // p ; NEW_LINE DEDENT DEDENT if ( no > ...
Check whether Array represents a Fibonacci Series or not | Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver Code
def checkIsFibonacci ( arr , n ) : NEW_LINE INDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LI...
Last digit in a power of 2 | Python3 program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; Driver code
def lastDigit2PowerN ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n % 4 == 1 : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif n % 4 == 2 : NEW_LINE INDENT return 4 NEW_LINE DEDENT elif n % 4 == 3 : NEW_LINE INDENT return 8 NEW_LINE DEDENT else : NEW_LINE DEDENT for n in range ( 20 ) ...
Sum of all subsequences of length K | Function to find nCr ; Function that returns factorial of n ; Function for finding sum of all K length subsequences ; Calculate the sum of array ; Calculate nCk ; Return the final result ; Driver Code
def nCr ( n , r ) : NEW_LINE INDENT return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def sumSubsequences ( arr , n , k ) : NEW_LINE INDENT sum = 0 ...
Sum of first K numbers which are not divisible by N | Function to find the sum ; Find the last multiple of N ; Find the K - th non - multiple of N ; Calculate the sum of all elements from 1 to val ; Calculate the sum of all multiples of N between 1 to val ; Driver code
def findSum ( n , k ) : NEW_LINE INDENT val = ( k // ( n - 1 ) ) * n ; NEW_LINE rem = k % ( n - 1 ) ; NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT val = val - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT val = val + rem ; NEW_LINE DEDENT sum = ( val * ( val + 1 ) ) // 2 ; NEW_LINE x = k // ( n - 1 ) ; NEW_LINE sum_of_multi...
Count of the non | Python3 program to find count of non - prime divisors of given number ; Function to factors of the given number ; Loop to find the divisors of the number 2 ; Loop to find the divisors of the given number upto SQRT ( N ) ; Condition to check if the rest number is also a prime number ; Function to find...
from math import sqrt NEW_LINE def getFactorization ( x ) : NEW_LINE INDENT count = 0 NEW_LINE v = [ ] NEW_LINE while ( x % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE x = x // 2 NEW_LINE DEDENT if ( count != 0 ) : NEW_LINE INDENT v . append ( count ) NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( x ) ) + 12 ) : ...
Check if a number is Full Fibonacci or not | Python3 program to check if a given number is a Full Fibonacci Number or not ; A utility function that returns true if x is perfect square ; Returns true if N is a Fibonacci Number and false otherwise ; N is Fibonacci if one of 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4 or both is a per...
from math import * NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def checkDigits ( n ) : NEW_LINE INDENT while ( ...
Check whether a large number is divisible by 53 or not | Function to check if the number is divisible by 53 or not ; Driver Code
def isDivisible ( s ) : NEW_LINE INDENT flag = 0 NEW_LINE while ( len ( s ) > 4 ) : NEW_LINE INDENT l = len ( s ) - 1 NEW_LINE x = ( ord ( s [ l ] ) - ord ( '0' ) ) * 37 NEW_LINE s = s [ : : - 1 ] NEW_LINE s = s . replace ( '0' , ' ' , 1 ) NEW_LINE i = 0 NEW_LINE carry = 0 NEW_LINE while ( x ) : NEW_LINE INDENT d = ( (...
Check whether two numbers are in silver ratio | Function to check that two numbers are in silver ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in silver ratio ; Driver Code ; Function Call
def checksilverRatio ( a , b ) : NEW_LINE INDENT a , b = max ( a , b ) , min ( a , b ) NEW_LINE ratio1 = round ( a / b , 3 ) NEW_LINE ratio2 = round ( ( 2 * a + b ) / a , 3 ) NEW_LINE if ratio1 == ratio2 and ratio1 == 2.414 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT ...
Nth term where K + 1 th term is product of Kth term with difference of max and min digit of Kth term | Function to find minimum digit in the decimal representation of N ; Loop to find the minimum digit in the number ; Function to find maximum digit in the decimal representation of N ; Loop to find the maximum digit in ...
def MIN ( n ) : NEW_LINE INDENT ans = 11 NEW_LINE while n : NEW_LINE INDENT ans = min ( ans , n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def MAX ( n ) : NEW_LINE INDENT ans = - 1 NEW_LINE while n : NEW_LINE INDENT ans = max ( ans , n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT return ans NEW_LIN...
Program to check if N is a Decagonal Number | Python3 program for the above approach ; Function to check if N is a decagonal number ; Condition to check if the number is a decagonal number ; Driver Code ; Given number ; Function Call
import math NEW_LINE def isdecagonal ( N ) : NEW_LINE INDENT n = ( 3 + math . sqrt ( 16 * N + 9 ) ) / 8 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE if isdecagonal ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT...
Program to check if N is a Octadecagon number | Python3 program for the above approach ; Function to check if N is a octadecagon number ; Condition to check if the number is a octadecagon number ; Driver code ; Given number ; Function Call
import math NEW_LINE def isOctadecagon ( N ) : NEW_LINE INDENT n = ( 14 + math . sqrt ( 128 * N + 196 ) ) // 32 NEW_LINE return ( ( n - int ( n ) ) == 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 18 NEW_LINE if isOctadecagon ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : N...
Count of subsequences which consists exactly K prime numbers | Returns factorial of n ; Function to return total number of combinations ; Function check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for finding number of subsequences which consists exactly K primes ; If number of pri...
def fact ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) // ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW...
Length of the longest alternating even odd subarray | Function to find the longest subarray ; Length of longest alternating subarray ; Iterate in the array ; Increment count if consecutive elements has an odd sum ; Store maximum count in longest ; Reinitialize cnt as 1 consecutive elements does not have an odd sum ; Le...
def longestEvenOddSubarray ( arr , n ) : NEW_LINE INDENT longest = 1 NEW_LINE cnt = 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( ( arr [ i ] + arr [ i + 1 ] ) % 2 == 1 ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT else : NEW_LINE INDENT longest = max ( longest , cnt ) NEW_LINE cnt = 1 NEW_LINE DEDENT ...
Check whether given number N is a Moran Number or not | Function to calculate digit sum ; Function to check if number is prime ; Function to check if number is moran number ; Calculate digit sum ; Check if n is completely divisible by digit sum ; Calculate the quotient ; Check if the number is prime ; Driver code
def digSum ( a ) : NEW_LINE INDENT _sum = 0 NEW_LINE while ( a ) : NEW_LINE INDENT _sum += a % 10 NEW_LINE a = a // 10 NEW_LINE DEDENT return _sum NEW_LINE DEDENT def isPrime ( r ) : NEW_LINE INDENT s = True NEW_LINE i = 2 NEW_LINE while i * i <= r : NEW_LINE INDENT if ( r % i == 0 ) : NEW_LINE INDENT s = False NEW_LIN...
Program to check if N is a Hendecagonal Number | Python3 program for the above approach ; Function to check if N is a Hendecagonal Number ; Condition to check if the number is a hendecagonal number ; Given Number ; Function call
import math NEW_LINE def ishendecagonal ( N ) : NEW_LINE INDENT n = ( 7 + math . sqrt ( 72 * N + 49 ) ) // 18 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT N = 11 ; NEW_LINE if ( ishendecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE ...
Program to check if N is a Hexadecagonal Number | Python3 program for the above approach ; Function to check if N is a hexadecagonal number ; Condition to check if the number is a hexadecagonal number ; Driver code ; Given number ; Function call
from math import sqrt NEW_LINE def ishexadecagonal ( N ) : NEW_LINE INDENT n = ( 12 + sqrt ( 112 * N + 144 ) ) / 28 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 16 ; NEW_LINE if ( ishexadecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE D...
Program to check if N is a Nonagonal Number | Function to check if N is a nonagonal number ; Condition to check if the number is a nonagonal number ; Driver code ; Given number ; Function call
def isnonagonal ( N ) : NEW_LINE INDENT n = ( 5 + pow ( ( 56 * N + 25 ) , 1 / 2 ) ) / 14 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 ; NEW_LINE if ( isnonagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT pri...
Count of decreasing pairs formed from numbers 1 to N | Function to count the possible number of pairs ; if the number is even then the answer in ( N / 2 ) - 1 ; if the number is odd then the answer in N / 2 ; Driver code
def divParts ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT print ( ( N / 2 ) - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( N / 2 ) ; NEW_LINE DEDENT DEDENT N = 8 ; NEW_LINE divParts ( N ) ; NEW_LINE
Count the minimum steps to reach 0 from the given integer N | Function returns min step to reach 0 from N ; Direct possible reduction of value N ; Remaining steps needs to be reduced by 1 ; Summation of both the values ; Return the final answer ; Driver code
def getMinSteps ( n , jump ) : NEW_LINE INDENT quotient = int ( n / jump ) NEW_LINE remainder = n % jump NEW_LINE steps = quotient + remainder NEW_LINE return steps NEW_LINE DEDENT N = 6 NEW_LINE K = 3 NEW_LINE print ( getMinSteps ( N , K ) ) NEW_LINE
Construct an Array of size N whose sum of cube of all elements is a perfect square | Function to construct an array of size N ; Prints the first N natural numbers ; Driver code
def constructArray ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE constructArray ( N ) NEW_LINE DEDENT
Program to check if N is a Centered Decagonal Number | Python3 program for the above approach ; Function to check if the number N is a centered decagonal number ; Condition to check if N is centered decagonal number ; Driver Code ; Function call
import numpy as np NEW_LINE def isCentereddecagonal ( N ) : NEW_LINE INDENT n = ( 5 + np . sqrt ( 20 * N + 5 ) ) / 10 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 11 NEW_LINE if ( isCentereddecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LI...
Count Sexy Prime Pairs in the given array | Python 3 program to count Sexy Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n...
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) : N...
Find the K | Function to find the index of number at first position of kth sequence of set of size n ; n_actual_fact = n ! ; First position of the kth sequence will be occupied by the number present at index = k / ( n - 1 ) ! ; Function to find the kth permutation of n numbers ; Store final answer ; Insert all natural ...
def findFirstNumIndex ( k , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 , k NEW_LINE DEDENT n -= 1 NEW_LINE first_num_index = 0 NEW_LINE n_partial_fact = n NEW_LINE while ( k >= n_partial_fact and n > 1 ) : NEW_LINE INDENT n_partial_fact = n_partial_fact * ( n - 1 ) NEW_LINE n -= 1 NEW_LINE DEDENT fi...
Find the Largest N digit perfect square number in Base B | Python3 implementation to find the largest N digit perfect square number in base B ; Function to find the largest N digit number ; Largest n - digit perfect square ; Print the result ; Driver Code
import math NEW_LINE def nDigitPerfectSquares ( n , b ) : NEW_LINE INDENT largest = pow ( math . ceil ( math . sqrt ( pow ( b , n ) ) ) - 1 , 2 ) NEW_LINE print ( largest ) NEW_LINE DEDENT N = 1 NEW_LINE B = 8 NEW_LINE nDigitPerfectSquares ( N , B ) NEW_LINE
Find Cube root of a number using Log function | Python3 program to find cube root of a number using logarithm ; Function to find the cube root ; Calculate the cube root ; Return the final answer ; Driver code
import numpy as np NEW_LINE def cubeRoot ( n ) : NEW_LINE INDENT ans = pow ( 3 , ( 1.0 / 3 ) * ( np . log ( n ) / np . log ( 3 ) ) ) NEW_LINE return ans NEW_LINE DEDENT N = 8 NEW_LINE print ( " % .2f " % cubeRoot ( N ) ) NEW_LINE
Find the maximum possible value for the given periodic function | Function to return the maximum value of f ( x ) ; Driver code
def floorMax ( A , B , N ) : NEW_LINE INDENT x = min ( B - 1 , N ) NEW_LINE return ( A * x ) // B NEW_LINE DEDENT A = 11 NEW_LINE B = 10 NEW_LINE N = 9 NEW_LINE print ( floorMax ( A , B , N ) ) NEW_LINE
Minimum moves taken to move coin of each cell to any one cell of Matrix | Function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of laye...
def calculateMoves ( n ) : NEW_LINE INDENT count = 0 NEW_LINE layers = n // 2 NEW_LINE for k in range ( 1 , layers + 1 ) : NEW_LINE INDENT count += 8 * k * k NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( calculateMoves ( N ) ) NEW_LINE DEDENT
Check if N can be represented as sum of squares of two consecutive integers | Python3 implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of ...
import math NEW_LINE def isSumSquare ( N ) : NEW_LINE INDENT n = ( 2 + math . sqrt ( 8 * N - 4 ) ) / 2 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT i = 13 NEW_LINE if isSumSquare ( i ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT ...
Program to check if N is a Centered heptagonal number | Python3 implementation to check that a number is a centered heptagonal number or not ; Function to check that the number is a centered heptagonal number ; Condition to check if the number is a centered heptagonal number ; Driver Code ; Function call
import math NEW_LINE def isCenteredheptagonal ( N ) : NEW_LINE INDENT n = ( 7 + math . sqrt ( 56 * N - 7 ) ) / 14 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT n = 8 NEW_LINE if ( isCenteredheptagonal ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE D...
Program to check if N is a Centered nonadecagonal number | Python3 implementation to check that a number is a Centered nonadecagonal number or not ; Function to check that the number is a Centered nonadecagonal number ; Condition to check if the number is a Centered nonadecagonal number ; Driver Code ; Function call
import math NEW_LINE def isCenterednonadecagonal ( N ) : NEW_LINE INDENT n = ( 19 + math . sqrt ( 152 * N + 209 ) ) / 38 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT n = 20 ; NEW_LINE if ( isCenterednonadecagonal ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ...
Program to check if N is a Centered Octadecagonal number | Python3 implementation to check that a number is a centered octadecagonal number or not ; Function to check that the number is a centered octadecagonal number ; Implement the formula generated ; Condition to check if the number is a centered octadecagonal numbe...
import math NEW_LINE def isCenteredOctadecagonal ( N ) : NEW_LINE INDENT n = ( 9 + math . sqrt ( 36 * N + 45 ) ) / 18 ; NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 19 NEW_LINE if isCenteredOctadecagonal ( n ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE D...
Logarithm tricks for Competitive Programming | Python3 implementation to find Kth root of the number ; Function to find the Kth root of the number ; Driver code
import math NEW_LINE def kth_root ( n , k ) : NEW_LINE INDENT return ( pow ( k , ( ( 1.0 / k ) * ( math . log ( n ) / math . log ( k ) ) ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8.0 NEW_LINE k = 3 NEW_LINE print ( round ( kth_root ( n , k ) ) ) NEW_LINE DEDENT
Logarithm tricks for Competitive Programming | Python3 implementation to check if a number is a power of the other number ; Function to check if the number is power of K ; Logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Driver code
from math import log NEW_LINE def isPower ( n , k ) : NEW_LINE INDENT res1 = int ( log ( n ) / log ( k ) ) NEW_LINE res2 = log ( n ) / log ( k ) NEW_LINE return ( res1 == res2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE k = 2 NEW_LINE if ( isPower ( n , k ) ) : NEW_LINE INDENT pri...
Count of subarrays which start and end with the same element | Function to find total sub - array which start and end with same element ; Initialize result with 0 ; Array to count frequency of 1 to N ; Update frequency of A [ i ] ; Update result with sub - array contributed by number i ; Print the result ; Driver code
def cntArray ( A , N ) : NEW_LINE INDENT result = 0 NEW_LINE frequency = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT frequency [ A [ i ] ] = frequency [ A [ i ] ] + 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT frequency_of_i = frequency [ i ] NEW_LINE result = result + ( ( ...
Length of array pair formed where one contains all distinct elements and other all same elements | Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
def findMaxSize ( a , n ) : NEW_LINE INDENT frq = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT frq [ a [ i ] ] += 1 NEW_LINE DEDENT maxfrq = max ( frq ) NEW_LINE dist = n + 1 - frq . count ( 0 ) NEW_LINE ans1 = min ( maxfrq - 1 , dist ) NEW_LINE ans2 = min ( maxfrq , dist - 1 ) NEW_LINE ans = max (...
Longest subarray with all elements same | Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
def subarray ( arr , n ) : NEW_LINE INDENT ans , temp = 1 , 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == arr [ i - 1 ] : NEW_LINE INDENT temp = temp + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( ans , temp ) NEW_LINE temp = 1 NEW_LINE DEDENT DEDENT ans = max ( ans , temp ) NEW_LINE ret...
Min and max length subarray having adjacent element difference atmost K | Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in...
def findMaxMinSubArray ( arr , K , n ) : NEW_LINE INDENT min = n NEW_LINE max = 0 NEW_LINE left = 0 NEW_LINE right = n NEW_LINE tmp = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tmp = 1 NEW_LINE left = i NEW_LINE while ( left - 1 >= 0 and abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) : NEW_LINE INDENT left =...
Count of elements which is the sum of a subarray of the given Array | Function to count element such that their exist a subarray whose sum is equal to this element ; Loop to compute frequency of the given elements ; Loop to iterate over every possible subarray of array ; Driver Code
def countElement ( arr , n ) : NEW_LINE INDENT freq = { } NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] = freq . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT tmpsum = arr [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT tmpsum +=...
Nth root of a number using log | Python3 implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code
import numpy as np NEW_LINE def kthRoot ( n , k ) : NEW_LINE INDENT return pow ( k , ( ( 1.0 / k ) * ( np . log ( n ) / np . log ( k ) ) ) ) NEW_LINE DEDENT n = 81 NEW_LINE k = 4 NEW_LINE print ( " % .6f " % kthRoot ( n , k ) ) NEW_LINE
Make A , B and C equal by adding total value N to them | Python3 Program to Distribute integer N among A , B , C such that they become equal ; find maximum among the three elements ; summation of three elements ; check if difference is divisible by 3 ; Driver code
def distributeN ( A , B , C , n ) : NEW_LINE INDENT maximum = max ( A , B , C ) NEW_LINE sum = A + B + C NEW_LINE p = ( 3 * maximum ) - sum NEW_LINE diff = n - p NEW_LINE if diff < 0 or diff % 3 : NEW_LINE INDENT print " No " NEW_LINE DEDENT else : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT DEDENT A = 10 NEW_LINE B ...
Represent N as sum of K odd numbers with repetitions allowed | Function to print the representation ; N must be greater than equal to 2 * K and must be odd ; Driver Code
def sumOddNumbers ( N , K ) : NEW_LINE INDENT check = N - ( K - 1 ) NEW_LINE if ( check > 0 and check % 2 == 1 ) : NEW_LINE INDENT for i in range ( 0 , K - 1 ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE DEDENT print ( check , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DED...
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
def minimumSteps ( a , b , c ) : NEW_LINE INDENT if a < 1 or b < 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if a == 1 and b == 1 : NEW_LINE INDENT return c NEW_LINE DEDENT if a < b : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT return minimumSteps ( a - b , b , c + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _...
Longest Mountain Subarray | Function to find the longest mountain subarray ; If the size of the array is less than 3 , the array won 't show mountain like behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub -...
def LongestMountain ( a ) : NEW_LINE INDENT i = 0 NEW_LINE j = - 1 NEW_LINE k = - 1 NEW_LINE p = 0 NEW_LINE d = 0 NEW_LINE n = 0 NEW_LINE if ( len ( a ) < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( len ( a ) - 1 ) : NEW_LINE INDENT if ( a [ i + 1 ] > a [ i ] ) : NEW_LINE INDENT if ( k != - 1 ) : NE...
Check whether an Array can be made 0 by splitting and merging repeatedly | Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
def solve ( A ) : NEW_LINE INDENT ctr = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] % 2 == 1 : NEW_LINE INDENT ctr += 1 NEW_LINE DEDENT DEDENT if ctr % 2 == 1 : NEW_LINE INDENT return ' No ' NEW_LINE DEDENT else : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _...
Count of perfect squares of given length | Python3 Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse ...
import math ; NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) ; NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) ; NEW_LINE DEDENT def countSquares ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( int ( math . pow ( 10 , ( n - 1 ) ) ) , int ( math . pow ( 10 , n ) ) ) : NEW_L...
Count of perfect squares of given length | Python3 program to count perfect squares of given length ; Function to return the count of n digit perfect squares ; Driver code
import math NEW_LINE def countSquares ( n ) : NEW_LINE INDENT r = math . ceil ( math . sqrt ( math . pow ( 10 , n ) ) ) ; NEW_LINE l = math . ceil ( math . sqrt ( math . pow ( 10 , n - 1 ) ) ) ; NEW_LINE return r - l ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countSquares ( n ) ) ; NEW_LINE