codeparrot/xlcost-text-to-code huggingface dataset:
Maximum subsequence sum possible by multiplying each element by its index | Initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Print the maximum sum possible ; Given array ; Size of the array ; Function call
dp = [ [ - 1 for x in range ( 1005 ) ] for y in range ( 1005 ) ] NEW_LINE def maximumSumUtil ( a , index , count , n ) : NEW_LINE INDENT if ( index > n or count > n + 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ index ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ count ] NEW_LINE DEDENT ans1 = ( maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ) NEW_LINE ans2 = maximumSumUtil ( a , index + 1 , count , n ) NEW_LINE dp [ index ] [ count ] = max ( ans1 , ans2 ) NEW_LINE return dp [ index ] [ count ] NEW_LINE DEDENT def maximumSum ( arr , N ) : NEW_LINE INDENT print ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) ) NEW_LINE DEDENT arr = [ - 1 , 2 , - 10 , 4 , - 20 ] NEW_LINE N = len ( arr ) NEW_LINE maximumSum ( arr , N ) NEW_LINE
Sort an array according to absolute difference with given value | Function to sort an array according absolute difference with x . ; Store values in a map with the difference with X as key ; Update the values of array ; Function to print the array ; Driver code
def rearrange ( arr , n , x ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = abs ( x - arr [ i ] ) NEW_LINE DEDENT m = { k : v for k , v in sorted ( m . items ( ) , key = lambda item : item [ 1 ] ) } NEW_LINE i = 0 NEW_LINE for it in m . keys ( ) : NEW_LINE INDENT arr [ i ] = it NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def printArray ( 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 , 5 , 3 , 9 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE x = 7 NEW_LINE rearrange ( arr , n , x ) NEW_LINE printArray ( arr , n ) NEW_LINE DEDENT
Minimum Steps to obtain N from 1 by the given operations | Python3 program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Given N ; Function Call
def find_sequence ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 1 , - 1 NEW_LINE DEDENT ans = ( find_sequence ( n - 1 ) [ 0 ] + 1 , n - 1 ) NEW_LINE if n % 2 == 0 : NEW_LINE INDENT div_by_2 = find_sequence ( n // 2 ) NEW_LINE if div_by_2 [ 0 ] < ans [ 0 ] : NEW_LINE INDENT ans = ( div_by_2 [ 0 ] + 1 , n // 2 ) NEW_LINE DEDENT DEDENT if n % 3 == 0 : NEW_LINE INDENT div_by_3 = find_sequence ( n // 3 ) NEW_LINE if div_by_3 [ 0 ] < ans [ 0 ] : NEW_LINE INDENT ans = ( div_by_3 [ 0 ] + 1 , n // 3 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def find_solution ( n ) : NEW_LINE INDENT a , b = find_sequence ( n ) NEW_LINE print ( a ) NEW_LINE sequence = [ ] NEW_LINE sequence . append ( n ) NEW_LINE while b != - 1 : NEW_LINE INDENT sequence . append ( b ) NEW_LINE _ , b = find_sequence ( b ) NEW_LINE DEDENT return sequence [ : : - 1 ] NEW_LINE DEDENT n = 5 NEW_LINE print ( * find_solution ( n ) ) NEW_LINE
Count of vessels completely filled after a given time | Function to find the number of completely filled vessels ; Store the vessels ; Assuming all water is present in the vessel at the first level ; Store the number of vessel that are completely full ; Traverse all the levels ; Number of vessel at each level is j ; Calculate the exceeded amount of water ; If current vessel has less than 1 unit of water then continue ; One more vessel is full ; If left bottom vessel present ; If right bottom vessel present ; Number of levels ; Number of seconds ; Function call
def FindNoOfFullVessels ( n , t ) : NEW_LINE INDENT Matrix = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE Matrix [ 0 ] [ 0 ] = t * 1.0 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT exceededwater = Matrix [ i ] [ j ] - 1.0 NEW_LINE if ( exceededwater < 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans += 1 NEW_LINE if ( i + 1 < n ) : NEW_LINE INDENT Matrix [ i + 1 ] [ j ] += exceededwater / 2 NEW_LINE DEDENT if ( i + 1 < n and j + 1 < n ) : NEW_LINE INDENT Matrix [ i + 1 ] [ j + 1 ] += exceededwater / 2 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT N = 3 NEW_LINE T = 4 NEW_LINE print ( FindNoOfFullVessels ( N , T ) ) NEW_LINE
Sort an array in wave form | Python function to sort the array arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ] ; sort the array ; Swap adjacent elements ; Driver program
def sortInWave ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( 0 , n - 1 , 2 ) : NEW_LINE INDENT arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ] NEW_LINE DEDENT DEDENT arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ] NEW_LINE sortInWave ( arr , len ( arr ) ) NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT print arr [ i ] , NEW_LINE DEDENT
Count of maximum occurring subsequence using only those characters whose indices are in GP | Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Initialize 1 - D array and 2 - D dp array to 0 ; Iterate till the length of the given string ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Given string s ; Function call
def findMaxTimes ( S ) : NEW_LINE INDENT arr = [ 0 ] * 26 NEW_LINE dp = [ [ 0 for x in range ( 26 ) ] for y in range ( 26 ) ] NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT now = ord ( S [ i ] ) - ord ( ' a ' ) NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT dp [ j ] [ now ] += arr [ j ] NEW_LINE DEDENT arr [ now ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] ) NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT S = " ddee " NEW_LINE print ( findMaxTimes ( S ) ) NEW_LINE
Sort an array in wave form | Python function to sort the array arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ] ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; Driver program
def sortInWave ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n , 2 ) : NEW_LINE INDENT if ( i > 0 and arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ] NEW_LINE DEDENT if ( i < n - 1 and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ] NEW_LINE DEDENT DEDENT DEDENT arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ] NEW_LINE sortInWave ( arr , len ( arr ) ) NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT print arr [ i ] , NEW_LINE DEDENT
Remove all nodes which don 't lie in any path with sum>= k | binary tree node contains data field , left and right pointer ; inorder traversal ; Function to remove all nodes which do not lie in th sum path ; Base case ; Recur for left and right subtree ; if node is leaf and sum is found greater than data than remove node An important thing to remember is that a non - leaf node can become a leaf when its children are removed ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( root . left ) NEW_LINE print ( root . data , " " , end = " " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT def prune ( root , sum ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = prune ( root . left , sum - root . data ) NEW_LINE root . right = prune ( root . right , sum - root . data ) NEW_LINE if root . left is None and root . right is None : NEW_LINE INDENT if sum > root . data : NEW_LINE INDENT return None NEW_LINE DEDENT DEDENT return root NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 8 ) NEW_LINE root . left . left . right = Node ( 9 ) NEW_LINE root . left . right . left = Node ( 12 ) NEW_LINE root . right . right . left = Node ( 10 ) NEW_LINE root . right . right . left . right = Node ( 11 ) NEW_LINE root . left . left . right . left = Node ( 13 ) NEW_LINE root . left . left . right . right = Node ( 14 ) NEW_LINE root . left . left . right . right . left = Node ( 15 ) NEW_LINE print ( " Tree ▁ before ▁ truncation " ) NEW_LINE inorder ( root ) NEW_LINE prune ( root , 45 ) NEW_LINE print ( " Tree after truncation " ) NEW_LINE inorder ( root ) NEW_LINE
Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | Function to count the unique sum ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Given a number N ; Function call
def count_unique_sum ( n ) : NEW_LINE INDENT ar = [ 0 ] * ( n + 1 ) NEW_LINE fsum = [ 0 ] * ( n + 1 ) NEW_LINE rsum = [ 0 ] * ( n + 1 ) NEW_LINE ans = 1 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT ar [ i ] = i NEW_LINE DEDENT fsum [ 0 ] = ar [ 0 ] NEW_LINE rsum [ n ] = ar [ n ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fsum [ i ] = ar [ i ] + fsum [ i - 1 ] NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT rsum [ i ] = ar [ i ] + rsum [ i + 1 ] NEW_LINE DEDENT for k in range ( 2 , n + 1 ) : NEW_LINE INDENT ans += ( 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 4 NEW_LINE print ( count_unique_sum ( N ) ) NEW_LINE
Merge an array of size n into another array of size m + n | Python program to Merge an array of size n into another array of size m + n ; Function to move m elements at the end of array mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
NA = - 1 NEW_LINE def moveToEnd ( mPlusN , size ) : NEW_LINE INDENT i = 0 NEW_LINE j = size - 1 NEW_LINE for i in range ( size - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( mPlusN [ i ] != NA ) : NEW_LINE INDENT mPlusN [ j ] = mPlusN [ i ] NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT DEDENT def merge ( mPlusN , N , m , n ) : NEW_LINE INDENT i = n NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE while ( k < ( m + n ) ) : NEW_LINE INDENT if ( ( j == n ) or ( i < ( m + n ) and mPlusN [ i ] <= N [ j ] ) ) : NEW_LINE INDENT mPlusN [ k ] = mPlusN [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mPlusN [ k ] = N [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT mPlusN = [ 2 , 8 , NA , NA , NA , 13 , NA , 15 , 20 ] NEW_LINE N = [ 5 , 7 , 9 , 25 ] NEW_LINE n = len ( N ) NEW_LINE m = len ( mPlusN ) - n NEW_LINE moveToEnd ( mPlusN , m + n ) NEW_LINE merge ( mPlusN , N , m , n ) NEW_LINE printArray ( mPlusN , m + n ) NEW_LINE
Count of N | Function to return the count of such numbers ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ j ] stores the number of such i - digit numbers ending with j ; Stores the results of length i ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual count of i - digit numbers ending with j ; Update dp [ ] ; Stores the final answer ; Return the final answer ; Driver code
def getCount ( n , K ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 10 NEW_LINE DEDENT dp = [ 0 ] * 11 NEW_LINE next = [ 0 ] * 11 NEW_LINE for i in range ( 1 , 9 + 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 9 + 1 ) : NEW_LINE INDENT l = max ( 0 , j - k ) NEW_LINE r = min ( 9 , j + k ) NEW_LINE next [ l ] += dp [ j ] NEW_LINE next [ r + 1 ] -= dp [ j ] NEW_LINE DEDENT for j in range ( 1 , 9 + 1 ) : NEW_LINE INDENT next [ j ] += next [ j - 1 ] NEW_LINE DEDENT for j in range ( 10 ) : NEW_LINE INDENT dp [ j ] = next [ j ] NEW_LINE next [ j ] = 0 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( 9 + 1 ) : NEW_LINE INDENT count += dp [ i ] NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE k = 1 NEW_LINE print ( getCount ( n , k ) ) NEW_LINE DEDENT
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver program to test sortedAfterSwap ( )
def sortedAfterSwap ( A , B , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( B [ i ] == 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( B [ j ] == 1 ) : NEW_LINE INDENT j = j + 1 NEW_LINE DEDENT A = A [ 0 : i ] + sorted ( A [ i : j + 1 ] ) + A [ j + 1 : ] NEW_LINE i = j NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( A [ i ] != i + 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT A = [ 1 , 2 , 5 , 3 , 4 , 6 ] NEW_LINE B = [ 0 , 1 , 1 , 1 , 0 ] NEW_LINE n = len ( A ) NEW_LINE if ( sortedAfterSwap ( A , B , n ) ) : NEW_LINE INDENT print ( " A ▁ can ▁ be ▁ sorted " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " A ▁ can ▁ not ▁ be ▁ sorted " ) NEW_LINE DEDENT
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver program
def sortedAfterSwap ( A , B , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if B [ i ] : NEW_LINE INDENT if A [ i ] != i + 1 : NEW_LINE INDENT A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if A [ i ] != i + 1 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 5 , 3 , 4 , 6 ] NEW_LINE B = [ 0 , 1 , 1 , 1 , 0 ] NEW_LINE n = len ( A ) NEW_LINE if ( sortedAfterSwap ( A , B , n ) ) : NEW_LINE INDENT print ( " A ▁ can ▁ be ▁ sorted " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " A ▁ can ▁ not ▁ be ▁ sorted " ) NEW_LINE DEDENT DEDENT
Sort an array containing two types of elements | Method for segregation 0 and 1 given input array ; Driver Code
def segregate0and1 ( arr , n ) : NEW_LINE INDENT type0 = 0 ; type1 = n - 1 NEW_LINE while ( type0 < type1 ) : NEW_LINE INDENT if ( arr [ type0 ] == 1 ) : NEW_LINE INDENT arr [ type0 ] , arr [ type1 ] = arr [ type1 ] , arr [ type0 ] NEW_LINE type1 -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT type0 += 1 NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE segregate0and1 ( arr , n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Pentanacci Numbers | Recursive program to find the Nth Pentanacci number ; Function to print the Nth Pentanacci number ; Driver code
def printpentaRec ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 or \ n == 2 or n == 3 or n == 4 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( n == 5 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( printpentaRec ( n - 1 ) + printpentaRec ( n - 2 ) + printpentaRec ( n - 3 ) + printpentaRec ( n - 4 ) + printpentaRec ( n - 5 ) ) NEW_LINE DEDENT DEDENT def printPenta ( n ) : NEW_LINE INDENT print ( printpentaRec ( n ) ) NEW_LINE DEDENT n = 10 NEW_LINE printPenta ( n ) NEW_LINE
Count Inversions in an array | Set 1 ( Using Merge Sort ) | Python3 program to count inversions in an array ; Driver Code
def getInvCount ( arr , n ) : NEW_LINE INDENT inv_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT inv_count += 1 NEW_LINE DEDENT DEDENT DEDENT return inv_count NEW_LINE DEDENT arr = [ 1 , 20 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Number ▁ of ▁ inversions ▁ are " , getInvCount ( arr , n ) ) NEW_LINE
Find if there is a path between two vertices in an undirected graph | Python3 program to check if there is exist a path between two vertices of an undirected graph . ; A BFS based function to check whether d is reachable from s . ; Base case ; Mark all the vertices as not visited ; Create a queue for BFS ; Mark the current node as visited and enqueue it ; Dequeue a vertex from queue and prit ; Get all adjacent vertices of the dequeued vertex s If a adjacent has not been visited , then mark it visited and enqueue it ; If this adjacent node is the destination node , then return true ; Else , continue to do BFS ; If BFS is complete without visiting d ; Driver program to test methods of graph class ; Create a graph given in the above diagram
from collections import deque NEW_LINE def addEdge ( v , w ) : NEW_LINE INDENT global adj NEW_LINE adj [ v ] . append ( w ) NEW_LINE adj [ w ] . append ( v ) NEW_LINE DEDENT def isReachable ( s , d ) : NEW_LINE INDENT if ( s == d ) : NEW_LINE INDENT return True NEW_LINE DEDENT visited = [ False for i in range ( V ) ] NEW_LINE queue = deque ( ) NEW_LINE visited [ s ] = True NEW_LINE queue . append ( s ) NEW_LINE while ( len ( queue ) > 0 ) : NEW_LINE INDENT s = queue . popleft ( ) NEW_LINE for i in adj [ s ] : NEW_LINE INDENT if ( i == d ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( not visited [ i ] ) : NEW_LINE INDENT visited [ i ] = True NEW_LINE queue . append ( i ) NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 4 NEW_LINE adj = [ [ ] for i in range ( V + 1 ) ] NEW_LINE addEdge ( 0 , 1 ) NEW_LINE addEdge ( 0 , 2 ) NEW_LINE addEdge ( 1 , 2 ) NEW_LINE addEdge ( 2 , 0 ) NEW_LINE addEdge ( 2 , 3 ) NEW_LINE addEdge ( 3 , 3 ) NEW_LINE u , v = 1 , 3 NEW_LINE if ( isReachable ( u , v ) ) : NEW_LINE INDENT print ( " There ▁ is ▁ a ▁ path ▁ from " , u , " to " , v ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ path ▁ from " , u , " to " , v ) NEW_LINE DEDENT DEDENT
Range queries for alternatively addition and subtraction on given Array | Function to find the result of alternatively adding and subtracting elements in the range [ L , R ] ; A boolean variable flag to alternatively add and subtract ; Iterate from [ L , R ] ; If flag is False , then add & toggle the flag ; If flag is True subtract and toggle the flag ; Return the final result ; Function to find the value for each query ; Iterate for each query ; Driver Code ; Given array ; Given Queries ; Function Call
def findResultUtil ( arr , L , R ) : NEW_LINE INDENT result = 0 NEW_LINE flag = False NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( flag == False ) : NEW_LINE INDENT result = result + arr [ i ] NEW_LINE flag = True NEW_LINE DEDENT else : NEW_LINE INDENT result = result - arr [ i ] NEW_LINE flag = False NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT def findResult ( arr , n , q , m ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT print ( findResultUtil ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 ] NEW_LINE n = len ( arr ) NEW_LINE q = [ [ 2 , 5 ] , [ 6 , 8 ] , [ 1 , 7 ] , [ 4 , 8 ] , [ 0 , 5 ] ] NEW_LINE m = len ( q ) NEW_LINE findResult ( arr , n , q , m ) NEW_LINE DEDENT
Two elements whose sum is closest to zero | Python3 code to find Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; Driver program to test above function
def minAbsSumPair ( arr , arr_size ) : NEW_LINE INDENT inv_count = 0 NEW_LINE if arr_size < 2 : NEW_LINE INDENT print ( " Invalid ▁ Input " ) NEW_LINE return NEW_LINE DEDENT min_l = 0 NEW_LINE min_r = 1 NEW_LINE min_sum = arr [ 0 ] + arr [ 1 ] NEW_LINE for l in range ( 0 , arr_size - 1 ) : NEW_LINE INDENT for r in range ( l + 1 , arr_size ) : NEW_LINE INDENT sum = arr [ l ] + arr [ r ] NEW_LINE if abs ( min_sum ) > abs ( sum ) : NEW_LINE INDENT min_sum = sum NEW_LINE min_l = l NEW_LINE min_r = r NEW_LINE DEDENT DEDENT DEDENT print ( " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are " , arr [ min_l ] , " and ▁ " , arr [ min_r ] ) NEW_LINE DEDENT arr = [ 1 , 60 , - 10 , 70 , - 80 , 85 ] NEW_LINE minAbsSumPair ( arr , 6 ) ; NEW_LINE
Maximize sum of topmost elements of S stacks by popping at most N elements | Python3 program to maximize the sum of top of the stack values of S stacks by popping at most N element ; Function for computing the maximum sum at the top of the stacks after popping at most N elements from S stack ; Constructing a dp matrix of dimensions ( S + 1 ) x ( N + 1 ) ; Loop over all i stacks ; Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined ; Store the maximum sum of popping N elements across all stacks ; dp [ S ] [ N ] has the maximum sum ; Driver code ; Number of stacks ; Length of each stack ; Maximum elements that can be popped
import sys NEW_LINE def maximumSum ( S , M , N , stacks ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( S + 1 ) ] NEW_LINE for i in range ( S ) : NEW_LINE INDENT for j in range ( N + 1 ) : NEW_LINE INDENT for k in range ( min ( j , M ) + 1 ) : NEW_LINE INDENT dp [ i + 1 ] [ j ] = max ( dp [ i + 1 ] [ j ] , stacks [ i ] [ k ] + dp [ i ] [ j - k ] ) NEW_LINE DEDENT DEDENT DEDENT result = - sys . maxsize - 1 NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT result = max ( result , dp [ S ] [ i ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = 2 NEW_LINE M = 4 NEW_LINE stacks = [ [ 2 , 6 , 4 , 5 ] , [ 1 , 6 , 15 , 10 ] ] NEW_LINE N = 3 NEW_LINE print ( maximumSum ( S , M , N , stacks ) ) NEW_LINE DEDENT