codeparrot/xlcost-text-to-code huggingface dataset:
Check whether an array can be made strictly decreasing by modifying at most one element | Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver code
def check ( arr , n ) : NEW_LINE INDENT modify = 0 NEW_LINE if ( arr [ n - 1 ] >= arr [ n - 2 ] ) : NEW_LINE INDENT arr [ n - 1 ] = arr [ n - 2 ] - 1 NEW_LINE modify += 1 NEW_LINE DEDENT if ( arr [ 0 ] <= arr [ 1 ] ) : NEW_LINE INDENT arr [ 0 ] = arr [ 1 ] + 1 NEW_LINE modify += 1 NEW_LINE DEDENT for i in range ( n - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] <= arr [ i ] and arr [ i + 1 ] <= arr [ i ] ) or ( arr [ i - 1 ] >= arr [ i ] and arr [ i + 1 ] >= arr [ i ] ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) // 2 NEW_LINE modify += 1 NEW_LINE if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT if ( modify > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 11 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Maximal Disjoint Intervals | Function to find maximal disjoint set ; sort the list of intervals ; First interval will always be included in set ; End point of first interval ; Check if given interval overlap with previously included interval , if not then include this interval and update the end point of last added interval ; Driver code
def maxDisjointIntervals ( list_ ) : NEW_LINE INDENT list_ . sort ( key = lambda x : x [ 1 ] ) NEW_LINE print ( " [ " , list_ [ 0 ] [ 0 ] , " , ▁ " , list_ [ 0 ] [ 1 ] , " ] " ) NEW_LINE r1 = list_ [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , len ( list_ ) ) : NEW_LINE INDENT l1 = list_ [ i ] [ 0 ] NEW_LINE r2 = list_ [ i ] [ 1 ] NEW_LINE if l1 > r1 : NEW_LINE INDENT print ( " [ " , l1 , " , ▁ " , r2 , " ] " ) NEW_LINE r1 = r2 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE intervals = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 4 , 6 ] , [ 8 , 9 ] ] NEW_LINE maxDisjointIntervals ( intervals ) NEW_LINE DEDENT
Print k different sorted permutations of a given array | Utility function to print the original indices of the elements of the array ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call
def printIndices ( n , a ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end = " " ) NEW_LINE DEDENT def printPermutations ( n , a , k ) : NEW_LINE INDENT arr = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] [ 0 ] = a [ i ] NEW_LINE arr [ i ] [ 1 ] = i NEW_LINE DEDENT arr . sort ( reverse = False ) NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count < k ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT next_pos = 1 NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT printIndices ( n , arr ) NEW_LINE for j in range ( next_pos , n ) : NEW_LINE INDENT if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ j - 1 ] NEW_LINE arr [ j - 1 ] = temp NEW_LINE next_pos = j + 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT printIndices ( n , arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 3 , 3 , 1 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE printPermutations ( n , a , k ) NEW_LINE DEDENT
Maximum width of a binary tree | A binary tree node ; Function to get the maximum width of a binary tree ; Get width of each level and compare the width with maximum width so far ; Get width of a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Constructed binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 ; Function call
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 getMaxWidth ( root ) : NEW_LINE INDENT maxWidth = 0 NEW_LINE h = height ( root ) NEW_LINE for i in range ( 1 , h + 1 ) : NEW_LINE INDENT width = getWidth ( root , i ) NEW_LINE if ( width > maxWidth ) : NEW_LINE INDENT maxWidth = width NEW_LINE DEDENT DEDENT return maxWidth NEW_LINE DEDENT def getWidth ( root , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if level == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif level > 1 : NEW_LINE INDENT return ( getWidth ( root . left , level - 1 ) + getWidth ( root . right , level - 1 ) ) NEW_LINE DEDENT DEDENT def height ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT lHeight = height ( node . left ) NEW_LINE rHeight = height ( node . right ) NEW_LINE return ( lHeight + 1 ) if ( lHeight > rHeight ) else ( rHeight + 1 ) NEW_LINE DEDENT 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 . right = Node ( 8 ) NEW_LINE root . right . right . left = Node ( 6 ) NEW_LINE root . right . right . right = Node ( 7 ) NEW_LINE print " Maximum ▁ width ▁ is ▁ % d " % ( getMaxWidth ( root ) ) NEW_LINE
Divide N segments into two non | Function to print the answer if it exists using the concept of merge overlapping segments ; Sort the indices based on their corresponding value in V ; Resultant array Initialise all the values in resultant array with '2' except the first index of ' indices ' which is initialised as '1' Initialise maxR to store the maximum of all right values encountered so far ; If the i - th index has any any point in common with the ( i - 1 ) th index classify it as '1' in resultant array and update maxR if necessary else we have found the breakpoint and we can exit the loop ; Driver Code
def printAnswer ( v , n ) : NEW_LINE INDENT indices = list ( range ( n ) ) NEW_LINE indices . sort ( key = lambda i : v [ i ] ) NEW_LINE res = [ 2 ] * n NEW_LINE res [ indices [ 0 ] ] = 1 NEW_LINE maxR = v [ indices [ 0 ] ] [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if maxR >= v [ indices [ i ] ] [ 0 ] : NEW_LINE INDENT res [ indices [ i ] ] = res [ indices [ i - 1 ] ] NEW_LINE maxR = max ( maxR , v [ indices [ i ] ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE return NEW_LINE DEDENT print ( " ▁ " . join ( map ( str , res ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v = [ [ 2 , 8 ] , [ 3 , 4 ] , [ 5 , 8 ] , [ 9 , 10 ] ] NEW_LINE n = len ( v ) NEW_LINE printAnswer ( v , n ) NEW_LINE DEDENT
Count distinct elements in an array | This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver code
def countDistinct ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] not in s ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDistinct ( arr , n ) ) NEW_LINE
In | Python3 program for the above approach ; Calculating next gap ; Function for swapping ; Merging the subarrays using shell sorting Time Complexity : O ( nlog n ) Space Complexity : O ( 1 ) ; merge sort makes log n recursive calls and each time calls merge ( ) which takes nlog n steps Time Complexity : O ( n * log n + 2 ( ( n / 2 ) * log ( n / 2 ) ) + 4 ( ( n / 4 ) * log ( n / 4 ) ) + ... . . + 1 ) Time Complexity : O ( logn * ( n * log n ) ) i . e . O ( n * ( logn ) ^ 2 ) Space Complexity : O ( 1 ) ; Calculating mid to slice the array in two halves ; Recursive calls to sort left and right subarrays ; UTILITY FUNCTIONS Function to pran array ; Driver Code
import math NEW_LINE def nextGap ( gap ) : NEW_LINE INDENT if gap <= 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return int ( math . ceil ( gap / 2 ) ) NEW_LINE DEDENT def swap ( nums , i , j ) : NEW_LINE INDENT temp = nums [ i ] NEW_LINE nums [ i ] = nums [ j ] NEW_LINE nums [ j ] = temp NEW_LINE DEDENT def inPlaceMerge ( nums , start , end ) : NEW_LINE INDENT gap = end - start + 1 NEW_LINE gap = nextGap ( gap ) NEW_LINE while gap > 0 : NEW_LINE INDENT i = start NEW_LINE while ( i + gap ) <= end : NEW_LINE INDENT j = i + gap NEW_LINE if nums [ i ] > nums [ j ] : NEW_LINE INDENT swap ( nums , i , j ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT gap = nextGap ( gap ) NEW_LINE DEDENT DEDENT def mergeSort ( nums , s , e ) : NEW_LINE INDENT if s == e : NEW_LINE INDENT return NEW_LINE DEDENT mid = ( s + e ) // 2 NEW_LINE mergeSort ( nums , s , mid ) NEW_LINE mergeSort ( nums , mid + 1 , e ) NEW_LINE inPlaceMerge ( nums , s , e ) NEW_LINE DEDENT def printArray ( A , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] NEW_LINE arr_size = len ( arr ) NEW_LINE mergeSort ( arr , 0 , arr_size - 1 ) NEW_LINE printArray ( arr , arr_size ) NEW_LINE DEDENT
Rearrange an array to maximize i * arr [ i ] | Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver Code
def findOptimalSolution ( a , N ) : NEW_LINE INDENT a . sort ( ) NEW_LINE points = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT points += a [ i ] * i NEW_LINE DEDENT return points NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 2 , 3 , 9 ] NEW_LINE N = len ( a ) NEW_LINE print ( findOptimalSolution ( a , N ) ) NEW_LINE DEDENT
Minimum number of towers required such that every house is in the range of at least one tower | Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; Driver code ; given elements ; print number of towers
def number_of_tower ( house , r , n ) : NEW_LINE INDENT house . sort ( ) NEW_LINE numOfTower = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT numOfTower += 1 NEW_LINE loc = house [ i ] + r NEW_LINE while ( i < n and house [ i ] <= loc ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT i -= 1 NEW_LINE loc = house [ i ] + r NEW_LINE while ( i < n and house [ i ] <= loc ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT return numOfTower NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT house = [ 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ] NEW_LINE r = 2 NEW_LINE n = len ( house ) NEW_LINE print ( number_of_tower ( house , r , n ) ) NEW_LINE DEDENT
Check if the characters of a given string are in alphabetical order | Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; sort the character array ; check if the character array is equal to the string or not ; Driver code ; check whether the string is in alphabetical order or not
def isAlphabaticOrder ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE c = [ s [ i ] for i in range ( len ( s ) ) ] NEW_LINE c . sort ( reverse = False ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( c [ i ] != s [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aabbbcc " NEW_LINE if ( isAlphabaticOrder ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Sort first k values in ascending order and remaining n | function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Our arr contains 8 elements
def printOrder ( arr , n , k ) : NEW_LINE INDENT a = arr [ 0 : k ] ; NEW_LINE a . sort ( ) ; NEW_LINE b = arr [ k : n ] ; NEW_LINE b . sort ( ) ; NEW_LINE b . reverse ( ) ; NEW_LINE return a + b ; NEW_LINE DEDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 ] ; NEW_LINE k = 4 ; NEW_LINE n = len ( arr ) ; NEW_LINE arr = printOrder ( arr , n , k ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT
Maximum width of a binary tree | A binary tree node ; Function to get the maximum width of a binary tree ; base case ; Initialize result ; Do Level order traversal keeping track of number of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Update the maximum node count value ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ;  ; Driver program to test above function ; Function call
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 getMaxWidth ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT maxWidth = 0 NEW_LINE q = [ ] NEW_LINE q . insert ( 0 , root ) NEW_LINE while ( q != [ ] ) : NEW_LINE INDENT count = len ( q ) NEW_LINE maxWidth = max ( count , maxWidth ) NEW_LINE while ( count is not 0 ) : NEW_LINE INDENT count = count - 1 NEW_LINE temp = q [ - 1 ] NEW_LINE q . pop ( ) NEW_LINE if temp . left is not None : NEW_LINE INDENT q . insert ( 0 , temp . left ) NEW_LINE DEDENT if temp . right is not None : NEW_LINE INDENT q . insert ( 0 , temp . right ) NEW_LINE DEDENT DEDENT DEDENT return maxWidth 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 . right = Node ( 8 ) NEW_LINE root . right . right . left = Node ( 6 ) NEW_LINE root . right . right . right = Node ( 7 ) NEW_LINE print " Maximum ▁ width ▁ is ▁ % d " % ( getMaxWidth ( root ) ) NEW_LINE
Merging and Sorting Two Unsorted Stacks | Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function ; This is the temporary stack
def sortStack ( Input ) : NEW_LINE INDENT tmpStack = [ ] NEW_LINE while len ( Input ) != 0 : NEW_LINE INDENT tmp = Input [ - 1 ] NEW_LINE Input . pop ( ) NEW_LINE while len ( tmpStack ) != 0 and tmpStack [ - 1 ] > tmp : NEW_LINE INDENT Input . append ( tmpStack [ - 1 ] ) NEW_LINE tmpStack . pop ( ) NEW_LINE DEDENT tmpStack . append ( tmp ) NEW_LINE DEDENT return tmpStack NEW_LINE DEDENT def sortedMerge ( s1 , s2 ) : NEW_LINE INDENT res = [ ] NEW_LINE while len ( s1 ) != 0 : NEW_LINE INDENT res . append ( s1 [ - 1 ] ) NEW_LINE s1 . pop ( ) NEW_LINE DEDENT while len ( s2 ) != 0 : NEW_LINE INDENT res . append ( s2 [ - 1 ] ) NEW_LINE s2 . pop ( ) NEW_LINE DEDENT return sortStack ( res ) NEW_LINE DEDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE s1 . append ( 34 ) NEW_LINE s1 . append ( 3 ) NEW_LINE s1 . append ( 31 ) NEW_LINE s2 . append ( 1 ) NEW_LINE s2 . append ( 12 ) NEW_LINE s2 . append ( 23 ) NEW_LINE tmpStack = [ ] NEW_LINE tmpStack = sortedMerge ( s1 , s2 ) NEW_LINE print ( " Sorted ▁ and ▁ merged ▁ stack ▁ : " ) NEW_LINE while len ( tmpStack ) != 0 : NEW_LINE INDENT print ( tmpStack [ - 1 ] , end = " ▁ " ) NEW_LINE tmpStack . pop ( ) NEW_LINE DEDENT
Lexicographical concatenation of all substrings of a string | Python Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code
def lexicographicSubConcat ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE sub_count = ( n * ( n + 1 ) ) // 2 ; NEW_LINE arr = [ 0 ] * sub_count ; NEW_LINE index = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , n - i + 1 ) : NEW_LINE INDENT arr [ index ] = s [ i : i + j ] ; NEW_LINE index += 1 ; NEW_LINE DEDENT DEDENT arr . sort ( ) ; NEW_LINE res = " " ; NEW_LINE for i in range ( sub_count ) : NEW_LINE INDENT res += arr [ i ] ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT s = " abc " ; NEW_LINE print ( lexicographicSubConcat ( s ) ) ; NEW_LINE
Insertion Sort by Swapping Elements | Iterative python program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j ; Driver Code
import math NEW_LINE def printVector ( V ) : NEW_LINE INDENT for i in V : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " ▁ " ) NEW_LINE DEDENT def insertionSort ( V ) : NEW_LINE INDENT N = len ( V ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE j = i NEW_LINE while ( j > 0 and V [ j ] < V [ j - 1 ] ) : NEW_LINE INDENT temp = V [ j ] ; NEW_LINE V [ j ] = V [ j - 1 ] ; NEW_LINE V [ j - 1 ] = temp ; NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT A = [ 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( " Array " ) NEW_LINE printVector ( A ) NEW_LINE print ( " After ▁ Sorting ▁ : " ) NEW_LINE insertionSort ( A ) NEW_LINE printVector ( A ) NEW_LINE
Program to sort string in descending order | Python program to sort a string in descending order using library function ; Driver code ; function call
def descOrder ( s ) : NEW_LINE INDENT s . sort ( reverse = True ) NEW_LINE str1 = ' ' . join ( s ) NEW_LINE print ( str1 ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT s = list ( ' geeksforgeeks ' ) NEW_LINE descOrder ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Sort string of characters | Python 3 program to sort a string of characters ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ; ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver Code
MAX_CHAR = 26 NEW_LINE def sortString ( str ) : NEW_LINE INDENT charCount = [ 0 for i in range ( MAX_CHAR ) ] NEW_LINE for i in range ( 0 , len ( str ) , 1 ) : NEW_LINE INDENT charCount [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , MAX_CHAR , 1 ) : NEW_LINE INDENT for j in range ( 0 , charCount [ i ] , 1 ) : NEW_LINE INDENT print ( chr ( ord ( ' a ' ) + i ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE sortString ( s ) NEW_LINE DEDENT
Smallest element in an array that is repeated exactly ' k ' times . | Python program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
MAX = 1000 NEW_LINE def findDuplicate ( arr , n , k ) : NEW_LINE INDENT freq = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 1 and arr [ i ] > MAX ) : NEW_LINE INDENT print " Out ▁ of ▁ range " NEW_LINE return - 1 NEW_LINE DEDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] == k ) : NEW_LINE INDENT return i NEW_LINE DEDENT 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
Find the Sub | Python3 program to find subarray with sum closest to 0 ; Returns subarray with sum closest to 0. ; To consider the case of subarray starting from beginning of the array ; Store prefix sum with index ; Sort on the basis of sum ; Find two consecutive elements with minimum difference ; Update minimum difference and starting and ending indexes ; Return starting and ending indexes ; Driver code
class prefix : NEW_LINE INDENT def __init__ ( self , sum , index ) : NEW_LINE INDENT self . sum = sum NEW_LINE self . index = index NEW_LINE DEDENT DEDENT def findSubArray ( arr , n ) : NEW_LINE INDENT start , end , min_diff = None , None , float ( ' inf ' ) NEW_LINE pre_sum = [ None ] * ( n + 1 ) NEW_LINE pre_sum [ 0 ] = prefix ( 0 , - 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT pre_sum [ i ] = prefix ( pre_sum [ i - 1 ] . sum + arr [ i - 1 ] , i - 1 ) NEW_LINE DEDENT pre_sum . sort ( key = lambda x : x . sum ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT diff = pre_sum [ i ] . sum - pre_sum [ i - 1 ] . sum NEW_LINE if min_diff > diff : NEW_LINE INDENT min_diff = diff NEW_LINE start = pre_sum [ i - 1 ] . index NEW_LINE end = pre_sum [ i ] . index NEW_LINE DEDENT DEDENT return ( start + 1 , end ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , - 4 , - 1 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE point = findSubArray ( arr , n ) NEW_LINE print ( " Subarray ▁ starting ▁ from " , point [ 0 ] , " to " , point [ 1 ] ) NEW_LINE DEDENT
Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calling pendulum function
def pendulumArrangement ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE op = [ 0 ] * n NEW_LINE mid = int ( ( n - 1 ) / 2 ) NEW_LINE j = 1 NEW_LINE i = 1 NEW_LINE op [ mid ] = arr [ 0 ] NEW_LINE for i in range ( 1 , mid + 1 ) : NEW_LINE INDENT op [ mid + i ] = arr [ j ] NEW_LINE j += 1 NEW_LINE op [ mid - i ] = arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT if ( int ( n % 2 ) == 0 ) : NEW_LINE INDENT op [ mid + i ] = arr [ j ] NEW_LINE DEDENT print ( " Pendulum ▁ arrangement : " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( op [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 14 , 6 , 19 , 21 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE pendulumArrangement ( arr , n ) NEW_LINE