text
stringlengths
1
636
code
stringlengths
8
1.89k
Function to append a node to the BST
def AddNode ( root , data ) : NEW_LINE
If the tree is empty , return a new node
if ( root == None ) : NEW_LINE INDENT root = Node ( data ) NEW_LINE return root NEW_LINE DEDENT
Otherwise , recur down the tree
if ( root . data < data ) : NEW_LINE INDENT root . right = AddNode ( root . right , data ) NEW_LINE DEDENT elif ( root . data > data ) : NEW_LINE INDENT root . left = AddNode ( root . left , data ) NEW_LINE DEDENT return root NEW_LINE
Function to find the target pairs
def TargetPair ( node , tar ) : NEW_LINE
LeftList which stores the left side values
LeftList = [ ] NEW_LINE
RightList which stores the right side values
RightList = [ ] NEW_LINE
curr_left pointer is used for left side execution and curr_right pointer is used for right side execution
curr_left = node NEW_LINE curr_right = node NEW_LINE while ( curr_left != None or curr_right != None or len ( LeftList ) > 0 and len ( RightList ) > 0 ) : NEW_LINE
Storing the left side values into LeftList till leaf node not found
while ( curr_left != None ) : NEW_LINE INDENT LeftList . append ( curr_left ) NEW_LINE curr_left = curr_left . left NEW_LINE DEDENT
Storing the right side values into RightList till leaf node not found
while ( curr_right != None ) : NEW_LINE INDENT RightList . append ( curr_right ) NEW_LINE curr_right = curr_right . right NEW_LINE DEDENT
Last node of LeftList
LeftNode = LeftList [ - 1 ] NEW_LINE
Last node of RightList
RightNode = RightList [ - 1 ] NEW_LINE leftVal = LeftNode . data NEW_LINE rightVal = RightNode . data NEW_LINE
To prevent repetition like 2 , 6 and 6 , 2
if ( leftVal >= rightVal ) : NEW_LINE INDENT break NEW_LINE DEDENT
Delete the last value of LeftList and make the execution to the right side
if ( leftVal + rightVal < tar ) : NEW_LINE INDENT del LeftList [ - 1 ] NEW_LINE curr_left = LeftNode . right NEW_LINE DEDENT
Delete the last value of RightList and make the execution to the left side
elif ( leftVal + rightVal > tar ) : NEW_LINE INDENT del RightList [ - 1 ] NEW_LINE curr_right = RightNode . left NEW_LINE DEDENT
( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side
else : NEW_LINE INDENT print ( LeftNode . data , RightNode . data ) NEW_LINE del RightList [ - 1 ] NEW_LINE del LeftList [ - 1 ] NEW_LINE curr_left = LeftNode . right NEW_LINE curr_right = RightNode . left NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = AddNode ( root , 2 ) NEW_LINE root = AddNode ( root , 6 ) NEW_LINE root = AddNode ( root , 5 ) NEW_LINE root = AddNode ( root , 3 ) NEW_LINE root = AddNode ( root , 4 ) NEW_LINE root = AddNode ( root , 1 ) NEW_LINE root = AddNode ( root , 7 ) NEW_LINE sum = 8 NEW_LINE TargetPair ( root , sum ) NEW_LINE DEDENT
Function that returns the minimum number greater than Maximum of the array that cannot be formed using the elements of the array
def findNumber ( arr , n ) : NEW_LINE
Sort the given array
arr = sorted ( arr ) NEW_LINE
Maximum number in the array
Max = arr [ n - 1 ] NEW_LINE
table [ i ] will store the minimum number of elements from the array to form i
table = [ 10 ** 9 for i in range ( ( 2 * Max ) + 1 ) ] NEW_LINE table [ 0 ] = 0 NEW_LINE ans = - 1 NEW_LINE
Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * Max )
for i in range ( 1 , 2 * Max + 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] <= i ) : NEW_LINE INDENT res = table [ i - arr [ j ] ] NEW_LINE if ( res != 10 ** 9 and res + 1 < table [ i ] ) : NEW_LINE INDENT table [ i ] = res + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT
If there exists a number greater than the Maximum element of the array that can be formed using the numbers of array
if ( i > arr [ n - 1 ] and table [ i ] == 10 ** 9 ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT return ans NEW_LINE
Driver code
arr = [ 6 , 7 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findNumber ( arr , n ) ) NEW_LINE
Python 3 program to find product of all Subsequences of size K except the minimum and maximum Elements
MOD = 1000000007 NEW_LINE max = 101 NEW_LINE
2D array to store value of combinations nCr
C = [ [ 0 for i in range ( max ) ] for j in range ( max ) ] NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % MOD NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % MOD NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % MOD NEW_LINE DEDENT return res % MOD NEW_LINE DEDENT
Function to pre - calculate value of all combinations nCr
def combi ( n , k ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD NEW_LINE DEDENT DEDENT DEDENT DEDENT
Function to calculate product of all subsequences except the minimum and maximum elements
def product ( a , n , k ) : NEW_LINE INDENT ans = 1 NEW_LINE DEDENT
Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place
a . sort ( reverse = False ) NEW_LINE
An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element
powa = C [ n - 1 ] [ k - 1 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT powla = C [ i ] [ k - 1 ] NEW_LINE powfa = C [ n - i - 1 ] [ k - 1 ] NEW_LINE DEDENT
In total it will come powe = powa - powla - powfa times
powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD NEW_LINE
Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation
mul = power ( a [ i ] , powe ) % MOD NEW_LINE ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD NEW_LINE return ans % MOD NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
pre - calculation of all combinations
combi ( 100 , 100 ) NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE ans = product ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE
this function takes two unequal sized bit strings , converts them to same length by adding leading 0 s in the smaller string . Returns the the new length
def makeEqualLength ( a , b ) : NEW_LINE INDENT len_a = len ( a ) NEW_LINE len_b = len ( b ) NEW_LINE num_zeros = abs ( len_a - len_b ) NEW_LINE if ( len_a < len_b ) : NEW_LINE INDENT for i in range ( num_zeros ) : NEW_LINE INDENT a = '0' + a NEW_LINE DEDENT DEDENT DEDENT
Return len_b which is highest . No need to proceed further !
return len_b , a , b NEW_LINE else : NEW_LINE for i in range ( num_zeros ) : NEW_LINE INDENT b = '0' + b NEW_LINE DEDENT
Return len_a which is greater or equal to len_b
return len_a , a , b NEW_LINE
The main function that performs AND operation of two - bit sequences and returns the resultant string
def andOperationBitwise ( s1 , s2 ) : NEW_LINE
Make both strings of same length with the maximum length of s1 & s2 .
length , s1 , s2 = makeEqualLength ( s1 , s2 ) NEW_LINE
Initialize res as NULL string
res = " " NEW_LINE
We start from left to right as we have made both strings of same length .
for i in range ( length ) : NEW_LINE
Convert s1 [ i ] and s2 [ i ] to int and perform bitwise AND operation , append to " res " string
res = res + str ( int ( s1 [ i ] ) & int ( s2 [ i ] ) ) NEW_LINE return res NEW_LINE
Driver Code
arr = [ "101" , "110110" , "111" ] NEW_LINE n = len ( arr ) NEW_LINE
Check corner case : If there is just one binary string , then print it and return .
if ( n < 2 ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT result = andOperationBitwise ( result , arr [ i ] ) ; NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT
Function to return the maximum number of segments
def countPoints ( n , m , a , b , x , y ) : NEW_LINE
Sort both the vectors
a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE
Initially pointing to the first element of b [ ]
j , count = 0 , 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE
Try to find a match in b [ ]
while j < m : NEW_LINE
The segment ends before b [ j ]
if a [ i ] + y < b [ j ] : NEW_LINE INDENT break NEW_LINE DEDENT
The point lies within the segment
if ( b [ j ] >= a [ i ] - x and b [ j ] <= a [ i ] + y ) : NEW_LINE INDENT count += 1 NEW_LINE j += 1 NEW_LINE break NEW_LINE DEDENT
The segment starts after b [ j ]
else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT
Return the required count
return count NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y = 1 , 4 NEW_LINE a = [ 1 , 5 ] NEW_LINE n = len ( a ) NEW_LINE b = [ 1 , 1 , 2 ] NEW_LINE m = len ( b ) NEW_LINE print ( countPoints ( n , m , a , b , x , y ) ) NEW_LINE DEDENT
Python3 program to merge K sorted arrays
N = 4 NEW_LINE
Merge and sort k arrays
def merge_and_sort ( output , arr , n , k ) : NEW_LINE
Put the elements in sorted array .
for i in range ( k ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT output [ i * n + j ] = arr [ i ] [ j ] ; NEW_LINE DEDENT DEDENT
Sort the output array
output . sort ( ) NEW_LINE
Driver Function
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Input 2D - array
arr = [ [ 5 , 7 , 15 , 18 ] , [ 1 , 8 , 9 , 17 ] , [ 1 , 4 , 7 , 7 ] ] ; NEW_LINE n = N ; NEW_LINE
Number of arrays
k = len ( arr ) NEW_LINE
Output array
output = [ 0 for i in range ( n * k ) ] NEW_LINE merge_and_sort ( output , arr , n , k ) ; NEW_LINE
Print merged array
for i in range ( n * k ) : NEW_LINE INDENT print ( output [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT
Function to return the minimum number of operations required
def minOperations ( n , m , k , matrix ) : NEW_LINE
Create another array to store the elements of matrix
arr = [ ] NEW_LINE
will not work for negative elements , so . . adding this
if ( matrix [ 0 ] [ 0 ] < 0 ) : NEW_LINE INDENT mod = k - ( abs ( matrix [ 0 ] [ 0 ] ) % k ) NEW_LINE DEDENT else : NEW_LINE INDENT mod = matrix [ 0 ] [ 0 ] % k NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT arr . append ( matrix [ i ] [ j ] ) NEW_LINE DEDENT DEDENT
adding this to handle negative elements too .
val = matrix [ i ] [ j ] NEW_LINE if ( val < 0 ) : NEW_LINE INDENT res = k - ( abs ( val ) % k ) NEW_LINE if ( res != mod ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT foo = matrix [ i ] [ j ] NEW_LINE if ( foo % k != mod ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT
Sort the array to get median
arr . sort ( ) NEW_LINE median = arr [ ( n * m ) // 2 ] NEW_LINE
To count the minimum operations
minOperations = 0 NEW_LINE for i in range ( n * m ) : NEW_LINE INDENT minOperations += abs ( arr [ i ] - median ) // k NEW_LINE DEDENT
If there are even elements , then there are two medians . We consider the best of two as answer .
if ( ( n * m ) % 2 == 0 ) : NEW_LINE
changed here as in case of even elements there will be 2 medians
median2 = arr [ ( ( n * m ) // 2 ) - 1 ] NEW_LINE minOperations2 = 0 NEW_LINE for i in range ( n * m ) : NEW_LINE INDENT minOperations2 += abs ( arr [ i ] - median2 ) / k NEW_LINE DEDENT minOperations = min ( minOperations , minOperations2 ) NEW_LINE
Return minimum operations required
return minOperations NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT matrix = [ [ 2 , 4 , 6 ] , [ 8 , 10 , 12 ] , [ 14 , 16 , 18 ] , [ 20 , 22 , 24 ] ] NEW_LINE n = len ( matrix ) NEW_LINE m = len ( matrix [ 0 ] ) NEW_LINE k = 2 NEW_LINE print ( minOperations ( n , m , k , matrix ) ) NEW_LINE DEDENT
Python3 implementation of above approach
import sys NEW_LINE
Function to return length of smallest subarray containing both maximum and minimum value
def minSubarray ( A , n ) : NEW_LINE
find maximum and minimum values in the array
minValue = min ( A ) NEW_LINE maxValue = max ( A ) NEW_LINE pos_min , pos_max , ans = - 1 , - 1 , sys . maxsize NEW_LINE
iterate over the array and set answer to smallest difference between position of maximum and position of minimum value
for i in range ( 0 , n ) : NEW_LINE
last occurrence of minValue
if A [ i ] == minValue : NEW_LINE INDENT pos_min = i NEW_LINE DEDENT
last occurrence of maxValue
if A [ i ] == maxValue : NEW_LINE INDENT pos_max = i NEW_LINE DEDENT if pos_max != - 1 and pos_min != - 1 : NEW_LINE INDENT ans = min ( ans , abs ( pos_min - pos_max ) + 1 ) NEW_LINE DEDENT return ans NEW_LINE
Driver code
A = [ 1 , 5 , 9 , 7 , 1 , 9 , 4 ] NEW_LINE n = len ( A ) NEW_LINE print ( minSubarray ( A , n ) ) NEW_LINE
Python3 program find the minimum number of consecutive sequences in an array
def countSequences ( arr , n ) : NEW_LINE INDENT count = 1 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] + 1 != arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT
Driver program
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 7 , 3 , 5 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE DEDENT
function call to print required answer
print ( countSequences ( arr , n ) ) NEW_LINE
Function to find the minimum operations
def minimumMoves ( a , n ) : NEW_LINE INDENT operations = 0 NEW_LINE DEDENT
Sort the given array
a . sort ( reverse = False ) NEW_LINE
Count operations by assigning a [ i ] = i + 1
for i in range ( 0 , n , 1 ) : NEW_LINE INDENT operations = operations + abs ( a [ i ] - ( i + 1 ) ) NEW_LINE DEDENT return operations NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumMoves ( arr , n ) ) NEW_LINE DEDENT
Function to print a case where the given sorting algorithm fails
def printCase ( n ) : NEW_LINE
only case where it fails
if ( n <= 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE printCase ( n ) NEW_LINE DEDENT
Function to find missing elements from given Ranges
def findMissingNumber ( ranges , m ) : NEW_LINE
First of all sort all the given ranges
ranges . sort ( ) NEW_LINE
store ans in a different vector
ans = [ ] NEW_LINE
prev is use to store end of last range
prev = 0 NEW_LINE
j is used as a counter for ranges
for j in range ( len ( ranges ) ) : NEW_LINE INDENT start = ranges [ j ] [ 0 ] NEW_LINE end = ranges [ j ] [ 1 ] NEW_LINE for i in range ( prev + 1 , start ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE DEDENT prev = end NEW_LINE DEDENT
for last segment
for i in range ( prev + 1 , m + 1 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE DEDENT
finally print all answer
for i in range ( len ( ans ) ) : NEW_LINE INDENT if ans [ i ] <= m : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M = 2 , 6 NEW_LINE DEDENT
Store ranges in vector of pair
ranges = [ ] NEW_LINE ranges . append ( [ 1 , 2 ] ) NEW_LINE ranges . append ( [ 4 , 5 ] ) NEW_LINE findMissingNumber ( ranges , M ) NEW_LINE
Function to check if both sequences can be made equal
def check ( n , k , a , b ) : NEW_LINE
Sorting both the arrays
a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE
Flag to tell if there are more than one mismatch
fl = False NEW_LINE