codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of mangoes that can be bought | ''Function to check if mid number of mangoes can be bought ; '' Store the coins ; '' If watermelons needed are greater  than given watermelons ; '' Store remaining watermelons if vl  watermelons are used to buy mangoes ; '' Store the value of coins if these  watermelon get sold ; '' Increment coins by ex ; '' Number of mangoes that can be buyed  if only x coins needed for one mango ; '' If the condition is satisfied,  return true ; '' Otherwise return false ; ''Function to find the maximum number of mangoes that can be bought by selling watermelons ; '' Initialize the boundary values ; '' Store the required result ; '' Binary Search ; '' Store the mid value ; '' Check if it is possible to  buy mid number of mangoes ; '' Otherwise, update r to mid -1 ; '' Return the result ; ''Driver Code ; '' Given Input ; '' Function Call
def check ( n , m , x , y , vl ) : NEW_LINE INDENT temp = m NEW_LINE if ( vl > n ) : NEW_LINE INDENT return False NEW_LINE DEDENT ex = n - vl NEW_LINE ex *= y NEW_LINE temp += ex NEW_LINE cr = temp // x NEW_LINE if ( cr >= vl ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def maximizeMangoes ( n , m , x , y ) : NEW_LINE INDENT l = 0 NEW_LINE r = n NEW_LINE ans = 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = l + ( r - l ) // 2 NEW_LINE if ( check ( n , m , x , y , mid ) ) : NEW_LINE INDENT ans = mid NEW_LINE l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT W = 4 NEW_LINE C = 8 NEW_LINE x = 4 NEW_LINE y = 4 NEW_LINE print ( maximizeMangoes ( W , C , x , y ) ) NEW_LINE DEDENT
Kth Smallest Element in a sorted array formed by reversing subarrays from a random index | ''Function to find the Kth element in a sorted and rotated array at random point ; '' Set the boundaries for binary search ; '' Apply binary search to find R ; '' Initialize the middle element ; '' Check in the right side of mid ; '' Else check in the left side ; '' Random point either l or h ; '' Return the kth smallest element ; ''Driver Code ; '' Given Input ; '' Function Call
def findkthElement ( arr , n , K ) : NEW_LINE INDENT l = 0 NEW_LINE h = n - 1 NEW_LINE while l + 1 < h : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if arr [ l ] >= arr [ mid ] : NEW_LINE INDENT l = mid NEW_LINE DEDENT else : NEW_LINE INDENT h = mid NEW_LINE DEDENT DEDENT if arr [ l ] < arr [ h ] : NEW_LINE INDENT r = l NEW_LINE DEDENT else : NEW_LINE INDENT r = h NEW_LINE DEDENT if K <= r + 1 : NEW_LINE INDENT return arr [ r + 1 - K ] NEW_LINE DEDENT else : NEW_LINE INDENT return arr [ n - ( K - ( r + 1 ) ) ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( findkthElement ( arr , n , K ) ) NEW_LINE DEDENT
Find all pairs ( a , b ) in an array such that a % b = k | Function to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver Code
def printPairs ( arr , n , k ) : NEW_LINE INDENT isPairFound = True NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( i != j and arr [ i ] % arr [ j ] == k ) : NEW_LINE INDENT print ( " ( " , arr [ i ] , " , ▁ " , arr [ j ] , " ) " , sep = " " , end = " ▁ " ) NEW_LINE isPairFound = True NEW_LINE DEDENT DEDENT DEDENT return isPairFound NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 4 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE if ( printPairs ( arr , n , k ) == False ) : NEW_LINE INDENT print ( " No ▁ such ▁ pair ▁ exists " ) NEW_LINE DEDENT
Find all pairs ( a , b ) in an array such that a % b = k | Utiltity function to find the divisors of n and store in vector v [ ] ; Vector is used to store the divisors ; If n is a square number , push only one occurrence ; Function to find pairs such that ( a % b = k ) ; Store all the elements in the map to use map as hash for finding elements in O ( 1 ) time . ; Print all the pairs with ( a , b ) as ( k , numbers greater than k ) as k % ( num ( > k ) ) = k i . e . 2 % 4 = 2 ; Now check for the current element as ' a ' how many b exists such that a % b = k ; find all the divisors of ( arr [ i ] - k ) ; Check for each divisor i . e . arr [ i ] % b = k or not , if yes then prthat pair . ; Clear vector ; Driver Code
import math as mt NEW_LINE def findDivisors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 1 , mt . floor ( n ** ( .5 ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i == i ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( i ) NEW_LINE v . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT return v NEW_LINE DEDENT def printPairs ( arr , n , k ) : NEW_LINE INDENT occ = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT occ [ arr [ i ] ] = True NEW_LINE DEDENT isPairFound = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( occ [ k ] and k < arr [ i ] ) : NEW_LINE INDENT print ( " ( " , k , " , " , arr [ i ] , " ) " , end = " ▁ " ) NEW_LINE isPairFound = True NEW_LINE DEDENT if ( arr [ i ] >= k ) : NEW_LINE INDENT v = findDivisors ( arr [ i ] - k ) NEW_LINE for j in range ( len ( v ) ) : NEW_LINE INDENT if ( arr [ i ] % v [ j ] == k and arr [ i ] != v [ j ] and occ [ v [ j ] ] ) : NEW_LINE INDENT print ( " ( " , arr [ i ] , " , " , v [ j ] , " ) " , end = " ▁ " ) NEW_LINE isPairFound = True NEW_LINE DEDENT DEDENT DEDENT DEDENT return isPairFound NEW_LINE DEDENT arr = [ 3 , 1 , 2 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE if ( printPairs ( arr , n , k ) == False ) : NEW_LINE INDENT print ( " No ▁ such ▁ pair ▁ exists " ) NEW_LINE DEDENT
Convert an array to reduced form | Set 1 ( Simple and Hashing ) | Python3 program to convert an array in reduced form ; Create a temp array and copy contents of arr [ ] to temp ; Sort temp array ; create a map ; One by one insert elements of sorted temp [ ] and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver Code
def convert ( arr , n ) : NEW_LINE INDENT temp = [ arr [ i ] for i in range ( n ) ] NEW_LINE temp . sort ( ) NEW_LINE umap = { } NEW_LINE val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT umap [ temp [ i ] ] = val NEW_LINE val += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = umap [ arr [ i ] ] NEW_LINE DEDENT DEDENT def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ Array ▁ is ▁ " ) NEW_LINE printArr ( arr , n ) NEW_LINE convert ( arr , n ) NEW_LINE print ( " Converted Array is   " ) NEW_LINE printArr ( arr , n ) NEW_LINE DEDENT
Return maximum occurring character in an input string | Python program to return the maximum occurring character in the input string ; Create array to keep the count of individual characters Initialize the count array to zero ; Construct character count array from the input string . ; Initialize max count ; Initialize result ; Traversing through the string and maintaining the count of each character ; Driver program to test the above function
ASCII_SIZE = 256 NEW_LINE def getMaxOccuringChar ( str ) : NEW_LINE INDENT count = [ 0 ] * ASCII_SIZE NEW_LINE for i in str : NEW_LINE INDENT count [ ord ( i ) ] += 1 ; NEW_LINE DEDENT max = - 1 NEW_LINE c = ' ' NEW_LINE for i in str : NEW_LINE INDENT if max < count [ ord ( i ) ] : NEW_LINE INDENT max = count [ ord ( i ) ] NEW_LINE c = i NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT str = " sample ▁ string " NEW_LINE print " Max ▁ occurring ▁ character ▁ is ▁ " + getMaxOccuringChar ( str ) NEW_LINE
Check if a Binary Tree ( not BST ) has duplicate values | Helper function that allocates a new node with the given data and None left and right poers . ; If tree is empty , there are no duplicates . ; If current node 's data is already present.  ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def checkDupUtil ( root , s ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if root . data in s : NEW_LINE INDENT return True NEW_LINE DEDENT s . add ( root . data ) NEW_LINE return checkDupUtil ( root . left , s ) or checkDupUtil ( root . right , s ) NEW_LINE DEDENT def checkDup ( root ) : NEW_LINE INDENT s = set ( ) NEW_LINE return checkDupUtil ( root , s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE if ( checkDup ( root ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Expression Tree | An expression tree node ; A utility function to check if ' c ' is an operator ; A utility function to do inorder traversal ; Returns root of constructed tree for given postfix expression ; Traverse through every character of input expression ; if operand , simply push into stack ; Operator ; Pop two top nodes ; make them children ; Add this subexpression to stack ; Only element will be the root of expression tree ; Driver program to test above
class Et : NEW_LINE INDENT def __init__ ( self , value ) : NEW_LINE INDENT self . value = value NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isOperator ( c ) : NEW_LINE INDENT if ( c == ' + ' or c == ' - ' or c == ' * ' or c == ' / ' or c == ' ^ ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def inorder ( t ) : NEW_LINE INDENT if t is not None : NEW_LINE INDENT inorder ( t . left ) NEW_LINE print t . value , NEW_LINE inorder ( t . right ) NEW_LINE DEDENT DEDENT def constructTree ( postfix ) : NEW_LINE INDENT stack = [ ] NEW_LINE for char in postfix : NEW_LINE INDENT if not isOperator ( char ) : NEW_LINE INDENT t = Et ( char ) NEW_LINE stack . append ( t ) NEW_LINE DEDENT else : NEW_LINE INDENT t = Et ( char ) NEW_LINE t1 = stack . pop ( ) NEW_LINE t2 = stack . pop ( ) NEW_LINE t . right = t1 NEW_LINE t . left = t2 NEW_LINE stack . append ( t ) NEW_LINE DEDENT DEDENT t = stack . pop ( ) NEW_LINE return t NEW_LINE DEDENT postfix = " ab + ef * g * - " NEW_LINE r = constructTree ( postfix ) NEW_LINE print " Infix ▁ expression ▁ is " NEW_LINE inorder ( r ) NEW_LINE
Queries to count sum of rows and columns of a Matrix present in given ranges | ''Python3 program for the above approach ; ''Function to preprocess the matrix to execute the queries ; '' Stores the sum of each row ; '' Stores the sum of each col ; '' Traverse the matrix and calculate  sum of each row and column ; '' Insert all row sums in sum_list ; '' Insert all column sums in sum_list ; '' Sort the array in ascending order ; '' Traverse the array queries[][] ; '' Search the leftmost index of L ; '' Search the rightmost index of R ; ''Function to search for the leftmost index of given number ; '' Initialize low, high and ans ; '' Stores mid ; '' If A[mid] >= num ; ''Function to search for the rightmost index of given number ; '' Initialise low, high and ans ; '' Stores mid ; '' If A[mid] <= num ; '' Update ans ; '' Update mid ; '' Update high ; ''Driver Code ; '' Given dimensions of matrix ; '' Given matrix ; '' Given number of queries ; '' Given queries ; '' Function call to count the  number row-sums and column-sums  present in the given ranges
from collections import deque NEW_LINE from bisect import bisect_left , bisect_right NEW_LINE def totalCount ( A , N , M , queries , Q ) : NEW_LINE INDENT row_sum = [ 0 ] * N NEW_LINE col_sum = [ 0 ] * M NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT row_sum [ i ] += A [ i ] [ j ] NEW_LINE col_sum [ j ] += A [ i ] [ j ] NEW_LINE DEDENT DEDENT sum_list = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum_list . append ( row_sum [ i ] ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT sum_list . append ( col_sum [ i ] ) NEW_LINE DEDENT sum_list = sorted ( sum_list ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT L = queries [ i ] [ 0 ] NEW_LINE R = queries [ i ] [ 1 ] NEW_LINE l = left_search ( sum_list , L ) NEW_LINE r = right_search ( sum_list , R ) NEW_LINE print ( r - l + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT def left_search ( A , num ) : NEW_LINE INDENT low , high = 0 , len ( A ) - 1 NEW_LINE ans = 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( A [ mid ] >= num ) : NEW_LINE INDENT ans = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def right_search ( A , num ) : NEW_LINE INDENT low , high = 0 , len ( A ) - 1 NEW_LINE ans = high NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( A [ mid ] <= num ) : NEW_LINE INDENT ans = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , M = 3 , 2 NEW_LINE A = [ [ 13 , 3 ] , [ 9 , 4 ] , [ 6 , 10 ] ] NEW_LINE Q = 2 NEW_LINE queries = [ [ 10 , 20 ] , [ 25 , 35 ] ] NEW_LINE totalCount ( A , N , M , queries , Q ) NEW_LINE DEDENT
Smallest element repeated exactly ‘ k ’ times ( not limited to small range ) | Python program to find the smallest element with frequency exactly k . ; Map is used to store the count of elements present in the array ; Traverse the map and find minimum element with frequency k . ; Driver code
from collections import defaultdict NEW_LINE import sys NEW_LINE def smallestKFreq ( arr , n , k ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT res = sys . maxsize NEW_LINE res1 = sys . maxsize NEW_LINE for key , values in mp . items ( ) : NEW_LINE INDENT if values == k : NEW_LINE INDENT res = min ( res , key ) NEW_LINE DEDENT DEDENT return res if res != res1 else - 1 NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 3 , 1 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( smallestKFreq ( arr , n , k ) ) NEW_LINE
Maximize profit that can be earned by selling an item among N buyers |  ; ''Function to find the maximum profit earned by selling an item among N buyers ; '' Stores the maximum profit ; '' Stores the price of the item ; '' Traverse the array ; '' Count of buyers with  budget >= arr[i] ; '' Increment count ; '' Update the maximum profit ; '' Return the maximum possible  price ; ''Driver code
import sys NEW_LINE def maximumProfit ( arr , n ) : NEW_LINE INDENT ans = - sys . maxsize - 1 NEW_LINE price = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ i ] <= arr [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( ans < count * arr [ i ] ) : NEW_LINE INDENT price = arr [ i ] NEW_LINE ans = count * arr [ i ] NEW_LINE DEDENT DEDENT return price ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] NEW_LINE print ( maximumProfit ( arr , 6 ) ) NEW_LINE DEDENT
Lexicographically smallest permutation of the array possible by at most one swap | ''Function to print the elements of the array arr[] ; '' Traverse the array arr[] ; ''Function to convert given array to lexicographically smallest permutation possible by swapping at most one pair ; '' Stores the index of the first  element which is not at its  correct position ; '' Checks if any such array  element exists or not ; '' Traverse the given array ; '' If element is found at i ; '' If the first array is  not in correct position ; '' Store the index of  the first elements ; '' Store the index of  the first element ; '' Swap the pairs ; '' Print the array ; ''Given array ; ''Store the size of the array
def printt ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def makeLexicographically ( arr , N ) : NEW_LINE INDENT index = 0 NEW_LINE temp = 0 NEW_LINE check = 0 NEW_LINE condition = 0 NEW_LINE element = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( element == arr [ i ] ) : NEW_LINE INDENT check = i NEW_LINE break NEW_LINE DEDENT elif ( arr [ i ] != i + 1 and check == 0 ) : NEW_LINE INDENT index = i NEW_LINE check = 1 NEW_LINE condition = - 1 NEW_LINE element = i + 1 NEW_LINE DEDENT DEDENT if ( condition == - 1 ) : NEW_LINE INDENT temp = arr [ index ] NEW_LINE arr [ index ] = arr [ check ] NEW_LINE arr [ check ] = temp NEW_LINE DEDENT printt ( arr , N ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE makeLexicographically ( arr , N ) NEW_LINE
Check if uppercase characters in a string are used correctly or not | ''Function to check if the character c is in lowercase or not ; ''Function to check if the character c is in uppercase or not ; ''Utility function to check if uppercase characters are used correctly or not ; '' Length of string ; '' If the first character is in lowercase ; '' Otherwise ; '' Check if all characters  are in uppercase or not ; '' If all characters are  in uppercase ; '' Check if all characters except  the first are in lowercase ; ''Function to check if uppercase characters are used correctly or not ; '' Stores whether the use of uppercase  characters are correct or not ; '' If correct ; '' Otherwise ; ''Driver Code ; '' Given string ; '' Function call to check if use of  uppercase characters is correct or not
def isLower ( c ) : NEW_LINE INDENT return ord ( c ) >= ord ( ' a ' ) and ord ( c ) <= ord ( ' z ' ) NEW_LINE DEDENT def isUpper ( c ) : NEW_LINE INDENT return ord ( c ) >= ord ( ' A ' ) and ord ( c ) <= ord ( ' Z ' ) NEW_LINE DEDENT def detectUppercaseUseUtil ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE i = 0 NEW_LINE if ( isLower ( S [ 0 ] ) ) : NEW_LINE INDENT i = 1 NEW_LINE while ( S [ i ] and isLower ( S [ i ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT return True if ( i == N ) else False NEW_LINE DEDENT else : NEW_LINE INDENT i = 1 NEW_LINE while ( S [ i ] and isUpper ( S [ i ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == N ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( i > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT while ( S [ i ] and isLower ( S [ i ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT return True if ( i == N ) else False NEW_LINE DEDENT DEDENT def detectUppercaseUse ( S ) : NEW_LINE INDENT check = detectUppercaseUseUtil ( S ) NEW_LINE if ( check ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " GeeKs " NEW_LINE detectUppercaseUse ( S ) NEW_LINE DEDENT
Queries to count array elements from a given range having a single set bit | ''Check if N has only one set bit ; ''Function to build segment tree ; '' If se is leaf node ; '' Update the node ; '' Stores mid value of segment [ss, se] ; '' Recursive call for left Subtree ; '' Recursive call for right Subtree ; '' Update the Current Node ; ''Function to update a value at Xth index ; '' If ss is equal to X ; '' Update the Xth node ; '' Update the tree ; '' Stores the mid of segment [ss, se] ; '' Update current node ; ''Count of numbers having one set bit ; '' If L > se or R < ss  Invalid Range ; '' If [ss, se] lies  inside the [L, R] ; '' Stores the mid of segment [ss, se] ; '' Return the sum after recursively  traversing left and right subtree ; ''Function to solve queries ; '' Initialise Segment tree ; '' Build segment tree ; '' Perform queries ; ''Input ; ''Function call to solve queries
def check ( N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( ( N & ( N - 1 ) ) == 0 ) NEW_LINE DEDENT def build_seg_tree ( ss , se , si , tree , arr ) : NEW_LINE INDENT if ( ss == se ) : NEW_LINE INDENT tree [ si ] = check ( arr [ ss ] ) NEW_LINE return NEW_LINE DEDENT mid = ( ss + se ) // 2 NEW_LINE build_seg_tree ( ss , mid , 2 * si + 1 , tree , arr ) NEW_LINE build_seg_tree ( mid + 1 , se , 2 * si + 2 , tree , arr ) NEW_LINE tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] NEW_LINE DEDENT def update ( ss , se , si , X , V , tree , arr ) : NEW_LINE INDENT if ( ss == se ) : NEW_LINE INDENT if ( ss == X ) : NEW_LINE INDENT arr [ X ] = V NEW_LINE tree [ si ] = check ( V ) NEW_LINE DEDENT return NEW_LINE DEDENT mid = ( ss + se ) // 2 NEW_LINE if ( X <= mid ) : NEW_LINE INDENT update ( ss , mid , 2 * si + 1 , X , V , tree , arr ) NEW_LINE DEDENT else : NEW_LINE INDENT update ( mid + 1 , se , 2 * si + 2 , X , V , tree , arr ) NEW_LINE DEDENT tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] NEW_LINE DEDENT def query ( l , r , ss , se , si , tree ) : NEW_LINE INDENT if ( r < ss or l > se ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( l <= ss and r >= se ) : NEW_LINE INDENT return tree [ si ] NEW_LINE DEDENT mid = ( ss + se ) // 2 NEW_LINE return ( query ( l , r , ss , mid , 2 * si + 1 , tree ) + query ( l , r , mid + 1 , se , 2 * si + 2 , tree ) ) NEW_LINE DEDENT def Query ( arr , N , Q ) : NEW_LINE INDENT tree = [ 0 ] * ( 4 * N ) NEW_LINE build_seg_tree ( 0 , N - 1 , 0 , tree , arr ) NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT print ( query ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 , N - 1 , 0 , tree ) , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT update ( 0 , N - 1 , 0 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , tree , arr ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 12 , 11 , 16 , 2 , 32 ] NEW_LINE Q = [ [ 1 , 0 , 2 ] , [ 2 , 4 , 24 ] , [ 1 , 1 , 4 ] ] NEW_LINE N = len ( arr ) NEW_LINE Query ( arr , N , Q ) NEW_LINE
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | ''Function to check if sum of first N natural numbers is >= X ; ''Finds minimum value of N such that sum of first N natural number >= X ; '' Binary Search ; ' ' ▁ Checks ▁ if ▁ sum ▁ of ▁ first ▁ ' mid ' natural  numbers is greater than equal to X ; '' Update res ; '' Update high ; '' Update low ; ''Driver Code ; '' Input ; '' Finds minimum value of  N such that sum of first  N natural number >= X
def isGreaterEqual ( N , X ) : NEW_LINE INDENT return ( N * ( N + 1 ) // 2 ) >= X ; NEW_LINE DEDENT def minimumPossible ( X ) : NEW_LINE INDENT low = 1 NEW_LINE high = X NEW_LINE res = - 1 ; NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 ; NEW_LINE if ( isGreaterEqual ( mid , X ) ) : NEW_LINE INDENT res = mid ; NEW_LINE high = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 14 ; NEW_LINE print ( minimumPossible ( X ) ) ; NEW_LINE DEDENT
Find sum of non | Find the sum of all non - repeated elements in an array ; sort all elements of array ; Driver code
def findSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE sum = arr [ 0 ] NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT sum = sum + arr [ i + 1 ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSum ( arr , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT