codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Example input = [ 0 , 1 , 2 , 2 , 0 , 0 ] output = [ 0 , 0 , 0 , 1 , 2 , 2 ]
inputArray = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] NEW_LINE outputArray = [ ] NEW_LINE indexOfOne = 0 NEW_LINE for item in inputArray : NEW_LINE INDENT if item == 2 : NEW_LINE INDENT outputArray . append ( item ) NEW_LINE DEDENT elif item == 1 : NEW_LINE INDENT outputArray . insert ( indexOfOne , item ) NEW_LINE indexOfOne += 1 NEW_LINE DEDENT elif item == 0 : NEW_LINE INDENT outputArray . insert ( 0 , item ) NEW_LINE indexOfOne += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ wrong ▁ value ▁ - ▁ Aborting ▁ " ) NEW_LINE continue NEW_LINE DEDENT DEDENT print ( outputArray ) NEW_LINE
Number of visible boxes after putting one inside another | Python3 program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; driver Program
import collections NEW_LINE def minimumBox ( arr , n ) : NEW_LINE INDENT q = collections . deque ( [ ] ) NEW_LINE arr . sort ( ) NEW_LINE q . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT now = q [ 0 ] NEW_LINE if ( arr [ i ] >= 2 * now ) : NEW_LINE INDENT q . popleft ( ) NEW_LINE DEDENT q . append ( arr [ i ] ) NEW_LINE DEDENT return len ( q ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumBox ( arr , n ) ) NEW_LINE DEDENT
Sort a binary array using one traversal | A Python program to sort a binary array ; if number is smaller than 1 then swap it with j - th number ; Driver program
def sortBinaryArray ( a , n ) : NEW_LINE INDENT j = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] < 1 : NEW_LINE INDENT j = j + 1 NEW_LINE a [ i ] , a [ j ] = a [ j ] , a [ i ] NEW_LINE DEDENT DEDENT DEDENT a = [ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ] NEW_LINE n = len ( a ) NEW_LINE sortBinaryArray ( a , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
def findDuplicate ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT j , count = i + 1 , 1 NEW_LINE while ( j < n and arr [ j ] == arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( count == k ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT i = j NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 3 , 1 ] ; NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print findDuplicate ( arr , n , k ) NEW_LINE
Longest Common Prefix using Sorting | Python 3 program to find longest common prefix of given array of words . ; if size is 0 , return empty string ; sort the array of strings ; find the minimum length from first and last string ; find the common prefix between the first and last string ; Driver Code
def longestCommonPrefix ( a ) : NEW_LINE INDENT size = len ( a ) NEW_LINE if ( size == 0 ) : NEW_LINE INDENT return " " NEW_LINE DEDENT if ( size == 1 ) : NEW_LINE INDENT return a [ 0 ] NEW_LINE DEDENT a . sort ( ) NEW_LINE end = min ( len ( a [ 0 ] ) , len ( a [ size - 1 ] ) ) NEW_LINE i = 0 NEW_LINE while ( i < end and a [ 0 ] [ i ] == a [ size - 1 ] [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT pre = a [ 0 ] [ 0 : i ] NEW_LINE return pre NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE print ( " The ▁ longest ▁ Common ▁ Prefix ▁ is ▁ : " , longestCommonPrefix ( input ) ) NEW_LINE DEDENT
Find the minimum and maximum amount to buy all N candies | Python implementation to find the minimum and maximum amount ; function to find the maximum and the minimum cost required ; Sort the array ; print the minimum cost ; print the maximum cost ; Driver Code ; Function call
from math import ceil NEW_LINE def find ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE b = int ( ceil ( n / k ) ) NEW_LINE print ( " minimum ▁ " , sum ( arr [ : b ] ) ) NEW_LINE print ( " maximum ▁ " , sum ( arr [ - b : ] ) ) NEW_LINE DEDENT arr = [ 3 , 2 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE find ( arr , n , k ) NEW_LINE
Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false / ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver code
def checkForSorting ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i + 1 ] == 1 ) : NEW_LINE INDENT arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 0 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkForSorting ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sort an array of strings according to string lengths | Function to print the sorted array of string ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Driver code ; Function to perform sorting ; Calling the function to print result
def printArraystring ( string , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( string [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def sort ( s , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT temp = s [ i ] NEW_LINE j = i - 1 NEW_LINE while j >= 0 and len ( temp ) < len ( s [ j ] ) : NEW_LINE INDENT s [ j + 1 ] = s [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT s [ j + 1 ] = temp NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " GeeksforGeeks " , " I " , " from " , " am " ] NEW_LINE n = len ( arr ) NEW_LINE sort ( arr , n ) NEW_LINE printArraystring ( arr , n ) NEW_LINE DEDENT
Hoare 's vs Lomuto partition scheme in QuickSort | This function takes first element as pivot , and places all the elements smaller than the pivot on the left side and all the elements greater than the pivot on the right side . It returns the index of the last element on the smaller side ; Find leftmost element greater than or equal to pivot ; Find rightmost element smaller than or equal to pivot ; If two pointers met . ; The main function that implements QuickSort arr -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to pran array ; Driver code
def partition ( arr , low , high ) : NEW_LINE INDENT pivot = arr [ low ] NEW_LINE i = low - 1 NEW_LINE j = high + 1 NEW_LINE while ( True ) : NEW_LINE INDENT i += 1 NEW_LINE while ( arr [ i ] < pivot ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT j -= 1 NEW_LINE while ( arr [ j ] > pivot ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT if ( i >= j ) : NEW_LINE INDENT return j NEW_LINE DEDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE DEDENT DEDENT def quickSort ( arr , low , high ) : NEW_LINE INDENT if ( low < high ) : NEW_LINE INDENT pi = partition ( arr , low , high ) NEW_LINE quickSort ( arr , low , pi ) NEW_LINE quickSort ( arr , pi + 1 , high ) 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 print ( ) NEW_LINE DEDENT arr = [ 10 , 7 , 8 , 9 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE quickSort ( arr , 0 , n - 1 ) NEW_LINE print ( " Sorted ▁ array : " ) NEW_LINE printArray ( arr , n ) NEW_LINE
K | Return the K - th smallest element . ; sort ( arr , arr + n ) ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest element  after removing array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code
def ksmallest ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE if ( k < arr [ 0 ] ) : NEW_LINE INDENT return k ; NEW_LINE DEDENT if ( k == arr [ 0 ] ) : NEW_LINE INDENT return arr [ 0 ] + 1 ; NEW_LINE DEDENT if ( k > arr [ n - 1 ] ) : NEW_LINE INDENT return k + n ; NEW_LINE DEDENT if ( arr [ 0 ] == 1 ) : NEW_LINE INDENT k -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT k -= ( arr [ 0 ] - 1 ) ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT c = arr [ i ] - arr [ i - 1 ] - 1 ; NEW_LINE if ( k <= c ) : NEW_LINE INDENT return arr [ i - 1 ] + k ; NEW_LINE DEDENT else : NEW_LINE INDENT k -= c ; NEW_LINE DEDENT DEDENT return arr [ n - 1 ] + k ; NEW_LINE DEDENT k = 1 ; NEW_LINE arr = [ 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( ksmallest ( arr , n , k ) ) ; NEW_LINE
Count nodes within K | Python3 program to count nodes inside K distance range from marked nodes ; Utility bfs method to fill distance vector and returns most distant marked node from node u ; push node u in queue and initialize its distance as 0 ; loop untill all nodes are processed ; if node is marked , update lastMarked variable ; loop over all neighbors of u and update their distance before pushing in queue ; if not given value already ; return last updated marked value ; method returns count of nodes which are in K - distance range from marked nodes ; vertices in a tree are one more than number of edges ; fill vector for graph ; fill boolean array mark from marked array ; vectors to store distances ; first bfs ( from any random node ) to get one distant marked node ; second bfs to get other distant marked node and also dl is filled with distances from first chosen marked node ; third bfs to fill dr by distances from second chosen marked node ; loop over all nodes ; increase res by 1 , if current node has distance less than K from both extreme nodes ; Driver Code
import queue NEW_LINE def bfsWithDistance ( g , mark , u , dis ) : NEW_LINE INDENT lastMarked = 0 NEW_LINE q = queue . Queue ( ) NEW_LINE q . put ( u ) NEW_LINE dis [ u ] = 0 NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT u = q . get ( ) NEW_LINE if ( mark [ u ] ) : NEW_LINE INDENT lastMarked = u NEW_LINE DEDENT for i in range ( len ( g [ u ] ) ) : NEW_LINE INDENT v = g [ u ] [ i ] NEW_LINE if ( dis [ v ] == - 1 ) : NEW_LINE INDENT dis [ v ] = dis [ u ] + 1 NEW_LINE q . put ( v ) NEW_LINE DEDENT DEDENT DEDENT return lastMarked NEW_LINE DEDENT def nodesKDistanceFromMarked ( edges , V , marked , N , K ) : NEW_LINE INDENT V = V + 1 NEW_LINE g = [ [ ] for i in range ( V ) ] NEW_LINE u , v = 0 , 0 NEW_LINE for i in range ( V - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE g [ u ] . append ( v ) NEW_LINE g [ v ] . append ( u ) NEW_LINE DEDENT mark = [ False ] * V NEW_LINE for i in range ( N ) : NEW_LINE INDENT mark [ marked [ i ] ] = True NEW_LINE DEDENT tmp = [ - 1 ] * V NEW_LINE dl = [ - 1 ] * V NEW_LINE dr = [ - 1 ] * V NEW_LINE u = bfsWithDistance ( g , mark , 0 , tmp ) NEW_LINE u = bfsWithDistance ( g , mark , u , dl ) NEW_LINE bfsWithDistance ( g , mark , u , dr ) NEW_LINE res = 0 NEW_LINE for i in range ( V ) : NEW_LINE INDENT if ( dl [ i ] <= K and dr [ i ] <= K ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 1 , 0 ] , [ 0 , 3 ] , [ 0 , 8 ] , [ 2 , 3 ] , [ 3 , 5 ] , [ 3 , 6 ] , [ 3 , 7 ] , [ 4 , 5 ] , [ 5 , 9 ] ] NEW_LINE V = len ( edges ) NEW_LINE marked = [ 1 , 2 , 4 ] NEW_LINE N = len ( marked ) NEW_LINE K = 3 NEW_LINE print ( nodesKDistanceFromMarked ( edges , V , marked , N , K ) ) NEW_LINE DEDENT
Check whether a given number is even or odd | Returns true if n is even , else odd ; n & 1 is 1 , then odd , else even ; Driver code
def isEven ( n ) : NEW_LINE INDENT return ( not ( n & 1 ) ) NEW_LINE DEDENT n = 101 ; NEW_LINE print ( " Even " if isEven ( n ) else " Odd " ) NEW_LINE
Count distinct occurrences as a subsequence | Python3 program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . x An empty string can 't have another  string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match,  then value is same as the value  without last character in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; Driver Code
def findSubsequenceCount ( S , T ) : NEW_LINE INDENT m = len ( T ) NEW_LINE n = len ( S ) NEW_LINE if m > n : NEW_LINE INDENT return 0 NEW_LINE DEDENT mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT mat [ i ] [ 0 ] = 0 NEW_LINE DEDENT for j in range ( n + 1 ) : NEW_LINE INDENT mat [ 0 ] [ j ] = 1 NEW_LINE DEDENT for i in range ( 1 , m + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if T [ i - 1 ] != S [ j - 1 ] : NEW_LINE INDENT mat [ i ] [ j ] = mat [ i ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] [ j ] = ( mat [ i ] [ j - 1 ] + mat [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return mat [ m ] [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT T = " ge " NEW_LINE S = " geeksforgeeks " NEW_LINE print ( findSubsequenceCount ( S , T ) ) NEW_LINE DEDENT
QuickSort Tail Call Optimization ( Reducing worst case space to Log n ) | Python3 program for the above approach ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition
def quickSort ( arr , low , high ) : NEW_LINE INDENT if ( low < high ) : NEW_LINE INDENT pi = partition ( arr , low , high ) NEW_LINE quickSort ( arr , low , pi - 1 ) NEW_LINE quickSort ( arr , pi + 1 , high ) NEW_LINE DEDENT DEDENT
Check if a Regular Bracket Sequence can be formed with concatenation of given strings | Function to check possible RBS from the given strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code
def checkRBS ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE v = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT s = S [ i ] NEW_LINE sum = 0 NEW_LINE pre = 0 NEW_LINE for c in s : NEW_LINE INDENT if ( c == ' ( ' ) : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum -= 1 NEW_LINE DEDENT pre = min ( sum , pre ) NEW_LINE DEDENT v [ i ] = [ sum , pre ] NEW_LINE DEDENT pos = [ ] NEW_LINE neg = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( v [ i ] [ 0 ] >= 0 ) : NEW_LINE INDENT pos . append ( [ - v [ i ] [ 1 ] , v [ i ] [ 0 ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT neg . append ( [ v [ i ] [ 0 ] - v [ i ] [ 1 ] , - v [ i ] [ 0 ] ] ) NEW_LINE DEDENT DEDENT pos . sort ( ) NEW_LINE open = 0 NEW_LINE for p in pos : NEW_LINE INDENT if ( open - p [ 0 ] >= 0 ) : NEW_LINE INDENT open += p [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT neg . sort ( ) NEW_LINE negative = 0 NEW_LINE for p in neg : NEW_LINE INDENT if ( negative - p [ 0 ] >= 0 ) : NEW_LINE INDENT negative += p [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT if ( open != negative ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return 0 NEW_LINE DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : arr = [ " ) " , " ( ) ( " ] NEW_LINE INDENT checkRBS ( arr ) NEW_LINE DEDENT
Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | Python program for the above approach ; Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
import math NEW_LINE def maximumUniqueSquares ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE if N < 4 : NEW_LINE INDENT return 0 NEW_LINE DEDENT len = int ( math . sqrt ( N ) ) NEW_LINE N -= len * len NEW_LINE for i in range ( 1 , len ) : NEW_LINE INDENT ans += i * i NEW_LINE DEDENT if ( N >= len ) : NEW_LINE INDENT N -= len NEW_LINE for i in range ( 1 , len ) : NEW_LINE INDENT ans += i NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT ans += i NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 NEW_LINE print ( maximumUniqueSquares ( N ) ) NEW_LINE DEDENT
Lexicographically smallest permutation number up to K having given array as a subsequence | Function to find the lexicographically smallest permutation such that the given array is a subsequence of it ; Stores the missing elements in arr in the range [ 1 , K ] ; Stores if the ith element is present in arr or not ; Loop to mark all integers present in the array as visited ; Loop to insert all the integers not visited into missing ; Append INT_MAX at end in order to prevent going out of bounds ; Pointer to the current element ; Pointer to the missing element ; Stores the required permutation ; Loop to construct the permutation using greedy approach ; If missing element is smaller that the current element insert missing element ; Insert current element ; Print the required Permutation ; Driver Code ; Function Call
def findPermutation ( K , arr ) : NEW_LINE INDENT missing = [ ] NEW_LINE visited = [ 0 ] * ( K + 1 ) NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT visited [ arr [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT missing . append ( i ) NEW_LINE DEDENT DEDENT INT_MAX = 2147483647 NEW_LINE arr . append ( INT_MAX ) NEW_LINE missing . append ( INT_MAX ) NEW_LINE p1 = 0 NEW_LINE p2 = 0 NEW_LINE ans = [ ] NEW_LINE while ( len ( ans ) < K ) : NEW_LINE INDENT if ( arr [ p1 ] < missing [ p2 ] ) : NEW_LINE INDENT ans . append ( arr [ p1 ] ) NEW_LINE p1 = p1 + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( missing [ p2 ] ) NEW_LINE p2 = p2 + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , K ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 7 NEW_LINE arr = [ 6 , 4 , 2 , 1 ] NEW_LINE findPermutation ( K , arr ) NEW_LINE DEDENT
Last element remaining after repeated removal of Array elements at perfect square indices | Python 3 program for the above approach ; Function to find last remaining index after repeated removal of perfect square indices ; Initialize the ans variable as N ; Iterate a while loop and discard the possible values ; Total discarded values ; Check if this forms a perfect square ; Decrease answer by 1 ; Subtract the value from the current value of N ; Return the value remained ; Function to find last remaining element after repeated removal of array element at perfect square indices ; Find the remaining index ; Print the element at that index as the result ; Driver Code ; Given input
from math import sqrt NEW_LINE def findRemainingIndex ( N ) : NEW_LINE INDENT ans = N NEW_LINE while ( N > 1 ) : NEW_LINE INDENT discard = int ( sqrt ( N ) ) NEW_LINE if ( discard * discard == N ) : NEW_LINE INDENT ans -= 1 NEW_LINE DEDENT N -= discard NEW_LINE DEDENT return ans NEW_LINE DEDENT def findRemainingElement ( arr , N ) : NEW_LINE INDENT remainingIndex = findRemainingIndex ( N ) NEW_LINE print ( arr [ remainingIndex - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 4 , 2 , 4 , - 3 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE findRemainingElement ( arr , N ) NEW_LINE DEDENT
Minimum time to reach from Node 1 to N if travel is allowed only when node is Green | Import library for Queue ; Function to find min edge ; Initialize queue and push [ 1 , 0 ] ; Create visited array to keep the track if node is visited or not ; Run infinite loop ; If node is N , terminate loop ; Travel adjacent nodes of crnt ; Check whether we visited previously or not ; Push into queue and make as true ; Function to Find time required to reach 1 to N ; Check color of node is red or green ; Add C sec from next green color time ; Add C ; Return the answer ; Driver Code ; Given Input ; Function Call ; Print total time
from queue import Queue NEW_LINE def minEdge ( ) : NEW_LINE INDENT Q = Queue ( ) NEW_LINE Q . put ( [ 1 , 0 ] ) NEW_LINE V = [ False for _ in range ( N + 1 ) ] NEW_LINE while 1 : NEW_LINE INDENT crnt , c = Q . get ( ) NEW_LINE if crnt == N : NEW_LINE INDENT break NEW_LINE DEDENT for _ in X [ crnt ] : NEW_LINE INDENT if not V [ _ ] : NEW_LINE INDENT Q . put ( [ _ , c + 1 ] ) NEW_LINE V [ _ ] = True NEW_LINE DEDENT DEDENT DEDENT return c NEW_LINE DEDENT def findTime ( c ) : NEW_LINE INDENT ans = 0 NEW_LINE for _ in range ( c ) : NEW_LINE INDENT if ( ans // T ) % 2 : NEW_LINE INDENT ans = ( ans // T + 1 ) * T + C NEW_LINE DEDENT else : NEW_LINE INDENT ans += C NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE M = 5 NEW_LINE T = 3 NEW_LINE C = 5 NEW_LINE X = [ [ ] , [ 2 , 3 , 4 ] , [ 4 , 5 ] , [ 1 ] , [ 1 , 2 ] , [ 2 ] ] NEW_LINE c = minEdge ( ) NEW_LINE ans = findTime ( c ) NEW_LINE print ( ans ) NEW_LINE DEDENT