codeparrot/xlcost-text-to-code huggingface dataset:
Find the node at the center of an N | To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver code
tree = { } NEW_LINE path = [ ] NEW_LINE maxHeight , maxHeightNode = - 1 , - 1 NEW_LINE def getDiameterPath ( vertex , targetVertex , parent , path ) : NEW_LINE INDENT if ( vertex == targetVertex ) : NEW_LINE INDENT path . append ( vertex ) NEW_LINE return True NEW_LINE DEDENT for i in range ( len ( tree [ vertex ] ) ) : NEW_LINE INDENT if ( tree [ vertex ] [ i ] == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( getDiameterPath ( tree [ vertex ] [ i ] , targetVertex , vertex , path ) ) : NEW_LINE INDENT path . append ( vertex ) NEW_LINE return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def farthestNode ( vertex , parent , height ) : NEW_LINE INDENT global maxHeight , maxHeightNode NEW_LINE if ( height > maxHeight ) : NEW_LINE INDENT maxHeight = height NEW_LINE maxHeightNode = vertex NEW_LINE DEDENT if ( vertex in tree ) : NEW_LINE INDENT for i in range ( len ( tree [ vertex ] ) ) : NEW_LINE INDENT if ( tree [ vertex ] [ i ] == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT farthestNode ( tree [ vertex ] [ i ] , vertex , height + 1 ) NEW_LINE DEDENT DEDENT DEDENT def addedge ( a , b ) : NEW_LINE INDENT if ( a not in tree ) : NEW_LINE INDENT tree [ a ] = [ ] NEW_LINE DEDENT tree [ a ] . append ( b ) NEW_LINE if ( b not in tree ) : NEW_LINE INDENT tree [ b ] = [ ] NEW_LINE DEDENT tree [ b ] . append ( a ) NEW_LINE DEDENT def FindCenter ( n ) : NEW_LINE INDENT maxHeight = - 1 NEW_LINE maxHeightNode = - 1 NEW_LINE farthestNode ( 0 , - 1 , 0 ) NEW_LINE leaf1 = maxHeightNode NEW_LINE maxHeight = - 1 NEW_LINE farthestNode ( maxHeightNode , - 1 , 0 ) NEW_LINE leaf2 = maxHeightNode NEW_LINE path = [ ] NEW_LINE getDiameterPath ( leaf1 , leaf2 , - 1 , path ) NEW_LINE pathSize = len ( path ) NEW_LINE if ( pathSize % 2 == 1 ) : NEW_LINE INDENT print ( path [ int ( pathSize / 2 ) ] * - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( path [ int ( pathSize / 2 ) ] , " , ▁ " , path [ int ( ( pathSize - 1 ) / 2 ) ] , sep = " " , end = " " ) NEW_LINE DEDENT DEDENT N = 4 NEW_LINE tree = { } NEW_LINE addedge ( 1 , 0 ) NEW_LINE addedge ( 1 , 2 ) NEW_LINE addedge ( 1 , 3 ) NEW_LINE FindCenter ( N ) NEW_LINE
K | Python3 program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
maxm = 1000000000 NEW_LINE def count ( v , n ) : NEW_LINE INDENT odd , even = 0 , 0 NEW_LINE t = 1 << len ( v ) NEW_LINE size = len ( v ) NEW_LINE for i in range ( 1 , t ) : NEW_LINE INDENT d , count = 1 , 0 NEW_LINE for j in range ( 0 , size ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT d *= v [ j ] NEW_LINE count += 1 NEW_LINE DEDENT DEDENT if ( count & 1 ) : NEW_LINE INDENT odd += n // d NEW_LINE DEDENT else : NEW_LINE INDENT even += n // d NEW_LINE DEDENT DEDENT return ( odd - even ) NEW_LINE DEDENT def BinarySearch ( l , r , v , key ) : NEW_LINE INDENT while ( r - l > 1 ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( key <= count ( v , mid ) ) : NEW_LINE INDENT r = mid NEW_LINE DEDENT else : NEW_LINE INDENT l = mid NEW_LINE DEDENT DEDENT if ( key == count ( v , l ) ) : NEW_LINE INDENT return l NEW_LINE DEDENT else : NEW_LINE INDENT return r NEW_LINE DEDENT DEDENT N , K = 2 , 10 NEW_LINE v = [ 2 , 3 ] NEW_LINE print ( BinarySearch ( 1 , maxm , v , K ) ) NEW_LINE
Find integral points with minimum distance from given set of integers using BFS | Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; Vector to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code
def minDistancePoints ( A , K , n ) : NEW_LINE INDENT m = { } NEW_LINE q = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] = 1 NEW_LINE q . append ( A [ i ] ) NEW_LINE DEDENT ans = [ ] NEW_LINE while ( K > 0 ) : NEW_LINE INDENT x = q [ 0 ] NEW_LINE q = q [ 1 : : ] NEW_LINE if ( ( x - 1 ) not in m and K > 0 ) : NEW_LINE INDENT m [ x - 1 ] = m . get ( x - 1 , 0 ) + 1 NEW_LINE q . append ( x - 1 ) NEW_LINE ans . append ( x - 1 ) NEW_LINE K -= 1 NEW_LINE DEDENT if ( ( x + 1 ) not in m and K > 0 ) : NEW_LINE INDENT m [ x + 1 ] = m . get ( x + 1 , 0 ) + 1 NEW_LINE q . append ( x + 1 ) NEW_LINE ans . append ( x + 1 ) NEW_LINE K -= 1 NEW_LINE DEDENT DEDENT for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ - 1 , 4 , 6 ] NEW_LINE K = 3 NEW_LINE n = len ( A ) NEW_LINE minDistancePoints ( A , K , n ) NEW_LINE DEDENT
Leftmost Column with atleast one 1 in a row | Python3 implementation to find the Leftmost Column with atleast a 1 in a sorted binary matrix ; Function to search for the leftmost column of the matrix with atleast a 1 in sorted binary matrix ; Loop to iterate over all the rows of the matrix ; Binary Search to find the leftmost occurence of the 1 ; Condition if the column contains the 1 at this position of matrix ; If there is a better solution then update the answer ; Condition if the solution doesn 't exist in the matrix ; Driver Code
import sys NEW_LINE N = 3 NEW_LINE def search ( mat , n , m ) : NEW_LINE INDENT a = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT low = 0 NEW_LINE high = m - 1 NEW_LINE ans = sys . maxsize NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( mat [ i ] [ mid ] == 1 ) : NEW_LINE INDENT if ( mid == 0 ) : NEW_LINE INDENT ans = 0 NEW_LINE break NEW_LINE DEDENT elif ( mat [ i ] [ mid - 1 ] == 0 ) : NEW_LINE INDENT ans = mid NEW_LINE break NEW_LINE DEDENT DEDENT if ( mat [ i ] [ mid ] == 1 ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT if ( ans < a ) : NEW_LINE INDENT a = ans NEW_LINE DEDENT DEDENT if ( a == sys . maxsize ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return a + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] NEW_LINE print ( search ( mat , 3 , 3 ) ) NEW_LINE DEDENT
Largest index for each distinct character in given string with frequency K | Python3 implementation of the approach Function to find largest index for each distinct character occuring exactly K times . ; Function to find largest index for each distinct character occuring exactly K times ; Finding all characters present in S ; Finding all distinct characters in S ; To store result for each character ; Loop through each lower case English character ; If current character is absent in s ; Getting current character ; Finding count of character ch in S ; To store max Index encountred so far ; Printing required result ; If no such character exists , print - 1 ; Driver code
def maxSubstring ( S , K , N ) : NEW_LINE INDENT def maxSubstring ( S , K , N ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ ord ( S [ i ] ) - 97 ] = 1 NEW_LINE DEDENT answer = [ ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ch = chr ( i + 97 ) NEW_LINE count = 0 NEW_LINE index = - 1 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( S [ j ] == ch ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count == K ) : NEW_LINE INDENT index = j NEW_LINE DEDENT DEDENT answer . append ( [ ch , index ] ) NEW_LINE DEDENT flag = 0 NEW_LINE for i in range ( len ( answer ) ) : NEW_LINE INDENT if ( answer [ i ] [ 1 ] > - 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE print ( answer [ i ] [ 0 ] , answer [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " cbaabaacbcd " NEW_LINE K = 2 NEW_LINE N = len ( S ) NEW_LINE maxSubstring ( S , K , N ) NEW_LINE DEDENT
Smallest number greater than n that can be represented as a sum of distinct power of k | Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; Vector P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code
def greaterK ( n , k ) : NEW_LINE INDENT index = 0 NEW_LINE p = [ 0 for i in range ( n + 2 ) ] NEW_LINE x = n NEW_LINE while ( x > 0 ) : NEW_LINE INDENT p [ index ] = x % k NEW_LINE x //= k NEW_LINE index += 1 NEW_LINE DEDENT idx = 0 NEW_LINE for i in range ( 0 , len ( p ) - 1 , 1 ) : NEW_LINE INDENT if ( p [ i ] >= 2 ) : NEW_LINE INDENT p [ i ] = 0 NEW_LINE p [ i + 1 ] += 1 NEW_LINE for j in range ( idx , i , 1 ) : NEW_LINE INDENT p [ j ] = 0 NEW_LINE DEDENT idx = i + 1 NEW_LINE DEDENT if ( p [ i ] == k ) : NEW_LINE INDENT p [ i ] = 0 NEW_LINE p [ i + 1 ] += 1 NEW_LINE DEDENT DEDENT j = len ( p ) - 1 NEW_LINE if ( p [ j ] >= 2 ) : NEW_LINE INDENT p [ index ] = 1 NEW_LINE index += 1 NEW_LINE DEDENT ans = 0 NEW_LINE i = len ( p ) - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT ans = ans * k + p [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 29 NEW_LINE k = 7 NEW_LINE greaterK ( n , k ) NEW_LINE DEDENT
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | Function that returns true if the can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code
def canBeBalanced ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE count_1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT count_1 = min ( count_1 , count ) NEW_LINE DEDENT if ( count_1 >= - 1 and count == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT s = " ( ) ) ( ) ( " NEW_LINE n = len ( s ) NEW_LINE if ( canBeBalanced ( s , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Absolute difference between the XOR of Non | Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver code ; Find the absolute difference
def calculateDifference ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , max_val + 1 ) : NEW_LINE INDENT if p * p > max_val + 1 : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT X1 = 1 NEW_LINE X2 = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT X1 ^= arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] != 1 ) : NEW_LINE INDENT X2 ^= arr [ i ] NEW_LINE DEDENT DEDENT return abs ( X1 - X2 ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( calculateDifference ( arr , n ) ) NEW_LINE
Search in a trie Recursively | Python3 program to traverse in bottom up manner ; Trie node ; endOfWord is true if the node represents end of a word ; Function will return the new node ( initialized to NULLs ) ; Function will insert the string in a trie recursively ; Insert a new node ; Recursive call for insertion of string ; Make the endOfWord true which represents the end of string ; Function call to insert a string ; Function call with necessary arguments ; Function to search the string in a trie recursively ; When a string or any character of a string is not found ; Condition of finding string successfully ; Return true when endOfWord of last node containes true ; Recursive call and return value of function call stack ; Function call to search the string ; If string found ; Driver code ; Function call to insert the string ; Function call to search the string
CHILDREN = 26 NEW_LINE MAX = 100 NEW_LINE class trie : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . child = [ None for i in range ( CHILDREN ) ] NEW_LINE self . endOfWord = False NEW_LINE DEDENT DEDENT def createNode ( ) : NEW_LINE INDENT temp = trie ( ) NEW_LINE return temp NEW_LINE DEDENT def insertRecursively ( itr , str , i ) : NEW_LINE INDENT if ( i < len ( str ) ) : NEW_LINE INDENT index = ord ( str [ i ] ) - ord ( ' a ' ) NEW_LINE if ( itr . child [ index ] == None ) : NEW_LINE INDENT itr . child [ index ] = createNode ( ) ; NEW_LINE DEDENT insertRecursively ( itr . child [ index ] , str , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT itr . endOfWord = True ; NEW_LINE DEDENT DEDENT def insert ( itr , str ) : NEW_LINE INDENT insertRecursively ( itr , str , 0 ) ; NEW_LINE DEDENT def searchRecursively ( itr , str , i , len ) : NEW_LINE INDENT if ( itr == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( itr . endOfWord == True and i == len - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT index = ord ( str [ i ] ) - ord ( ' a ' ) NEW_LINE return searchRecursively ( itr . child [ index ] , str , i + 1 , len ) NEW_LINE DEDENT def search ( root , str ) : NEW_LINE INDENT arr = [ ' ' for i in range ( len ( str ) + 1 ) ] NEW_LINE arr = str NEW_LINE if ( searchRecursively ( root , arr , 0 , len ( str ) + 1 ) ) : NEW_LINE INDENT print ( " found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " not ▁ found " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = createNode ( ) ; NEW_LINE insert ( root , " thier " ) ; NEW_LINE insert ( root , " there " ) ; NEW_LINE insert ( root , " answer " ) ; NEW_LINE insert ( root , " any " ) ; NEW_LINE search ( root , " anywhere " ) NEW_LINE search ( root , " answer " ) NEW_LINE DEDENT
Count duplicates in a given linked list | Python3 implementation of the approach ; Representation of node ; Function to push a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; print ( 1 ) Starting from the next node ; print ( 2 ) If some duplicate node is found ; Return the count of duplicate nodes ; Driver code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head , item ) : NEW_LINE INDENT temp = Node ( item ) ; NEW_LINE temp . data = item ; NEW_LINE temp . next = head ; NEW_LINE head = temp ; NEW_LINE return head ; NEW_LINE DEDENT def countNode ( head ) : NEW_LINE INDENT count = 0 NEW_LINE while ( head . next != None ) : NEW_LINE INDENT ptr = head . next NEW_LINE while ( ptr != None ) : NEW_LINE INDENT if ( head . data == ptr . data ) : NEW_LINE INDENT count = count + 1 NEW_LINE break NEW_LINE DEDENT ptr = ptr . next NEW_LINE DEDENT head = head . next NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 1 ) NEW_LINE head = push ( head , 7 ) NEW_LINE print ( countNode ( head ) ) NEW_LINE DEDENT
Check if it is possible to form string B from A under the given constraints | Function that returns true if it is possible to form B from A satisfying the given conditions ; List to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ;
def isPossible ( A , B , b , m ) : NEW_LINE INDENT S = [ ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT S . append ( [ ] ) NEW_LINE DEDENT box = [ 0 ] * len ( A ) NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT S [ ord ( A [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT low = 0 NEW_LINE for i in range ( len ( B ) ) : NEW_LINE INDENT indexes = S [ ord ( B [ i ] ) - ord ( ' a ' ) ] NEW_LINE it = lower_bound ( indexes , low ) NEW_LINE if ( it == len ( indexes ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT count = indexes [ it ] // b NEW_LINE box [ count ] = box [ count ] + 1 NEW_LINE if ( box [ count ] >= m ) : NEW_LINE INDENT count += 1 NEW_LINE low = ( count ) * b NEW_LINE DEDENT else : NEW_LINE INDENT low = indexes [ it ] + 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def lower_bound ( indexes , k ) : NEW_LINE INDENT low , high = 0 , len ( indexes ) - 1 NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( indexes [ mid ] < k ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT if indexes [ low ] < k : NEW_LINE INDENT return ( low + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return low NEW_LINE DEDENT DEDENT / * Driver code * / NEW_LINE A = " abcbbcdefxyz " NEW_LINE B = " acdxyz " NEW_LINE b = 5 NEW_LINE m = 2 NEW_LINE if ( isPossible ( A , B , b , m ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Remove all occurrences of any element for maximum array sum | Python3 program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Driver Code
from sys import maxsize NEW_LINE def maxSumArray ( arr , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE mp = { i : 0 for i in range ( 4 ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE mp [ arr [ i ] ] += 1 NEW_LINE DEDENT minimum = maxsize NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT if ( key == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT minimum = min ( minimum , value * key ) NEW_LINE DEDENT return ( sum1 - minimum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSumArray ( arr , n ) ) NEW_LINE DEDENT
Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | Python3 implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries
MAX = 1001 NEW_LINE prefix = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE ar = [ 0 for i in range ( MAX ) ] NEW_LINE def cal_prefix ( n , arr ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT ar [ i + 1 ] = arr [ i ] NEW_LINE DEDENT for i in range ( 1 , 1001 , 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT prefix [ i ] [ j ] = 0 NEW_LINE DEDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ar [ j ] <= i : NEW_LINE INDENT k = 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = 0 NEW_LINE DEDENT prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + k NEW_LINE DEDENT DEDENT DEDENT def ksub ( l , r , n , k ) : NEW_LINE INDENT lo = 1 NEW_LINE hi = 1000 NEW_LINE while ( lo + 1 < hi ) : NEW_LINE INDENT mid = int ( ( lo + hi ) / 2 ) NEW_LINE if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) : NEW_LINE INDENT hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) : NEW_LINE INDENT hi = lo NEW_LINE DEDENT return hi NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 2 , 3 , 5 , 7 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE cal_prefix ( n , arr ) NEW_LINE queries = [ [ 1 , n , 1 ] , [ 2 , n - 2 , 2 ] , [ 3 , n - 1 , 3 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) ) NEW_LINE DEDENT DEDENT
Find maximum N such that the sum of square of first N natural numbers is not more than X | Python implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate till maxvalue of N ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code
import math NEW_LINE def squareSum ( N ) : NEW_LINE INDENT sum = ( N * ( N + 1 ) * ( 2 * N + 1 ) ) // 6 NEW_LINE return sum NEW_LINE DEDENT def findMaxN ( X ) : NEW_LINE INDENT N = ( int ) ( math . sqrt ( X ) ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( squareSum ( i ) > X ) : NEW_LINE INDENT return i - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT X = 25 NEW_LINE print ( findMaxN ( X ) ) NEW_LINE