text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Recursive function to print from N to 1 | def PrintReverseOrder ( N ) : NEW_LINE INDENT for i in range ( N , 0 , - 1 ) : NEW_LINE INDENT print ( i , end = " β " ) ; NEW_LINE DEDENT DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE PrintReverseOrder ( N ) ; NEW_LINE DEDENT |
Function to find the values | def findAns ( a , b , n ) : NEW_LINE |
Calculate the LCM | lcm = ( a * b ) // __gcd ( a , b ) ; NEW_LINE |
Calculate the multiples of lcm | multiples = ( n // lcm ) + 1 ; NEW_LINE |
Find the values which satisfies the given condition | answer = max ( a , b ) * multiples ; NEW_LINE |
Subtract the extra values | lastvalue = lcm * ( n // lcm ) + max ( a , b ) ; NEW_LINE if ( lastvalue > n ) : NEW_LINE INDENT answer = answer - ( lastvalue - n - 1 ) ; NEW_LINE DEDENT |
Return the final result | return answer ; NEW_LINE def __gcd ( a , b ) : NEW_LINE if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 1 ; NEW_LINE b = 13 ; NEW_LINE n = 500 ; NEW_LINE print ( findAns ( a , b , n ) ) ; NEW_LINE DEDENT |
Python3 program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers | from math import sqrt NEW_LINE |
Function to find the arithmetic mean of 2 numbers | def ArithmeticMean ( A , B ) : NEW_LINE INDENT return ( A + B ) / 2 NEW_LINE DEDENT |
Function to find the harmonic mean of 2 numbers | def HarmonicMean ( A , B ) : NEW_LINE INDENT return ( 2 * A * B ) / ( A + B ) NEW_LINE DEDENT |
Following function checks and computes the desired results based on the means | def CheckArithmeticHarmonic ( arr , A , B , N ) : NEW_LINE |
Calculate means | AM = ArithmeticMean ( A , B ) NEW_LINE HM = HarmonicMean ( A , B ) NEW_LINE |
Hash container ( set ) to store elements | Hash = set ( ) NEW_LINE |
Insertion of array elements in the set | for i in range ( N ) : NEW_LINE INDENT Hash . add ( arr [ i ] ) NEW_LINE DEDENT |
Conditionals to check if numbers are present in array by Hashing | if ( A in Hash and B in Hash ) : NEW_LINE |
Conditionals to check if the AM and HM of the numbers are present in array | if ( AM in Hash and HM in Hash ) : NEW_LINE |
If all conditions are satisfied , the Geometric Mean is calculated | print ( " GM β = " , round ( sqrt ( AM * HM ) , 2 ) ) NEW_LINE else : NEW_LINE |
If numbers are found but the respective AM and HM are not found in the array | print ( " AM β and β HM β not β found " ) NEW_LINE else : NEW_LINE |
If none of the conditions are satisfied | print ( " Numbers β not β found " ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 ] NEW_LINE N = len ( arr ) NEW_LINE A = 3.0 NEW_LINE B = 6.0 NEW_LINE CheckArithmeticHarmonic ( arr , A , B , N ) NEW_LINE DEDENT |
Function that print number of moves required | def movesRequired ( a , b ) : NEW_LINE |
Calculate modulo | total_moves = a % b NEW_LINE |
Print the required answer | print ( total_moves ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Initialise A and B | A = 10 NEW_LINE B = 3 NEW_LINE movesRequired ( A , B ) NEW_LINE |
Function to calculate the Pythagorean triplet in O ( n ) | def PythagoreanTriplet ( n ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT |
Iterate a from 1 to N - 1. | for a in range ( 1 , n , 1 ) : NEW_LINE |
Calculate value of b | b = ( n * n - 2 * n * a ) // ( 2 * n - 2 * a ) NEW_LINE |
The value of c = n - a - b | c = n - a - b NEW_LINE if ( a * a + b * b == c * c and b > 0 and c > 0 ) : NEW_LINE INDENT print ( a , b , c ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE print ( " - 1" ) NEW_LINE return NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE DEDENT |
Function call | PythagoreanTriplet ( N ) NEW_LINE |
Python3 program to check if there exists a number with X factors out of which exactly K are prime | from math import sqrt NEW_LINE |
Function to check if such number exists | def check ( X , K ) : NEW_LINE |
To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X | prime = 0 NEW_LINE temp = X NEW_LINE sqr = int ( sqrt ( X ) ) NEW_LINE |
Determining the prime factors of X | for i in range ( 2 , sqr + 1 , 1 ) : NEW_LINE INDENT while ( temp % i == 0 ) : NEW_LINE INDENT temp = temp // i NEW_LINE prime += 1 NEW_LINE DEDENT DEDENT |
To check if the number is prime | if ( temp > 2 ) : NEW_LINE INDENT prime += 1 NEW_LINE DEDENT |
If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) | if ( X == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT |
If X itself is prime then it can be represented as a power of only 1 prime factor w0hich is X itself so we return true | if ( prime == 1 and K == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT |
If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true | elif ( prime >= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT |
In any other case , we return false as we cannot form a number with X factors and K prime factors | else : NEW_LINE INDENT return False NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 4 NEW_LINE K = 2 NEW_LINE if ( check ( X , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT |
A Tree node | class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT |
Utility function to create a new node | def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE return temp NEW_LINE DEDENT N = 1000000 NEW_LINE |
Vector to store all the prime numbers | prime = [ ] NEW_LINE |
Function to store all the prime numbers in an array | def SieveOfEratosthenes ( ) : NEW_LINE |
Create a boolean array " prime [ 0 . . N ] " and initialize all the entries in it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . | check = [ True for i in range ( N + 1 ) ] NEW_LINE p = 2 NEW_LINE while ( p * p <= N ) : NEW_LINE |
If prime [ p ] is not changed , then it is a prime | if ( check [ p ] ) : NEW_LINE INDENT prime . append ( p ) NEW_LINE DEDENT |
Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . | for i in range ( p * p , N + 1 , p ) : NEW_LINE INDENT check [ i ] = False NEW_LINE DEDENT p += 1 NEW_LINE |
Function to check whether Path is Co - prime or not | def isPathCo_Prime ( path ) : NEW_LINE INDENT max = 0 NEW_LINE DEDENT |
Iterating through the array to find the maximum element in the array | for x in path : NEW_LINE INDENT if ( max < x ) : NEW_LINE INDENT max = x NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while ( i * prime [ i ] <= max // 2 ) : NEW_LINE INDENT ct = 0 NEW_LINE DEDENT |
Incrementing the variable if any of the value has a factor | for x in path : NEW_LINE INDENT if ( x % prime [ i ] == 0 ) : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT |
If not co - prime | if ( ct > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE return True NEW_LINE |
Function to print a Co - Prime path | def printCo_PrimePaths ( path ) : NEW_LINE INDENT for x in path : NEW_LINE INDENT print ( x , end = ' β ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT |
Function to find co - prime paths of binary tree | def findCo_PrimePaths ( root , path ) : NEW_LINE |
Base case | if ( root == None ) : NEW_LINE INDENT return path NEW_LINE DEDENT |
Store the value in path vector | path . append ( root . key ) NEW_LINE |
Recursively call for left sub tree | path = findCo_PrimePaths ( root . left , path ) NEW_LINE |
Recursively call for right sub tree | path = findCo_PrimePaths ( root . right , path ) NEW_LINE |
Condition to check , if leaf node | if ( root . left == None and root . right == None ) : NEW_LINE |
Condition to check , if path co - prime or not | if ( isPathCo_Prime ( path ) ) : NEW_LINE |
Print co - prime path | printCo_PrimePaths ( path ) NEW_LINE |
Remove the last element from the path vector | path . pop ( ) NEW_LINE return path NEW_LINE |
Function to find Co - Prime paths In a given binary tree | def printCo_PrimePath ( node ) : NEW_LINE |
To save all prime numbers | SieveOfEratosthenes ( ) NEW_LINE path = [ ] NEW_LINE |
Function call | path = findCo_PrimePaths ( node , path ) NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Create Binary Tree as shown | root = newNode ( 10 ) NEW_LINE root . left = newNode ( 48 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 11 ) NEW_LINE root . right . right = newNode ( 37 ) NEW_LINE root . right . left . left = newNode ( 7 ) NEW_LINE root . right . left . right = newNode ( 29 ) NEW_LINE root . right . right . left = newNode ( 42 ) NEW_LINE root . right . right . right = newNode ( 19 ) NEW_LINE root . right . right . right . left = newNode ( 7 ) NEW_LINE |
Print Co - Prime Paths | printCo_PrimePath ( root ) NEW_LINE |
Python3 implementation to find the number of subsets with equal bitwise AND , OR and XOR values | mod = 1000000007 ; NEW_LINE |
Function to find the number of subsets with equal bitwise AND , OR and XOR values | def countSubsets ( a , n ) : NEW_LINE INDENT answer = 0 ; NEW_LINE DEDENT |
Traverse through all the subsets | for i in range ( 1 << n ) : NEW_LINE INDENT bitwiseAND = - 1 ; NEW_LINE bitwiseOR = 0 ; NEW_LINE bitwiseXOR = 0 ; NEW_LINE DEDENT |
Finding the subsets with the bits of ' i ' which are set | for j in range ( n ) : NEW_LINE |
Computing the bitwise AND | if ( i & ( 1 << j ) ) : NEW_LINE INDENT if ( bitwiseAND == - 1 ) : NEW_LINE INDENT bitwiseAND = a [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT bitwiseAND &= a [ j ] ; NEW_LINE DEDENT DEDENT |
Computing the bitwise OR | bitwiseOR |= a [ j ] ; NEW_LINE |
Computing the bitwise XOR | bitwiseXOR ^= a [ j ] ; NEW_LINE |
Comparing all the three values | if ( bitwiseAND == bitwiseOR and bitwiseOR == bitwiseXOR ) : NEW_LINE INDENT answer = ( answer + 1 ) % mod ; NEW_LINE DEDENT return answer ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 ; NEW_LINE A = [ 1 , 3 , 2 , 1 , 2 , 1 ] ; NEW_LINE print ( countSubsets ( A , N ) ) ; NEW_LINE DEDENT |
Function to find the number of subsets formed by the given value K | def count ( arr , N , K ) : NEW_LINE |
Count is used to maintain the number of continuous K 's | count = 0 NEW_LINE ans = 0 NEW_LINE |
Iterating through the array | for i in range ( N ) : NEW_LINE |
If the element in the array is equal to K | if ( arr [ i ] == K ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE |
count * ( count + 1 ) / 2 is the total number of subsets with only K as their element | ans += ( count * ( count + 1 ) ) // 2 NEW_LINE |
Change count to 0 because other element apart from K has been found | count = 0 NEW_LINE |
To handle the last set of K 's | ans = ans + ( count * ( count + 1 ) ) // 2 NEW_LINE return ans NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 0 , 1 , 1 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE K = 0 NEW_LINE print ( count ( arr , N , K ) ) NEW_LINE DEDENT |
Function to convert a decimal number to a ternary number | def convertToTernary ( N ) : NEW_LINE |
Base case | if ( N == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT |
Finding the remainder when N is divided by 3 | x = N % 3 ; NEW_LINE N //= 3 ; NEW_LINE if ( x < 0 ) : NEW_LINE INDENT N += 1 ; NEW_LINE DEDENT |
Recursive function to call the function for the integer division of the value N / 3 | convertToTernary ( N ) ; NEW_LINE |
Handling the negative cases | if ( x < 0 ) : NEW_LINE INDENT print ( x + ( 3 * - 1 ) , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( x , end = " " ) ; NEW_LINE DEDENT |
Function to convert the decimal to ternary | def convert ( Decimal ) : NEW_LINE INDENT print ( " Ternary β number β of β " , Decimal , " β is : β " , end = " " ) ; NEW_LINE DEDENT |
If the number is greater than 0 , compute the ternary representation of the number | if ( Decimal != 0 ) : NEW_LINE INDENT convertToTernary ( Decimal ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Decimal = 2747 ; NEW_LINE convert ( Decimal ) ; NEW_LINE DEDENT |
Function to get the number | def get ( x , y , z ) : NEW_LINE |
remainder can ' t β be β larger β β than β the β largest β number , β β if β so β then β answer β doesn ' t exist . | if ( x > z ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
reduce number by x | val = z - x NEW_LINE |
finding the possible number that is divisible by y | div = ( z - x ) // y NEW_LINE |
this number is always <= x as we calculated over z - x | ans = div * y + x NEW_LINE return ans NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.