codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of characters required to be removed such that every character occurs same number of times | Java program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minimumDeletion ( String s , int n ) { HashMap < Character , Integer > countMap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; countMap . put ( ch , countMap . getOrDefault ( ch , 0 ) + 1 ) ; } ArrayList < Integer > countMultiset = new ArrayList < > ( countMap . values ( ) ) ; Collections . sort ( countMultiset ) ; int ans = Integer . MAX_VALUE ; int i = 0 ; int m = countMultiset . size ( ) ; for ( int j : countMultiset ) { ans = Math . min ( ans , n - ( m - i ) * j ) ; i ++ ; } return ans ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . length ( ) ; System . out . println ( minimumDeletion ( S , N ) ) ; } }
Minimum number of distinct elements present in a K | Java program for the above approach ; Function to count the minimum number of distinct elements present in any subsequence of length K of the given array ; Stores the frequency of each array element ; Traverse the array ; Update frequency of array elements ; Store the required result ; Store the length of the required subsequence ; Store the frequencies in decreasing order ; Traverse the map ; Push the frequencies into the HashMap ; Sort the array in decreasing order ; Add the elements into the subsequence starting from one with highest frequency ; If length of subsequence is >= k ; Print the result ; Driver code ; Store the size of the array ; Function Call to count minimum number of distinct elmeents present in a K - length subsequence
import java . util . * ; class GFG { static void findMinimumDistinct ( int A [ ] , int N , int K ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) mp . put ( A [ i ] , mp . getOrDefault ( A [ i ] , 0 ) + 1 ) ; int count = 0 ; int len = 0 ; ArrayList < Integer > counts = new ArrayList < > ( ) ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) counts . add ( i . getValue ( ) ) ; Collections . sort ( counts , ( a , b ) -> b - a ) ; for ( int i = 0 ; i < counts . size ( ) ; i ++ ) { if ( len >= K ) break ; len += counts . get ( i ) ; count ++ ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 1 , 3 , 2 , 3 , 4 , 5 , 4 } ; int K = 4 ; int N = A . length ; findMinimumDistinct ( A , N , K ) ; } }
Calculate cost of visiting all array elements in increasing order | java program for the above approach ; Pair class ; Function to calculate total cost of visiting array elements in increasing order ; Stores the pair of element and their positions ; Traverse the array arr [ ] ; Push the pair { arr [ i ] , i } in v ; Sort the vector in ascending order . ; Stores the total cost ; Stores the index of last element visited ; Traverse the vector v ; Increment ans ; Assign ; Return ans ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int calculateDistance ( int arr [ ] , int N ) { Pair v [ ] = new Pair [ N ] ; for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Pair ( arr [ i ] , i ) ; Arrays . sort ( v , ( p1 , p2 ) -> { if ( p1 . first != p2 . first ) return p1 . first - p2 . first ; return p1 . second - p2 . second ; } ) ; int ans = 0 ; int last = 0 ; for ( Pair j : v ) { ans += Math . abs ( j . second - last ) ; last = j . second ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 5 , 1 } ; int N = arr . length ; System . out . println ( calculateDistance ( arr , N ) ) ; } }
Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | Java program for the above approach ; Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
import java . util . * ; class GFG { static void reverse ( char [ ] a ) { int i , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static void sortArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { String s = Integer . toString ( arr [ i ] ) ; char [ ] str = s . toCharArray ( ) ; Arrays . sort ( str ) ; reverse ( str ) ; String string = new String ( str ) ; arr [ i ] = Integer . parseInt ( string ) ; int sr = ( int ) Math . sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) arr [ i ] = a ; else arr [ i ] = b ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 29 , 43 , 28 , 12 } ; int N = arr . length ; sortArr ( arr , N ) ; } }
Minimize cost required to make all array elements greater than or equal to zero | Java program for the above approach ; Function to find the minimum cost to make all array elements greater than or equal to 0 ; Sort the array in ascending order ; Stores the cost to make current array element >= 0 ; Stores the cost to make all array elements >= 0 ; Traverse the array and insert all the elements which are < 0 ; If current array element is negative ; Cost to make all array elements >= 0 ; Update curr if ans is minimum ; Print the minimum cost ; Driver Code ; Given array ; Size of the array ; Given value of X ; Function call to find minimum cost to make all array elements >= 0
import java . util . Arrays ; public class GFG { static void minCost ( int arr [ ] , int N , int X ) { Arrays . sort ( arr ) ; int sum = 0 ; int cost = 0 ; int INT_MAX = Integer . MAX_VALUE ; int min_cost = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cost = Math . abs ( arr [ i ] ) * X + ( sum - Math . abs ( arr [ i ] ) * i ) ; sum += Math . abs ( arr [ i ] ) ; min_cost = Math . min ( min_cost , cost ) ; } } System . out . print ( min_cost ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 3 , - 2 , 4 , - 1 } ; int N = arr . length ; int X = 2 ; minCost ( arr , N , X ) ; } }
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | Java program for the above approach ; Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Driver Code ; Given vector ; Convert the input as ArrayList ; Given value of X & K
import java . util . ArrayList ; class GFG { static int ans = 0 ; public static void countArraysUtil ( ArrayList < Integer > arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr . get ( i ) > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr . get ( 0 ) ; arr . remove ( 0 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( 0 , temp ) ; temp = arr . get ( arr . size ( ) - 1 ) ; arr . remove ( arr . size ( ) - 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( temp ) ; arr . set ( 0 , arr . get ( 0 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( 0 , arr . get ( 0 ) - 1 ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) - 1 ) ; } public static void countArrays ( ArrayList < Integer > arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) xorVal = xorVal ^ arr . get ( i ) ; countArraysUtil ( arr , X , K , xorVal ) ; System . out . println ( ans ) ; } public static void main ( String arg [ ] ) { int [ ] input = { 10 , 2 , 10 , 5 } ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; for ( int i : input ) { arr . add ( i ) ; } int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; } }
Difference between maximum and minimum of a set of anagrams from an array | Java program for the above approach ; Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; If the end of Map is reached , then no anagrams are present ; Driver code ; Given array ; Size of the array
import java . util . * ; class GFG { static int hashFunction ( int N ) { int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 } ; int value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = N / 10 ; } return value ; } static void findDiff ( int [ ] arr , int n ) { HashMap < Integer , Vector < Integer > > m = new HashMap < > ( ) ; int h , min , max ; for ( int i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . containsKey ( h ) ) { m . put ( h , new Vector < Integer > ( ) ) ; } m . get ( h ) . add ( arr [ i ] ) ; } for ( Map . Entry < Integer , Vector < Integer > > i : m . entrySet ( ) ) { if ( i . getValue ( ) . size ( ) > 1 ) { min = Integer . MAX_VALUE ; max = - ( Integer . MAX_VALUE ) ; for ( int j = 0 ; j < i . getValue ( ) . size ( ) ; j ++ ) { if ( m . get ( i . getKey ( ) ) . get ( j ) < min ) { min = m . get ( i . getKey ( ) ) . get ( j ) ; } if ( m . get ( i . getKey ( ) ) . get ( j ) > max ) { max = m . get ( i . getKey ( ) ) . get ( j ) ; } } System . out . print ( max - min ) ; break ; } else if ( m . get ( i . getKey ( ) ) == m . values ( ) . toArray ( ) [ m . size ( ) - 1 ] ) System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 121 , 312 , 234 , 211 , 112 , 102 } ; int N = arr . length ; findDiff ( arr , N ) ; } }
Maximum area rectangle by picking four sides from array | Java program for finding maximum area possible of a rectangle ; function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; driver function
import java . util . Arrays ; import java . util . Collections ; public class GFG { static int findArea ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int [ ] dimension = { 0 , 0 } ; for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . length ; System . out . println ( findArea ( arr , n ) ) ; } }
Maximum area rectangle by picking four sides from array | Java program for finding maximum area possible of a rectangle ; function for finding max area ; traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; return the product of dimensions ; driver function
import java . util . HashSet ; import java . util . Set ; public class GFG { static int findArea ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < > ( ) ; int first = 0 , second = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . contains ( arr [ i ] ) ) { s . add ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } return ( first * second ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . length ; System . out . println ( findArea ( arr , n ) ) ; } }
Print root to leaf paths without using recursion | Java program to Print root to leaf path WITHOUT using recursion ; A binary tree node ; Function to print root to leaf path for a leaf using parent nodes stored in map ; start from leaf node and keep on pushing nodes into stack till root node is reached ; Start popping nodes from stack and print them ; An iterative function to do preorder traversal of binary tree and print root to leaf path without using recursion ; Corner Case ; Create an empty stack and push root to it ; Create a map to store parent pointers of binary tree nodes ; parent of root is NULL ; Pop all items one by one . Do following for every popped item a ) push its right child and set its parent pointer b ) push its left child and set its parent pointer Note that right child is pushed first so that left is processed first ; Pop the top item from stack ; If leaf node encountered , print Top To Bottom path ; Push right & left children of the popped node to stack . Also set their parent pointer in the map ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
import java . util . Stack ; import java . util . HashMap ; public class PrintPath { class Node { int data ; Node left , right ; Node ( int data ) { left = right = null ; this . data = data ; } } ; public static void printTopToBottomPath ( Node curr , HashMap < Node , Node > parent ) { Stack < Node > stk = new Stack < > ( ) ; while ( curr != null ) { stk . push ( curr ) ; curr = parent . get ( curr ) ; } while ( ! stk . isEmpty ( ) ) { curr = stk . pop ( ) ; System . out . print ( curr . data + " ▁ " ) ; } System . out . println ( ) ; } public static void printRootToLeaf ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < > ( ) ; nodeStack . push ( root ) ; HashMap < Node , Node > parent = new HashMap < > ( ) ; parent . put ( root , null ) ; while ( ! nodeStack . isEmpty ( ) ) { Node current = nodeStack . pop ( ) ; if ( current . left == null && current . right == null ) printTopToBottomPath ( current , parent ) ; if ( current . right != null ) { parent . put ( current . right , current ) ; nodeStack . push ( current . right ) ; } if ( current . left != null ) { parent . put ( current . left , current ) ; nodeStack . push ( current . left ) ; } } } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 2 ) ; printRootToLeaf ( root ) ; } }
Game of replacing array elements | Java program for Game of Replacement ; Function return which player win the game ; Create hash that will stores all distinct element ; Traverse an array element ; Driver Function
import java . util . HashSet ; public class GameOfReplacingArrayElements { public static int playGame ( int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i : arr ) set . add ( i ) ; return ( set . size ( ) % 2 == 0 ) ? 1 : 2 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 2 , 2 , 2 } ; System . out . print ( " Player ▁ " + playGame ( arr ) + " ▁ wins " ) ; } }
Length of longest strict bitonic subsequence | Java implementation to find length of longest strict bitonic subsequence ; function to find length of longest strict bitonic subsequence ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last / first element of that subsequence ; arrays to store the length of increasing and decreasing subsequences which end at them or start from them ; to store the length of longest strict bitonic subsequence ; traverse the array elements from left to right ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' inc ' ; update arr [ i ] subsequence length in ' inc ' and in len_inc [ ] ; traverse the array elements from right to left ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' dcr ' ; update arr [ i ] subsequence length in ' dcr ' and in len_dcr [ ] ; calculating the length of all the strict bitonic subsequence ; required longest length strict bitonic subsequence ; Driver code
import java . util . * ; class GfG { static int longLenStrictBitonicSub ( int arr [ ] , int n ) { HashMap < Integer , Integer > inc = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > dcr = new HashMap < Integer , Integer > ( ) ; int len_inc [ ] = new int [ n ] ; int len_dcr [ ] = new int [ n ] ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( inc . containsKey ( arr [ i ] - 1 ) ) len = inc . get ( arr [ i ] - 1 ) ; len_inc [ i ] = len + 1 ; inc . put ( arr [ i ] , len_inc [ i ] ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { int len = 0 ; if ( dcr . containsKey ( arr [ i ] - 1 ) ) len = dcr . get ( arr [ i ] - 1 ) ; len_dcr [ i ] = len + 1 ; dcr . put ( arr [ i ] , len_dcr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( " Longest ▁ length ▁ strict ▁ " + " bitonic ▁ subsequence ▁ = ▁ " + longLenStrictBitonicSub ( arr , n ) ) ; } }
Selection Sort VS Bubble Sort |  ; Driver code
class GFG { static void Selection_Sort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { int min_index = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( arr [ j ] < arr [ min_index ] ) min_index = j ; } int temp = arr [ i ] ; arr [ i ] = arr [ min_index ] ; arr [ min_index ] = temp ; } } public static void main ( String [ ] args ) { int n = 5 ; int arr [ ] = { 2 , 0 , 1 , 4 , 3 } ; Selection_Sort ( arr , n ) ; System . out . print ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Selection ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Last seen array element ( last appearance is earliest ) | Java program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; Driver Code
import java . util . * ; class GFG { static int lastSeenElement ( int a [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { hash . put ( a [ i ] , i ) ; } int res_ind = Integer . MAX_VALUE , res = 0 ; for ( Map . Entry < Integer , Integer > x : hash . entrySet ( ) ) { if ( x . getValue ( ) < res_ind ) { res_ind = x . getValue ( ) ; res = x . getKey ( ) ; } } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 1 , 2 , 2 , 4 , 1 } ; int n = a . length ; System . out . println ( lastSeenElement ( a , n ) ) ; } }
Selection Sort VS Bubble Sort |  ; Driver code
import java . io . * ; class GFG { static void Bubble_Sort ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; } } } } public static void main ( String [ ] args ) { int n = 5 ; int arr [ ] = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; System . out . print ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | Java implementation of the above approach ; Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Stores all the array elements in sorted order ; Traverse the array ; Insert current element into multiset ; Perform each operation ; Stores smallest element of ms ; Stores the largest element of ms ; Stores updated value of smallest element of ms by given operations ; Stores updated value of largest element of ms by given operations ; If the value of a + b less than the sum of smallest and largest array element ; Erase the smallest element ; Erase the largest element ; Insert the updated value of the smallest element ; Insert the updated value of the smallest element ; Stores sum of array elements ; Traverse the multiset ; Update ans ; Driver Code
import java . util . * ; class GFG { static int minimum_possible_sum ( int arr [ ] , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } Vector < Integer > ms = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ms . add ( arr [ i ] ) ; } Collections . sort ( ms ) ; for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = ms . get ( 0 ) ; int largest_element = ms . get ( ms . size ( ) - 1 ) ; int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { ms . remove ( 0 ) ; ms . remove ( ms . size ( ) - 1 ) ; ms . add ( a ) ; ms . add ( b ) ; Collections . sort ( ms ) ; } } int ans = 0 ; for ( int x : ms ) { ans += x ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = arr . length ; System . out . print ( minimum_possible_sum ( arr , n , K ) ) ; } }
Lexicographically smallest subsequence possible by removing a character from given string | Java program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given String S ; Function Call
import java . util . * ; class GFG { static void firstSubsequence ( String s ) { int isMax = - 1 ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s = s . substring ( 0 , isMax ) + s . substring ( isMax + 1 ) ; s . rerase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; s = s . substring ( 0 , s . length ( ) - 1 ) ; } System . out . print ( s ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; firstSubsequence ( S ) ; } }