codeparrot/xlcost-text-to-code huggingface dataset:
Farthest index that can be reached from the Kth index of given array by given operations | C ++ program for the above approach ; Function to find the farthest index that can be reached ; Declare a priority queue ; Iterate the array ; If current element is greater than the next element ; Otherwise , store their difference ; Push diff into pq ; If size of pq exceeds Y ; Decrease X by the top element of pq ; Remove top of pq ; If X is exhausted ; Current index is the farthest possible ; Print N - 1 as farthest index ; Driver Code ; Function Call ;
# include < bits / stdc ++ . h > using namespace std ; void farthestHill ( int arr [ ] , int X , int Y , int N , int K ) { int i , diff ; priority_queue < int > pq ; for ( i = K ; i < N - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i + 1 ] ) continue ; diff = arr [ i + 1 ] - arr [ i ] ; pq . push ( diff ) ; if ( pq . size ( ) > Y ) { X -= pq . top ( ) ; pq . pop ( ) ; } if ( X < 0 ) { cout << i ; return ; } } cout << N - 1 ; } int main ( ) { int arr [ ] = { 4 , 2 , 7 , 6 , 9 , 14 , 12 } ; int X = 5 , Y = 1 ; int K = 0 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; farthestHill ( arr , X , Y , N , K ) ; return 0 ; } ]
Lexicographically largest string possible consisting of at most K consecutive similar characters | Java solution for above approach ; Function to find largest string ; Stores the frequency of characters ; Traverse the string ; Stores the resultant string ; Append larger character ; Decrease count in charset ; Increase count ; Check if count reached to charLimit ; Find nearest lower char ; If no character can be appended ; Append nearest lower character ; Reset count for next calculation ; Return new largest string ; Function to return nearest lower character ; Traverse charset from start - 1 ; If no character can be appended ; Driver Code
import java . util . * ; class GFG { static String newString ( String originalLabel , int limit ) { int n = originalLabel . length ( ) ; int [ ] charset = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { charset [ originalLabel . charAt ( i ) - ' a ' ] ++ ; } StringBuilder newString = new StringBuilder ( n ) ; for ( int i = 25 ; i >= 0 ; i -- ) { int count = 0 ; while ( charset [ i ] > 0 ) { newString . append ( ( char ) ( i + ' a ' ) ) ; charset [ i ] -- ; count ++ ; if ( charset [ i ] > 0 && count == limit ) { Character next = nextAvailableChar ( charset , i ) ; if ( next == null ) return newString . toString ( ) ; newString . append ( next ) ; count = 0 ; } } } return newString . toString ( ) ; } static Character nextAvailableChar ( int [ ] charset , int start ) { for ( int i = start - 1 ; i >= 0 ; i -- ) { if ( charset [ i ] > 0 ) { charset [ i ] -- ; return ( char ) ( i + ' a ' ) ; } } return null ; } public static void main ( String [ ] args ) { String S = " ccbbb " ; int K = 2 ; System . out . println ( newString ( S , K ) ) ; } }
Rearrange array by interchanging positions of even and odd elements in the given array | Java program for the above approach ; Function to replace odd elements with even elements and vice versa ; Length of the array ; Traverse the given array ; If arr [ i ] is visited ; Find the next odd element ; Find next even element ; Mark them visited ; Swap them ; Print the final array ; Driver Code ; Given array arr [ ] ; Function Call
import java . io . * ; class GFG { static void swapEvenOdd ( int arr [ ] ) { int n = arr . length ; int o = - 1 , e = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) continue ; int r = - 1 ; if ( arr [ i ] % 2 == 0 ) { o ++ ; while ( arr [ o ] % 2 == 0 arr [ o ] < 0 ) o ++ ; r = o ; } else { e ++ ; while ( arr [ e ] % 2 == 1 arr [ e ] < 0 ) e ++ ; r = e ; } arr [ i ] *= - 1 ; arr [ r ] *= - 1 ; int tmp = arr [ i ] ; arr [ i ] = arr [ r ] ; arr [ r ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ( - 1 * arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; swapEvenOdd ( arr ) ; } }
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Java program to implement the above approach ; Iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; Else we could just break the loop as its not a palindrome type sequence ; We could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; We would check for the palindrome condition if yes we print True else False ; Driver Code
import java . util . * ; import java . io . * ; class GFG { public static String rev ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; char [ ] s = str . toCharArray ( ) ; while ( st < ed ) { char temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return ( s . toString ( ) ) ; } public static boolean check ( String a , String b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a . charAt ( i ) != b . charAt ( j ) ) break ; i += 1 ; j -= 1 ; String xa = a . substring ( i , j + 1 ) ; String xb = b . substring ( i , j + 1 ) ; StringBuffer XA = new StringBuffer ( xa ) ; XA . reverse ( ) ; StringBuffer XB = new StringBuffer ( xb ) ; XB . reverse ( ) ; if ( xa == XA . toString ( ) || xb == XB . toString ( ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String a = " xbdef " ; String b = " cabex " ; if ( check ( a , b , a . length ( ) ) || check ( b , a , a . length ( ) ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
Maximize length of subarray having equal elements by adding at most K | Java program for above approach ; Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Driver Code ; Function call
import java . util . * ; class GFG { public static int maxEqualIdx ( int [ ] arr , int k ) { Arrays . sort ( arr ) ; int [ ] prefixSum = new int [ arr . length + 1 ] ; prefixSum [ 1 ] = arr [ 0 ] ; for ( int i = 1 ; i < prefixSum . length - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } int max = arr . length ; int min = 1 ; int ans = 1 ; while ( min <= max ) { int mid = ( max + min ) / 2 ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } public static boolean check ( int [ ] pSum , int len , int k , int [ ] a ) { int i = 0 ; int j = len ; while ( j <= a . length ) { int maxSize = a [ j - 1 ] ; int totalNumbers = maxSize * len ; int currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int k = 7 ; System . out . println ( maxEqualIdx ( arr , k ) ) ; } }
Sum of product of all pairs of a Binary Array | Java program to implement the above approach ; Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; Stores the size of the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code
import java . util . * ; class GFG { static int productSum ( int [ ] arr ) { int cntOne = 0 ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 } ; System . out . println ( productSum ( arr ) ) ; } }
Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | Java program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Find the number of digits ; Calculate sum of the digits ; Add the digits of num2 ; If the sum of the digits of N is less than or equal to K ; No number needs to be added ; Otherwise ; Calculate the sum of digits from least significant digit ; If sum exceeds K ; Increase previous digit by 1 ; Add zeros to the end ; Calculate difference between the result and N ; Return the result ; Driver Code
import java . util . * ; import java . io . * ; class GFG { static int minDigits ( int N , int K ) { int digits_num = ( int ) Math . floor ( Math . log ( N ) + 1 ) ; int temp_sum = 0 ; int temp = digits_num ; int result = 0 ; int X , var ; int sum = 0 ; int num2 = N ; while ( num2 != 0 ) { sum += num2 % 10 ; num2 /= 10 ; } if ( sum <= K ) { X = 0 ; } else { while ( temp > 0 ) { var = ( N / ( ( int ) Math . pow ( 10 , temp - 1 ) ) ) ; temp_sum += var % 10 ; if ( temp_sum >= K ) { var /= 10 ; var ++ ; result = var * ( int ) Math . pow ( 10 , temp ) ; break ; } temp -- ; } X = result - N ; return X ; } return - 1 ; } public static void main ( String args [ ] ) { int N = 11 ; int K = 1 ; System . out . println ( minDigits ( N , K ) ) ; } }
Count of Ways to obtain given Sum from the given Array elements | Java Program to implement the above approach ; Function to call dfs to calculate the number of ways ; Iterate till the length of array ; Initialize the memorization table ; Function to perform the DFS to calculate the number of ways ; Base case : Reached the end of array ; If current sum is obtained ; Otherwise ; If previously calculated subproblem occurred ; Check if the required sum can be obtained by adding current element or by subtracting the current index element ; Store the count of ways ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int findWays ( int [ ] nums , int S ) { int sum = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) sum += nums [ i ] ; int [ ] [ ] memo = new int [ nums . length + 1 ] [ 2 * sum + 1 ] ; for ( int [ ] m : memo ) { Arrays . fill ( m , Integer . MIN_VALUE ) ; } return dfs ( memo , nums , S , 0 , 0 , sum ) ; } static int dfs ( int [ ] [ ] memo , int [ ] nums , int S , int curr_sum , int index , int sum ) { if ( index == nums . length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } if ( memo [ index ] [ curr_sum + sum ] != Integer . MIN_VALUE ) { return memo [ index ] [ curr_sum + sum ] ; } int ans = dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) ; memo [ index ] [ curr_sum + sum ] = ans ; return ans ; } public static void main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = findWays ( arr , S ) ; System . out . println ( answer ) ; } }
Longest subarray with sum not divisible by X | Java program to implement the above approach ; Function to print the longest subarray with sum of elements not divisible by X ; Pref [ ] stores the prefix sum Suff [ ] stores the suffix sum ; If array element is divisibile by x ; Increase count ; If all the array elements are divisible by x ; No subarray possible ; Reverse v to calculate the suffix sum ; Calculate the suffix sum ; Reverse to original form ; Reverse the suffix sum array ; Calculate the prefix sum ; Stores the starting index of required subarray ; Stores the ending index of required subarray ; If suffix sum till i - th index is not divisible by x ; Update the answer ; If prefix sum till i - th index is not divisible by x ; Update the answer ; Print the longest subarray ; Driver Code
import java . util . * ; class GFG { static void max_length ( int N , int x , int [ ] v ) { int i , a ; List < Integer > preff = new Vector < Integer > ( ) ; List < Integer > suff = new Vector < Integer > ( ) ; int ct = 0 ; for ( i = 0 ; i < N ; i ++ ) { a = v [ i ] ; if ( a % x == 0 ) { ct += 1 ; } } if ( ct == N ) { System . out . print ( - 1 + "NEW_LINE"); return ; } v = reverse ( v ) ; suff . add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { suff . add ( v [ i ] + suff . get ( i - 1 ) ) ; } v = reverse ( v ) ; Collections . reverse ( suff ) ; preff . add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { preff . add ( v [ i ] + preff . get ( i - 1 ) ) ; } int ans = 0 ; int lp = 0 ; int rp = N - 1 ; for ( i = 0 ; i < N ; i ++ ) { if ( suff . get ( i ) % x != 0 && ( ans < ( N - 1 ) ) ) { lp = i ; rp = N - 1 ; ans = Math . max ( ans , N - i ) ; } if ( preff . get ( i ) % x != 0 && ( ans < ( i + 1 ) ) ) { lp = 0 ; rp = i ; ans = Math . max ( ans , i + 1 ) ; } } for ( i = lp ; i <= rp ; i ++ ) { System . out . print ( v [ i ] + " ▁ " ) ; } } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int x = 3 ; int [ ] v = { 1 , 3 , 2 , 6 } ; int N = v . length ; max_length ( N , x , v ) ; } }
Number of times Maximum and minimum value updated during traversal of array | Java implementation to find the number of times minimum and maximum value updated during the traversal of the array ; Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code
public class GFG { static void maxUpdated ( int [ ] arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; for ( Integer n : arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } System . out . print ( " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ) ; System . out . print ( " updated ▁ = ▁ " + i + "NEW_LINE"); System . out . print ( " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ) ; System . out . print ( " updated ▁ = ▁ " + j ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 20 , 22 } ; maxUpdated ( arr ) ; } }
Find the farthest smaller number in the right side | Java implementation of the approach ; Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code
class GFG { static void farthest_min ( int [ ] a , int n ) { int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int low = i + 1 , high = n - 1 , ans = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } System . out . print ( ans + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] a = { 3 , 1 , 5 , 2 , 4 } ; int n = a . length ; farthest_min ( a , n ) ; } }
Print numbers in descending order along with their frequencies | Java program to print the elements in descending along with their frequencies ; Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
import java . util . * ; class GFG { static void printElements ( int a [ ] , int n ) { Arrays . sort ( a ) ; a = reverse ( a ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { System . out . print ( a [ i ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); cnt = 1 ; } else cnt += 1 ; } System . out . print ( a [ n - 1 ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = a . length ; printElements ( a , n ) ; } }
Check if array can be sorted with one swap | Java program to check if an array can be sorted with at - most one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Code
import java . util . Arrays ; class GFG { static boolean checkSorted ( int n , int arr [ ] ) { int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = arr [ i ] ; Arrays . sort ( b , 0 , n ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != b [ i ] ) ct ++ ; if ( ct == 0 ct == 2 ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; if ( checkSorted ( n , arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check whether ( i , j ) exists such that arr [ i ] != arr [ j ] and arr [ arr [ i ] ] is equal to arr [ arr [ j ] ] | Function that calculates marks . ; Checking 1 st condition i . e whether Arr [ i ] equal to Arr [ j ] or not ; Checking 2 nd condition i . e whether Arr [ Arr [ i ] ] equal to Arr [ Arr [ j ] ] or not . ; Driver code
class GFG { static boolean checkIndices ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] != Arr [ j ] ) { if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) return true ; } } } return false ; } public static void main ( String args [ ] ) { int Arr [ ] = { 3 , 2 , 1 , 1 , 4 } ; int N = Arr . length ; if ( checkIndices ( Arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find two non | Java program to find two non - overlapping pairs having equal sum in an Array ; Declare a pair ; Function to find two non - overlapping pairs with same sum in an array ; first create an empty map key -> which is sum of a pair of elements in the array value -> list storing index of every pair having that sum ; consider every pair ( A [ i ] , A [ j ] ) where ( j > i ) ; calculate sum of current pair ; if sum is already present in the map ; check every pair having desired sum ; if pairs don 't overlap, print  them and return ; Insert current pair into the map ; Driver Code
import java . util . ArrayList ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; class Pair { public int x , y ; Pair ( int x , int y ) { this . x = x ; this . y = y ; } } class GFG { public static void findPairs ( int [ ] A ) { Map < Integer , List < Pair > > map = new HashMap < > ( ) ; for ( int i = 0 ; i < A . length - 1 ; i ++ ) { for ( int j = i + 1 ; j < A . length ; j ++ ) { int sum = A [ i ] + A [ j ] ; if ( map . containsKey ( sum ) ) { for ( Pair pair : map . get ( sum ) ) { int x = pair . x ; int y = pair . y ; if ( ( x != i && x != j ) && ( y != i && y != j ) ) { System . out . println ( " Pair ▁ First ( " + A [ i ] + " , ▁ " + A [ j ] + " ) " ) ; System . out . println ( " Pair ▁ Second ▁ ( " + A [ x ] + " , ▁ " + A [ y ] + " ) " ) ; return ; } } } map . putIfAbsent ( sum , new ArrayList < > ( ) ) ; map . get ( sum ) . add ( new Pair ( i , j ) ) ; } } System . out . print ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 8 , 4 , 7 , 8 , 4 } ; findPairs ( A ) ; } }
Print all pairs with given sum | Java implementation of simple method to find print pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Consider all possible pairs and check their sums ; Driver Code
class GFG { static void printPairs ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) System . out . println ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; } public static void main ( String [ ] arg ) { int arr [ ] = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
Ways to choose three points with distance between the most distant points <= L | Java program to count ways to choose triplets such that the distance between the farthest points <= L ; Returns the number of triplets with the distance between farthest points <= L ; sort the array ; find index of element greater than arr [ i ] + L ; find Number of elements between the ith index and indexGreater since the Numbers are sorted and the elements are distinct from the points btw these indices represent points within range ( a [ i ] + 1 and a [ i ] + L ) both inclusive ; if there are at least two elements in between i and indexGreater find the Number of ways to select two points out of these ; Driver Code ; set of n points on the X axis
import java . util . * ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Arrays . sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int indexGreater = upper_bound ( arr , 0 , n , arr [ i ] + L ) ; int numberOfElements = indexGreater - ( i + 1 ) ; if ( numberOfElements >= 2 ) { ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; } } return ways ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int L = 4 ; int ans = countTripletsLessThanL ( n , L , arr ) ; System . out . println ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }
Making elements distinct in a sorted array by minimum increments | Java program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
import java . io . * ; class GFG { static int minSum ( int arr [ ] , int n ) { int sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { int j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . length ; System . out . println ( minSum ( arr , n ) ) ; } }
Making elements distinct in a sorted array by minimum increments | Efficient Java program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Drivers code
import java . io . * ; class GFG { static int minSum ( int arr [ ] , int n ) { int sum = arr [ 0 ] , prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= prev ) { prev = prev + 1 ; sum = sum + prev ; } else { sum = sum + arr [ i ] ; prev = arr [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . length ; System . out . println ( minSum ( arr , n ) ) ; } }