codeparrot/xlcost-text-to-code huggingface dataset:
Queries for elements greater than K in the given index range using Segment Tree | Python implementation of the approach ; combine function to make parent node ; building the tree ; leaf node ; merging the nodes while backtracking ; performing query ; out of bounds ; complete overlaps ; partial overlaps ; Driver Code ; 1 - based indexing
import math NEW_LINE def combine ( a , b ) : NEW_LINE INDENT if a != 0 and b != 0 : NEW_LINE INDENT return a NEW_LINE DEDENT if a >= b : NEW_LINE INDENT return a NEW_LINE DEDENT return b NEW_LINE DEDENT def build_tree ( arr , tree , ind , low , high , x ) : NEW_LINE INDENT if low == high : NEW_LINE INDENT if arr [ low ] > x : NEW_LINE INDENT tree [ ind ] = arr [ low ] NEW_LINE DEDENT else : NEW_LINE INDENT tree [ ind ] = 0 NEW_LINE DEDENT return NEW_LINE DEDENT mid = ( high - low ) / 2 + low NEW_LINE mid = math . floor ( mid ) NEW_LINE mid = int ( mid ) NEW_LINE build_tree ( arr , tree , 2 * ind + 1 , low , mid , x ) NEW_LINE build_tree ( arr , tree , 2 * ind + 2 , mid + 1 , high , x ) NEW_LINE tree [ ind ] = combine ( tree [ 2 * ind + 1 ] , tree [ 2 * ind + 2 ] ) NEW_LINE DEDENT def query ( tree , ind , low , high , l , r ) : NEW_LINE INDENT mid = ( high - low ) / 2 + low NEW_LINE mid = math . floor ( mid ) NEW_LINE mid = int ( mid ) NEW_LINE if low > r or high < l : NEW_LINE INDENT return 0 NEW_LINE DEDENT if l <= low and r >= high : NEW_LINE INDENT return tree [ ind ] NEW_LINE DEDENT q1 = query ( tree , 2 * ind + 1 , low , mid , l , r ) NEW_LINE q2 = query ( tree , 2 * ind + 2 , mid + 1 , high , l , r ) NEW_LINE return combine ( q1 , q2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 3 , 9 , 13 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 6 NEW_LINE tree = [ [ ] for i in range ( 4 * n ) ] NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE build_tree ( arr , tree , 0 , 0 , n - 1 , k ) NEW_LINE print ( query ( tree , 0 , 0 , n - 1 , l - 1 , r - 1 ) ) NEW_LINE DEDENT
Calculate the Sum of GCD over all subarrays | Utility function to calculate sum of gcd of all sub - arrays . ; Fixing the starting index of a subarray ; Fixing the ending index of a subarray ; Finding the GCD of this subarray ; Adding this GCD in our sum ; Driver Code
def findGCDSum ( n , a ) : NEW_LINE INDENT GCDSum = 0 ; NEW_LINE tempGCD = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT tempGCD = 0 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT tempGCD = __gcd ( tempGCD , a [ k ] ) ; NEW_LINE DEDENT GCDSum += tempGCD ; NEW_LINE DEDENT DEDENT return GCDSum ; NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE totalSum = findGCDSum ( n , a ) ; NEW_LINE print ( totalSum ) ; NEW_LINE DEDENT
Check if the array has an element which is equal to XOR of remaining elements | Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver Code
def containsElement ( arr , n ) : NEW_LINE INDENT xorArr = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xorArr ^= arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT x = xorArr ^ arr [ i ] NEW_LINE if ( arr [ i ] == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 8 , 2 , 4 , 15 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE if ( containsElement ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the winner of the match | Multiple Queries | Function to add edge between two nodes ; Function returns topological order of given graph ; Indeg vector will store indegrees of all vertices ; Answer vector will have our final topological order ; Visited will be true if that vertex has been visited ; q will store the vertices that have indegree equal to zero ; Iterate till queue is not empty ; Push the front of queue to answer ; For all neighbours of u , decrement their indegree value ; If indegree of any vertex becomes zero and it is not marked then push it to queue ; Mark this vertex as visited ; Return the resultant topological order ; Function to return the winner between u and v ; Player who comes first wins ; Driver code ; Total number of players ; Build the graph add ( adj , x , y ) means x wins over y ; Resultant topological order in topotable ; Queries
def add ( adj , u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE DEDENT def topo ( adj , n ) : NEW_LINE INDENT indeg = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for x in adj [ i ] : NEW_LINE INDENT indeg [ x ] += 1 NEW_LINE DEDENT DEDENT answer = [ ] NEW_LINE visited = [ False for i in range ( n ) ] NEW_LINE q = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( indeg [ i ] == 0 ) : NEW_LINE INDENT q . append ( i ) NEW_LINE visited [ i ] = True NEW_LINE DEDENT DEDENT while ( len ( q ) != 0 ) : NEW_LINE INDENT u = q [ 0 ] NEW_LINE answer . append ( u ) NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE for x in adj [ u ] : NEW_LINE INDENT indeg [ x ] -= 1 NEW_LINE if ( indeg [ x ] == 0 and visited [ x ] == False ) : NEW_LINE INDENT q . append ( x ) NEW_LINE visited [ x ] = True NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT def who_wins ( topotable , u , v ) : NEW_LINE INDENT for x in topotable : NEW_LINE INDENT if ( x == u ) : NEW_LINE INDENT return u NEW_LINE DEDENT if ( x == v ) : NEW_LINE INDENT return v NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT adj = [ [ ] for i in range ( 10 ) ] NEW_LINE n = 7 NEW_LINE add ( adj , 0 , 1 ) NEW_LINE add ( adj , 0 , 2 ) NEW_LINE add ( adj , 0 , 3 ) NEW_LINE add ( adj , 1 , 5 ) NEW_LINE add ( adj , 2 , 5 ) NEW_LINE add ( adj , 3 , 4 ) NEW_LINE add ( adj , 4 , 5 ) NEW_LINE add ( adj , 6 , 0 ) NEW_LINE topotable = topo ( adj , n ) NEW_LINE print ( who_wins ( topotable , 3 , 5 ) ) NEW_LINE print ( who_wins ( topotable , 1 , 2 ) ) NEW_LINE DEDENT
Square root of a number without using sqrt ( ) function | Python3 implementation of the approach ; Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
import math NEW_LINE def Square ( n , i , j ) : NEW_LINE INDENT mid = ( i + j ) / 2 ; NEW_LINE mul = mid * mid ; NEW_LINE if ( ( mul == n ) or ( abs ( mul - n ) < 0.00001 ) ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT elif ( mul < n ) : NEW_LINE INDENT return Square ( n , mid , j ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return Square ( n , i , mid ) ; NEW_LINE DEDENT DEDENT def findSqrt ( n ) : NEW_LINE INDENT i = 1 ; NEW_LINE found = False ; NEW_LINE while ( found == False ) : NEW_LINE INDENT if ( i * i == n ) : NEW_LINE INDENT print ( i ) ; NEW_LINE found = True ; NEW_LINE DEDENT elif ( i * i > n ) : NEW_LINE INDENT res = Square ( n , i - 1 , i ) ; NEW_LINE print ( " { 0 : . 5f } " . format ( res ) ) NEW_LINE found = True NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE findSqrt ( n ) ; NEW_LINE DEDENT
Length of longest substring having all characters as K | Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
def length_substring ( S , K ) : NEW_LINE INDENT curr_cnt = 0 NEW_LINE prev_cnt = 0 NEW_LINE max_len = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == K ) : NEW_LINE INDENT curr_cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prev_cnt = max ( prev_cnt , curr_cnt ) NEW_LINE curr_cnt = 0 NEW_LINE DEDENT DEDENT prev_cnt = max ( prev_cnt , curr_cnt ) NEW_LINE max_len = prev_cnt NEW_LINE return max_len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcd1111aabc " NEW_LINE K = '1' NEW_LINE print ( length_substring ( S , K ) ) NEW_LINE DEDENT
Find a partition point in array to maximize its xor sum | Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call
def Xor_Sum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE index , left_xor = 0 , 0 NEW_LINE right_xor = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left_xor = left_xor ^ arr [ i ] NEW_LINE right_xor = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT right_xor = right_xor ^ arr [ j ] NEW_LINE DEDENT if ( left_xor + right_xor > sum ) : NEW_LINE INDENT sum = left_xor + right_xor NEW_LINE index = i NEW_LINE DEDENT DEDENT return index + 1 NEW_LINE DEDENT arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( Xor_Sum ( arr , n ) ) NEW_LINE
Find a partition point in array to maximize its xor sum | Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call
def ComputePrefixXor ( arr , PrefixXor , n ) : NEW_LINE INDENT PrefixXor [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; NEW_LINE DEDENT DEDENT def Xor_Sum ( arr , n ) : NEW_LINE INDENT PrefixXor = [ 0 ] * n ; NEW_LINE ComputePrefixXor ( arr , PrefixXor , n ) ; NEW_LINE sum , index = 0 , 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) : NEW_LINE INDENT sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; NEW_LINE index = i ; NEW_LINE DEDENT DEDENT return index + 1 ; NEW_LINE DEDENT arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( Xor_Sum ( arr , n ) ) ; NEW_LINE
Find the color of given node in an infinite binary tree | Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
def findColor ( mapWithColor , query ) : NEW_LINE INDENT maximum = 0 NEW_LINE while ( query >= 1 ) : NEW_LINE INDENT if ( query ) in mapWithColor . keys ( ) : NEW_LINE INDENT maximum = max ( maximum , mapWithColor [ query ] ) NEW_LINE DEDENT if ( query % 2 == 1 ) : NEW_LINE INDENT query = ( query - 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT query = query // 2 NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT def buildMapWithColor ( arr , n ) : NEW_LINE INDENT mapWithColor = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapWithColor [ arr [ i ] ] = i + 1 NEW_LINE DEDENT return mapWithColor NEW_LINE DEDENT arr = [ 3 , 2 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 7 NEW_LINE mapWithColor = buildMapWithColor ( arr , n ) NEW_LINE print ( findColor ( mapWithColor , k ) ) NEW_LINE
Count of strings in the first array which are smaller than every string in the second array | Python3 implementation of the approach ; Function to count the number of smaller strings in A for every in B ; Count the frequency of all characters ; Iterate for all possible strings in A ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every in B ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A which has the frequency of the smaller character less than the frequency of the smaller character of the in B ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
from bisect import bisect_left as lower_bound NEW_LINE MAX = 26 NEW_LINE def findCount ( a , b , n , m ) : NEW_LINE INDENT freq = [ 0 for i in range ( MAX ) ] NEW_LINE smallestFreq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT freq [ i ] = 0 NEW_LINE DEDENT for j in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for j in range ( MAX ) : NEW_LINE INDENT if ( freq [ j ] ) : NEW_LINE INDENT smallestFreq . append ( freq [ j ] ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT smallestFreq = sorted ( smallestFreq ) NEW_LINE ans = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT s = b [ i ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT freq [ i ] = 0 NEW_LINE DEDENT for j in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT frequency = 0 NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ( freq [ j ] ) : NEW_LINE INDENT frequency = freq [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT ind = lower_bound ( smallestFreq , frequency ) NEW_LINE ans . append ( ind ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def printAnswer ( a , b , n , m ) : NEW_LINE INDENT ans = findCount ( a , b , n , m ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ " aaa " , " aa " , " bdc " ] NEW_LINE B = [ " cccch " , " cccd " ] NEW_LINE n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE printAnswer ( A , B , n , m ) NEW_LINE
Find Leftmost and Rightmost node of BST from its given preorder traversal | Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
def LeftRightNode ( preorder , n ) : NEW_LINE INDENT min = 10 ** 9 NEW_LINE max = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( min > preorder [ i ] ) : NEW_LINE INDENT min = preorder [ i ] NEW_LINE DEDENT if ( max < preorder [ i ] ) : NEW_LINE INDENT max = preorder [ i ] NEW_LINE DEDENT DEDENT print ( " Leftmost ▁ node ▁ is ▁ " , min ) NEW_LINE print ( " Rightmost ▁ node ▁ is ▁ " , max ) NEW_LINE DEDENT preorder = [ 3 , 2 , 1 , 5 , 4 ] NEW_LINE n = len ( preorder ) NEW_LINE LeftRightNode ( preorder , n ) NEW_LINE
Find the position of the given row in a 2 | Python3 implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code
m = 6 ; NEW_LINE n = 4 ; NEW_LINE def compareRow ( a1 , a2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( a1 [ i ] < a2 [ i ] ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( a1 [ i ] > a2 [ i ] ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT def binaryCheck ( ar , arr ) : NEW_LINE INDENT l = 0 ; r = m - 1 ; NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 ; NEW_LINE temp = compareRow ( ar [ mid ] , arr ) ; NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT return mid + 1 ; NEW_LINE DEDENT elif ( temp == 1 ) : NEW_LINE INDENT l = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; NEW_LINE row = [ 10 , 9 , 22 , 23 ] ; NEW_LINE print ( binaryCheck ( mat , row ) ) ; NEW_LINE DEDENT
Number of subarrays having absolute sum greater than K | Set | Python3 implementation of the above approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value
from bisect import bisect as upper_bound NEW_LINE maxLen = 30 NEW_LINE def findCnt ( arr , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE if ( arr [ i ] > k or arr [ i ] < - 1 * k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( arr [ 0 ] > k or arr [ 0 ] < - 1 * k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += n - upper_bound ( arr , arr [ i ] + k ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ - 1 , 4 , - 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 0 NEW_LINE print ( findCnt ( arr , n , k ) ) NEW_LINE
Number of intersections between two ranges | Python3 implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code
from bisect import bisect as upper_bound NEW_LINE def FindIntersection ( type1 , n , type2 , m ) : NEW_LINE INDENT ans = n * m NEW_LINE start = [ ] NEW_LINE end = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT start . append ( type1 [ i ] [ 0 ] ) NEW_LINE end . append ( type1 [ i ] [ 1 ] ) NEW_LINE DEDENT start = sorted ( start ) NEW_LINE start = sorted ( end ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT L = type2 [ i ] [ 0 ] NEW_LINE R = type2 [ i ] [ 1 ] NEW_LINE ans -= ( len ( start ) - upper_bound ( start , R ) ) NEW_LINE ans -= ( upper_bound ( end , L - 1 ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT type1 = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 6 , 7 ] ] NEW_LINE type2 = [ [ 1 , 5 ] , [ 2 , 3 ] , [ 4 , 7 ] , [ 5 , 7 ] ] NEW_LINE n = len ( type1 ) NEW_LINE m = len ( type2 ) NEW_LINE print ( FindIntersection ( type1 , n , type2 , m ) ) NEW_LINE
Find the Nth term divisible by a or b or c | Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( ( a * b ) // gcd ( a , b ) ) NEW_LINE DEDENT def divTermCount ( a , b , c , num ) : NEW_LINE INDENT return ( ( num // a ) + ( num // b ) + ( num // c ) - ( num // lcm ( a , b ) ) - ( num // lcm ( b , c ) ) - ( num // lcm ( a , c ) ) + ( num // lcm ( a , lcm ( b , c ) ) ) ) NEW_LINE DEDENT def findNthTerm ( a , b , c , n ) : NEW_LINE INDENT low = 1 NEW_LINE high = 10 ** 9 NEW_LINE mid = 0 NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( divTermCount ( a , b , c , mid ) < n ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT a = 2 NEW_LINE b = 3 NEW_LINE c = 5 NEW_LINE n = 10 NEW_LINE print ( findNthTerm ( a , b , c , n ) ) NEW_LINE
Split the given array into K sub | Function to check if mid can be maximum sub - arrays sum ; If individual element is greater maximum possible sum ; Increase sum of current sub - array ; If the sum is greater than mid increase count ; Check condition ; Function to find maximum subarray sum which is minimum ; start = max ( array ) Max subarray sum , considering subarray of length 1 ; Answer stores possible maximum sub array sum ; If mid is possible solution Put answer = mid ; ; Driver Code
def check ( mid , array , n , K ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( array [ i ] > mid ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum += array [ i ] NEW_LINE if ( sum > mid ) : NEW_LINE INDENT count += 1 NEW_LINE sum = array [ i ] NEW_LINE DEDENT DEDENT count += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solve ( array , n , K ) : NEW_LINE INDENT end = 0 NEW_LINE for i in range ( n ) : NEW_LINE answer = 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( check ( mid , array , n , K ) ) : NEW_LINE INDENT answer = mid NEW_LINE end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT array = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( array ) NEW_LINE K = 3 NEW_LINE print ( solve ( array , n , K ) ) NEW_LINE DEDENT
Count number of common elements between two arrays by using Bitset and Bitwise operation | Python3 implementation of the approach ; Function to return the count of common elements ; Traverse the first array ; Set 1 at ( index ) position a [ i ] ; Traverse the second array ; Set 1 at ( index ) position b [ i ] ; Bitwise AND of both the bitsets ; Find the count of 1 's  ; Driver code
MAX = 100000 NEW_LINE bit1 , bit2 , bit3 = 0 , 0 , 0 NEW_LINE def count_common ( a , n , b , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT global bit1 , bit2 , bit3 NEW_LINE bit1 = bit1 | ( 1 << a [ i ] ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT bit2 = bit2 | ( 1 << b [ i ] ) NEW_LINE DEDENT bit3 = bit1 & bit2 ; NEW_LINE count = bin ( bit3 ) . count ( '1' ) ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 7 , 2 , 3 ] ; NEW_LINE b = [ 2 , 11 , 7 , 4 , 15 , 20 , 24 ] ; NEW_LINE n = len ( a ) ; NEW_LINE m = len ( b ) ; NEW_LINE print ( count_common ( a , n , b , m ) ) ; NEW_LINE DEDENT