codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest Fibonacci subarray formed by removing only one element | Python3 program to find length of the longest subarray with all fibonacci numbers ; Function to create hash table to check for Fibonacci numbers ; Insert first two fibnonacci numbers ; Summation of last two numbers ; Update the variable each time ; Function to find the longest fibonacci subarray ; Find maximum value in the array ; Creating a set containing Fibonacci numbers ; Left array is used to count number of continuous fibonacci numbers starting from left of current element ; Check if current element is a fibonacci number ; Right array is used to count number of continuous fibonacci numbers starting from right of current element ; Check if current element is a fibonacci number ; Driver code
N = 100000 NEW_LINE def createHash ( hash , maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 NEW_LINE hash . add ( prev ) NEW_LINE hash . add ( curr ) NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE hash . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def longestFibSubarray ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE hash = { int } NEW_LINE createHash ( hash , max_val ) NEW_LINE left = [ 0 for i in range ( n ) ] NEW_LINE right = [ 0 for i in range ( n ) ] NEW_LINE fibcount = 0 NEW_LINE res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left [ i ] = fibcount NEW_LINE if ( arr [ i ] in hash ) : NEW_LINE INDENT fibcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT fibcount = 0 NEW_LINE DEDENT DEDENT fibcount = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT right [ i ] = fibcount NEW_LINE if ( arr [ i ] in hash ) : NEW_LINE INDENT fibcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT fibcount = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT res = max ( res , left [ i ] + right [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 2 , 8 , 5 , 7 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestFibSubarray ( arr , n ) ) NEW_LINE
Minimum window size containing atleast P primes in every window of given range | Python3 implementation to find the minimum window size in the range such that each window of that size contains atleast P primes ; Function to check that a number is a prime or not in O ( sqrt ( N ) ) ; Loop to check if any number number is divisible by any other number or not ; Function to check whether window size satisfies condition or not ; Loop to check each window of size have atleast P primes ; Checking condition using prefix sum ; Function to find the minimum window size possible for the given range in X and Y ; Prefix array ; Mark those numbers which are primes as 1 ; Convert to prefix sum ; Applying binary search over window size ; Check whether mid satisfies the condition or not ; If satisfies search in first half ; Else search in second half ; Driver Code
from math import sqrt NEW_LINE def isPrime ( N ) : NEW_LINE INDENT if ( N < 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( N < 4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ( N & 1 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( N % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr = 5 NEW_LINE s = sqrt ( N ) NEW_LINE while ( curr <= s ) : NEW_LINE INDENT if ( N % curr == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr += 2 NEW_LINE if ( N % curr == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr += 4 NEW_LINE DEDENT return True NEW_LINE DEDENT def check ( s , p , prefix_sum , n ) : NEW_LINE INDENT satisfies = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i + s - 1 >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( i - 1 >= 0 ) : NEW_LINE INDENT x = prefix_sum [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT x = 0 NEW_LINE DEDENT if ( prefix_sum [ i + s - 1 ] - x < p ) : NEW_LINE INDENT satisfies = False NEW_LINE DEDENT DEDENT return satisfies NEW_LINE DEDENT def minimumWindowSize ( x , y , p ) : NEW_LINE INDENT prefix_sum = [ 0 ] * ( y - x + 1 ) NEW_LINE for i in range ( x , y + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT prefix_sum [ i - x ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , y - x + 1 ) : NEW_LINE INDENT prefix_sum [ i ] += prefix_sum [ i - 1 ] NEW_LINE DEDENT low = 1 NEW_LINE high = y - x + 1 NEW_LINE while ( high - low > 1 ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( check ( mid , p , prefix_sum , y - x + 1 ) ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT if ( check ( low , p , prefix_sum , y - x + 1 ) ) : NEW_LINE INDENT return low NEW_LINE DEDENT return high NEW_LINE DEDENT x = 12 NEW_LINE y = 42 NEW_LINE p = 3 NEW_LINE print ( minimumWindowSize ( x , y , p ) ) NEW_LINE
XOR of pairwise sum of every unordered pairs in an array | Function to find XOR of pairwise sum of every unordered pairs ; Loop to choose every possible pairs in the array ; Driver Code
def xorOfSum ( a , n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT answer ^= ( a [ i ] + a [ j ] ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE A = [ 1 , 2 , 3 ] NEW_LINE print ( xorOfSum ( A , n ) ) NEW_LINE DEDENT
Maximum size of square such that all submatrices of that size have sum less than K | Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Loop to choose the size of matrix ; Loop to find the sum of the matrix of every possible submatrix ; Driver Code
N = 4 NEW_LINE M = 5 NEW_LINE def preProcess ( mat , aux ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT aux [ 0 ] [ i ] = mat [ 0 ] [ i ] NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT aux [ i ] [ j ] = ( mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT aux [ i ] [ j ] += aux [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT def sumQuery ( aux , tli , tlj , rbi , rbj ) : NEW_LINE INDENT res = aux [ rbi ] [ rbj ] NEW_LINE if ( tli > 0 ) : NEW_LINE INDENT res = res - aux [ tli - 1 ] [ rbj ] NEW_LINE DEDENT if ( tlj > 0 ) : NEW_LINE INDENT res = res - aux [ rbi ] [ tlj - 1 ] NEW_LINE DEDENT if ( tli > 0 and tlj > 0 ) : NEW_LINE INDENT res = ( res + aux [ tli - 1 ] [ tlj - 1 ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def maximumSquareSize ( mat , K ) : NEW_LINE INDENT aux = [ [ 0 for x in range ( M ) ] for y in range ( N ) ] NEW_LINE preProcess ( mat , aux ) NEW_LINE for i in range ( min ( N , M ) , 0 , - 1 ) : NEW_LINE INDENT satisfies = True NEW_LINE for x in range ( N ) : NEW_LINE INDENT for y in range ( M ) : NEW_LINE INDENT if ( x + i - 1 <= N - 1 and y + i - 1 <= M - 1 ) : NEW_LINE INDENT if ( sumQuery ( aux , x , y , x + i - 1 , y + i - 1 ) > K ) : NEW_LINE INDENT satisfies = False NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( satisfies == True ) : NEW_LINE INDENT return ( i ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 30 NEW_LINE mat = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 5 , 3 , 8 , 1 , 2 ] , [ 4 , 6 , 7 , 5 , 5 ] , [ 2 , 4 , 8 , 9 , 4 ] ] NEW_LINE print ( maximumSquareSize ( mat , K ) ) NEW_LINE DEDENT
Find two Fibonacci numbers whose sum can be represented as N | Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Printing the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driven code
def createHash ( hash1 , maxElement ) : NEW_LINE INDENT prev , curr = 0 , 1 NEW_LINE hash1 . add ( prev ) NEW_LINE hash1 . add ( curr ) NEW_LINE while ( curr < maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE hash1 . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def findFibonacciPair ( n ) : NEW_LINE INDENT hash1 = set ( ) NEW_LINE createHash ( hash1 , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i in hash1 and ( n - i ) in hash1 ) : NEW_LINE INDENT print ( i , " , ▁ " , ( n - i ) ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 90 NEW_LINE findFibonacciPair ( N ) NEW_LINE DEDENT
Count of multiples in an Array before every element | Python 3 program to count of multiples in an Array before every element ; Function to find all factors of N and keep their count in map ; Traverse from 1 to sqrt ( N ) if i divides N , increment i and N / i in map ; Function to count of multiples in an Array before every element ; To store factors all of all numbers ; Traverse for all possible i 's ; Printing value of a [ i ] in map ; Now updating the factors of a [ i ] in the map ; Driver code ; Function call
from collections import defaultdict NEW_LINE import math NEW_LINE def add_factors ( n , mp ) : NEW_LINE INDENT for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 , ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT mp [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ i ] += 1 NEW_LINE mp [ n // i ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def count_divisors ( a , n ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mp [ a [ i ] ] , end = " ▁ " ) NEW_LINE add_factors ( a [ i ] , mp ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE count_divisors ( arr , n ) NEW_LINE DEDENT
Reduce the array such that each element appears at most 2 times | Function to remove duplicates ; Initialise 2 nd pointer ; Iterate over the array ; Updating the 2 nd pointer ; Driver code ; Function call
def removeDuplicates ( arr , n ) : NEW_LINE INDENT st = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n - 2 and arr [ i ] == arr [ i + 1 ] and arr [ i ] == arr [ i + 2 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ st ] = arr [ i ] ; NEW_LINE st += 1 ; NEW_LINE DEDENT DEDENT print ( " { " , end = " " ) NEW_LINE for i in range ( st ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) ; NEW_LINE if ( i != st - 1 ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) ; NEW_LINE DEDENT DEDENT print ( " } " , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE removeDuplicates ( arr , n ) ; NEW_LINE DEDENT
Check if an Array is a permutation of numbers from 1 to N | Function to check if an array represents a permutation or not ; Set to check the count of non - repeating elements ; Insert all elements in the set ; Calculating the max element ; Check if set size is equal to n ; Driver code
def permutation ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE maxEle = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( arr [ i ] ) ; NEW_LINE maxEle = max ( maxEle , arr [ i ] ) ; NEW_LINE DEDENT if ( maxEle != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( len ( s ) == n ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( permutation ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
D 'Esopo | Python3 implementation for D 'Esopo-Pape algorithm ; Number of vertices in graph ; Adjacency list of graph ; Queue to store unoperated vertices ; Distance from source vertex ; Status of vertex ; let 0 be the source vertex ; Pop from front of the queue ; scan adjacent vertices of u ; e < - [ weight , vertex ] ; if e [ 1 ] is entering first time in the queue ; Append at back of queue ; Append at front of queue ; Driver Code ; Adjacency matrix of graph
from collections import defaultdict , deque NEW_LINE def desopo ( graph ) : NEW_LINE INDENT v = len ( graph ) NEW_LINE adj = defaultdict ( list ) NEW_LINE for i in range ( v ) : NEW_LINE INDENT for j in range ( i + 1 , v ) : NEW_LINE INDENT if graph [ i ] [ j ] != 0 : NEW_LINE INDENT adj [ i ] . append ( [ graph [ i ] [ j ] , j ] ) NEW_LINE adj [ j ] . append ( [ graph [ i ] [ j ] , i ] ) NEW_LINE DEDENT DEDENT DEDENT q = deque ( [ ] ) NEW_LINE distance = [ float ( ' inf ' ) ] * v NEW_LINE is_in_queue = [ False ] * v NEW_LINE source = 0 NEW_LINE distance = 0 NEW_LINE q . append ( source ) NEW_LINE is_in_queue = True NEW_LINE while q : NEW_LINE INDENT u = q . popleft ( ) NEW_LINE is_in_queue [ u ] = False NEW_LINE for e in adj [ u ] : NEW_LINE INDENT if distance [ e [ 1 ] ] > distance [ u ] + e [ 0 ] : NEW_LINE INDENT distance [ e [ 1 ] ] = distance [ u ] + e [ 0 ] NEW_LINE if is_in_queue [ e [ 1 ] ] == False : NEW_LINE INDENT if distance [ e [ 1 ] ] == float ( ' inf ' ) : NEW_LINE INDENT q . append ( e [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT q . appendleft ( e [ 1 ] ) NEW_LINE DEDENT is_in_queue [ e [ 1 ] ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT return distance NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT graph = [ [ 0 , 4 , 0 , 0 , 8 ] , [ 0 , 0 , 8 , 0 , 11 ] , [ 0 , 8 , 0 , 2 , 0 ] , [ 0 , 0 , 2 , 0 , 1 ] , [ 8 , 11 , 0 , 1 , 0 ] ] NEW_LINE print ( desopo ( graph ) ) NEW_LINE DEDENT
Count of Subsets of a given Set with element X present in it | Python3 code to implement the above approach ; N stores total number of subsets ; Generate each subset one by one ; Check every bit of i ; if j 'th bit of i is set,  check arr[j] with X ; Driver code
def CountSubSet ( arr , n , X ) : NEW_LINE INDENT N = 2 ** n ; NEW_LINE count = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT if ( arr [ j ] == X ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 6 , 7 ] ; NEW_LINE X = 5 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( CountSubSet ( arr , n , X ) ) ; NEW_LINE DEDENT
Count of Subsets of a given Set with element X present in it | Function to calculate ( 2 ^ ( n - 1 ) ) ; Initially initialize answer to 1 ; If e is odd , multiply b with answer ; Function to count subsets in which X element is present ; Check if X is present in given subset or not ; If X is present in set then calculate 2 ^ ( n - 1 ) as count ; if X is not present in a given set ; Driver code
def calculatePower ( b , e ) : NEW_LINE INDENT ans = 1 ; NEW_LINE while ( e > 0 ) : NEW_LINE INDENT if ( e % 2 == 1 ) : NEW_LINE INDENT ans = ans * b ; NEW_LINE DEDENT e = e // 2 ; NEW_LINE b = b * b ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def CountSubSet ( arr , n , X ) : NEW_LINE INDENT count = 0 ; checkX = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == X ) : NEW_LINE INDENT checkX = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( checkX == 1 ) : NEW_LINE INDENT count = calculatePower ( 2 , n - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 6 , 7 ] ; NEW_LINE X = 5 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( CountSubSet ( arr , n , X ) ) ; NEW_LINE DEDENT
Value to be subtracted from array elements to make sum of all elements equals K | Function to return the amount of wood collected if the cut is made at height m ; Function that returns Height at which cut should be made ; Sort the heights of the trees ; The minimum and the maximum cut that can be made ; Binary search to find the answer ; The amount of wood collected when cut is made at the mid ; If the current collected wood is equal to the required amount ; If it is more than the required amount then the cut needs to be made at a height higher than the current height ; Else made the cut at a lower height ; Driver code
def woodCollected ( height , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( height [ i ] - m <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT sum += ( height [ i ] - m ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def collectKWood ( height , n , k ) : NEW_LINE INDENT height = sorted ( height ) NEW_LINE low = 0 NEW_LINE high = height [ n - 1 ] NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( ( high - low ) // 2 ) NEW_LINE collected = woodCollected ( height , n , mid ) NEW_LINE if ( collected == k ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( collected > k ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT height = [ 1 , 2 , 1 , 2 ] NEW_LINE n = len ( height ) NEW_LINE k = 2 NEW_LINE print ( collectKWood ( height , n , k ) ) NEW_LINE
Check if given string contains all the digits | Python3 implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if st contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the string ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in st ; Driver code
MAX = 10 NEW_LINE def isDigit ( ch ) : NEW_LINE INDENT ch = ord ( ch ) NEW_LINE if ( ch >= ord ( '0' ) and ch <= ord ( '9' ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def allDigits ( st , le ) : NEW_LINE INDENT present = [ False for i in range ( MAX ) ] NEW_LINE for i in range ( le ) : NEW_LINE INDENT if ( isDigit ( st [ i ] ) ) : NEW_LINE INDENT digit = ord ( st [ i ] ) - ord ( '0' ) NEW_LINE present [ digit ] = True NEW_LINE DEDENT DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( present [ i ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT st = " Geeks12345for69708" NEW_LINE le = len ( st ) NEW_LINE if ( allDigits ( st , le ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if a symmetric plus is possible from the elements of the given array | Function that return true if a symmetric is possible with the elements of the array ; Map to store the frequency of the array elements ; Traverse through array elements and count frequencies ; For every unique element ; Element has already been found ; The frequency of the element something other than 0 and 1 ; Driver code
def isPlusPossible ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] = mp . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT foundModOne = False NEW_LINE for x in mp : NEW_LINE INDENT element = x NEW_LINE frequency = mp [ x ] NEW_LINE if ( frequency % 4 == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( frequency % 4 == 1 ) : NEW_LINE INDENT if ( foundModOne == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT foundModOne = True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isPlusPossible ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Queries for elements greater than K in the given index range using Segment Tree | Python3 implementation of the approach ; Merge procedure to merge two vectors into a single vector ; Final vector to return after merging ; Loop continues until it reaches the end of one of the vectors ; Here , simply add the remaining elements to the vector v ; Procedure to build the segment tree ; Reached the leaf node of the segment tree ; Recursively call the buildTree on both the nodes of the tree ; Storing the final vector after merging the two of its sorted child vector ; Query procedure to get the answer for each query l and r are query range ; out of bound or no overlap ; Complete overlap Query range completely lies in the segment tree node range ; binary search to find index of k ; Partially overlap Query range partially lies in the segment tree node range ; Function to perform the queries ; Driver code ; 1 - based indexing ; Number of queries
from bisect import bisect_left as lower_bound NEW_LINE def merge ( v1 , v2 ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE v = [ ] NEW_LINE while ( i < len ( v1 ) and j < len ( v2 ) ) : NEW_LINE INDENT if ( v1 [ i ] <= v2 [ j ] ) : NEW_LINE INDENT v . append ( v1 [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( v2 [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for k in range ( i , len ( v1 ) ) : NEW_LINE INDENT v . append ( v1 [ k ] ) NEW_LINE DEDENT for k in range ( j , len ( v2 ) ) : NEW_LINE INDENT v . append ( v2 [ k ] ) NEW_LINE DEDENT return v NEW_LINE DEDENT def buildTree ( tree , arr , index , s , e ) : NEW_LINE INDENT if ( s == e ) : NEW_LINE INDENT tree [ index ] . append ( arr [ s ] ) NEW_LINE return NEW_LINE DEDENT mid = ( s + e ) // 2 NEW_LINE buildTree ( tree , arr , 2 * index , s , mid ) NEW_LINE buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e ) NEW_LINE tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] ) NEW_LINE DEDENT def query ( tree , index , s , e , l , r , k ) : NEW_LINE INDENT if ( r < s or l > e ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( s >= l and e <= r ) : NEW_LINE INDENT return len ( tree [ index ] ) - ( lower_bound ( tree [ index ] , k ) ) NEW_LINE DEDENT mid = ( s + e ) // 2 NEW_LINE return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) ) NEW_LINE DEDENT def performQueries ( L , R , K , n , q , tree ) : NEW_LINE INDENT for i in range ( q ) : NEW_LINE INDENT print ( query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 3 , 9 , 13 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE tree = [ [ ] for i in range ( 4 * n + 1 ) ] NEW_LINE buildTree ( tree , arr , 1 , 0 , n - 1 ) NEW_LINE L = [ 1 , 2 ] NEW_LINE R = [ 4 , 6 ] NEW_LINE K = [ 6 , 8 ] NEW_LINE q = len ( L ) NEW_LINE performQueries ( L , R , K , n , q , tree ) NEW_LINE DEDENT