codeparrot/xlcost-text-to-code huggingface dataset:
Find the segment that overlaps with maximum number of segments | Java program for the above approach ; Function to find the segment which overlaps with maximum number of segments ; ' L ' & ' R ' co - ordinates of all segments are stored in lvalues & rvalues ; Assign co - ordinates ; Co - ordinate compression ; Stores the required segment ; Stores the current maximum number of intersections ; Find number of ' R ' coordinates which are less than the ' L ' value of the current segment ; Find number of ' L ' coordinates which are greater than the ' R ' value of the current segment ; Segments excluding ' lesser ' and ' greater ' gives the number of intersections ; Update the current maximum ; Print segment coordinates ; Driver Code ; Given segments ; Size of segments array
import java . util . * ; class GFG { static void maxIntersection ( int segments [ ] [ ] , int N ) { int [ ] rvalues = new int [ N ] ; int [ ] lvalues = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i ] [ 0 ] ; rvalues [ i ] = segments [ i ] [ 1 ] ; } Arrays . sort ( lvalues ) ; Arrays . sort ( rvalues ) ; int [ ] answer = { - 1 , - 1 } ; int numIntersections = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int lesser = lower_bound ( rvalues , 0 , segments . length , segments [ i ] [ 0 ] ) ; int greater = Math . max ( 0 , N - ( upper_bound ( lvalues , 0 , segments . length , segments [ i ] [ 1 ] ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = new int [ ] { segments [ i ] [ 0 ] , segments [ i ] [ 1 ] } ; numIntersections = ( N - lesser - greater ) ; } } System . out . print ( answer [ 0 ] + " ▁ " + answer [ 1 ] ) ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } 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 segments [ ] [ ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 6 } } ; int N = segments . length ; maxIntersection ( segments , N ) ; } }
Count swaps required to sort an array using Insertion Sort | Java program for the above approach ; Stores the sorted array elements ; Function to count the number of swaps required to merge two sorted subarray in a sorted form ; Stores the count of swaps ; Function to count the total number of swaps required to sort the array ; Stores the total count of swaps required ; Find the middle index splitting the two halves ; Count the number of swaps required to sort the left subarray ; Count the number of swaps required to sort the right subarray ; Count the number of swaps required to sort the two sorted subarrays ; Driver code
import java . util . * ; class GFG { static int temp [ ] = new int [ 100000 ] ; static int merge ( int A [ ] , int left , int mid , int right ) { int swaps = 0 ; int i = left , j = mid , k = left ; while ( i < mid && j <= right ) { if ( A [ i ] <= A [ j ] ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } else { temp [ k ] = A [ j ] ; k ++ ; j ++ ; swaps += mid - i ; } } while ( i < mid ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } while ( j <= right ) { temp [ k ] = A [ j ] ; k ++ ; j ++ ; } while ( left <= right ) { A [ left ] = temp [ left ] ; left ++ ; } return swaps ; } static int mergeInsertionSwap ( int A [ ] , int left , int right ) { int swaps = 0 ; if ( left < right ) { int mid = left + ( right - left ) / 2 ; swaps += mergeInsertionSwap ( A , left , mid ) ; swaps += mergeInsertionSwap ( A , mid + 1 , right ) ; swaps += merge ( A , left , mid + 1 , right ) ; } return swaps ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 3 , 1 , 2 } ; int N = A . length ; System . out . println ( mergeInsertionSwap ( A , 0 , N - 1 ) ) ; } }
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | Java program to implement the above approach ; Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int maxCntRemovedfromArray ( int [ ] arr , int N , int [ ] brr , int M ) { Arrays . sort ( arr ) ; int i = 0 ; int sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } int sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } int cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 } ; int [ ] brr = new int [ ] { 7 } ; int N = arr . length ; int M = brr . length ; System . out . println ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ; } }
Find any two pairs ( a , b ) and ( c , d ) such that a d | Java implementation to sort the array of points by its distance from the given point ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPair ( pair arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int a = arr [ i ] . first , b = arr [ i ] . second ; for ( int j = i + 1 ; j < N ; j ++ ) { int c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { System . out . println ( " ( " + a + " ▁ " + b + " ) , ▁ ( " + c + " ▁ " + d + " ) " ) ; return ; } } } System . out . println ( " NO ▁ SUCH ▁ PAIR ▁ EXIST " ) ; } public static void main ( String [ ] args ) { pair arr [ ] = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findPair ( arr , 5 ) ; } }
Find any two pairs ( a , b ) and ( c , d ) such that a d | Java program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code
import java . util . * ; class GFG { static class pair implements Comparable < pair > { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int compareTo ( pair p ) { return this . first - p . first ; } } static void findpair ( pair [ ] arr , int N ) { Arrays . sort ( arr ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { System . out . print ( " ( " + arr [ i - 1 ] . first + " ▁ " + b + " ) , ▁ ( " + arr [ i ] . first + " ▁ " + d + " ) " ) ; return ; } } System . out . print ( "NO SUCH PAIR EXISTNEW_LINE"); } public static void main ( String [ ] args ) { pair arr [ ] = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findpair ( arr , 5 ) ; } }
Print indices in non | package whatever do not write package name here ; Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printOrder ( int order [ ] , int N , int X ) { ArrayList < int [ ] > vect = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . add ( new int [ ] { order [ i ] / X , i + 1 } ) ; } else { vect . add ( new int [ ] { order [ i ] / X + 1 , i + 1 } ) ; } } Collections . sort ( vect , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( vect . get ( i ) [ 1 ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { int N = 3 , X = 3 ; int order [ ] = { 2 , 7 , 4 } ; printOrder ( order , N , X ) ; } }
Shuffle 2 n integers as a1 | Java program for the above approach ; Function to reverse the array from the position ' start ' to position ' end ' ; Stores mid of start and end ; Traverse the array in the range [ start , end ] ; Stores arr [ start + i ] ; Update arr [ start + i ] ; Update arr [ end - i ] ; Utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; Stores the length of the array ; If length of the array is 2 ; Stores mid of the { start , end } ; Divide array into two halves of even length ; Update mid ; Calculate the mid - points of both halves of the array ; Reverse the subarray made from mid1 to mid2 ; Reverse the subarray made from mid1 to mid ; Reverse the subarray made from mid to mid2 ; Recursively calls for both the halves of the array ; Function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; Function Call ; Print the modified array ; Driver Code ; Given array ; Size of the array ; Shuffles the given array to the required permutation
class GFG { static void reverse ( int arr [ ] , int start , int end ) { int mid = ( end - start + 1 ) / 2 ; for ( int i = 0 ; i < mid ; i ++ ) { int temp = arr [ start + i ] ; arr [ start + i ] = arr [ end - i ] ; arr [ end - i ] = temp ; } return ; } static void shuffleArrayUtil ( int arr [ ] , int start , int end ) { int i ; int l = end - start + 1 ; if ( l == 2 ) return ; int mid = start + l / 2 ; if ( l % 4 > 0 ) { mid -= 1 ; } int mid1 = start + ( mid - start ) / 2 ; int mid2 = mid + ( end + 1 - mid ) / 2 ; reverse ( arr , mid1 , mid2 - 1 ) ; reverse ( arr , mid1 , mid - 1 ) ; reverse ( arr , mid , mid2 - 1 ) ; shuffleArrayUtil ( arr , start , mid - 1 ) ; shuffleArrayUtil ( arr , mid , end ) ; } static void shuffleArray ( int arr [ ] , int N , int start , int end ) { shuffleArrayUtil ( arr , start , end ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . printf ( " % d ▁ " , arr [ i ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 2 , 4 , 6 } ; int N = arr . length ; shuffleArray ( arr , N , 0 , N - 1 ) ; } }
Reduce sum of same | Java program for the above approach ; Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
import java . util . * ; class GFG { static void rearrange ( int A [ ] , int B [ ] , int N , int X ) { boolean flag = true ; Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 1 , 2 } ; int X = 4 ; int N = A . length ; rearrange ( A , B , N , X ) ; } }
Minimum increments or decrements by D required to make all array elements equal | Java program for the above approach ; Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Driver code ; Given N & D ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static void numOperation ( int arr [ ] , int N , int D ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) { System . out . println ( " - 1" ) ; return ; } } int count = 0 ; int mid = arr [ N / 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { count += Math . abs ( mid - arr [ i ] ) / D ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 4 , D = 2 ; int arr [ ] = { 2 , 4 , 6 , 8 } ; numOperation ( arr , N , D ) ; } }
Maximize sum of second minimums of each K length partitions of the array | Java program for the above approach ; Function to find the maximum sum of second smallest of each partition of size K ; Sort the array A [ ] in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array A [ ] ; Size of the given array ; Function Call
import java . util . * ; class GFG { static void findSum ( int A [ ] , int N , int K ) { Arrays . sort ( A ) ; int sum = 0 ; for ( int i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { int K = 4 ; int A [ ] = { 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 } ; int N = A . length ; findSum ( A , N , K ) ; } }
Minimize remaining array element by removing pairs and replacing them with their average | Java program to implement the above approach ; Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver Code
import java . util . PriorityQueue ; class GFG { static int findSmallestNumLeft ( int arr [ ] , int N ) { PriorityQueue < Integer > PQ = new PriorityQueue < Integer > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . add ( arr [ i ] ) ; } while ( PQ . size ( ) > 1 ) { int top1 = PQ . peek ( ) ; PQ . remove ( ) ; int top2 = PQ . peek ( ) ; PQ . remove ( ) ; PQ . add ( ( top1 + top2 + 1 ) / 2 ) ; } return PQ . peek ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 30 , 16 , 40 } ; int N = arr . length ; System . out . print ( findSmallestNumLeft ( arr , N ) ) ; } }
Find the array element from indices not divisible by K having largest composite product of digits | Java program to implement the above approach ; Function to check if a number is a composite number or not ; Corner cases ; Check if number is divisible by 2 or 3 ; Check if number is a multiple of any other prime number ; Function to calculate the product of digits of a number ; Stores the product of digits ; Extract digits of a number ; Calculate product of digits ; Function to check if the product of digits of a number is a composite number or not ; Stores product of digits ; If product of digits is equal to 1 ; If product of digits is not prime ; Function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; Traverse the array ; If index is divisible by k ; Check if product of digits is a composite number or not ; Sort the products ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static int digitProduct ( int number ) { int product = 1 ; while ( number > 0 ) { product *= ( number % 10 ) ; number /= 10 ; } return product ; } static boolean compositedigitProduct ( int num ) { int res = digitProduct ( num ) ; if ( res == 1 ) { return false ; } if ( isComposite ( res ) ) { return true ; } return false ; } static int largestCompositeDigitProduct ( int a [ ] , int n , int k ) { Vector < pair > pq = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i % k ) == 0 ) { continue ; } if ( compositedigitProduct ( a [ i ] ) ) { int b = digitProduct ( a [ i ] ) ; pq . add ( new pair ( b , a [ i ] ) ) ; } } Collections . sort ( pq , ( x , y ) -> x . first - y . first ) ; return pq . get ( pq . size ( ) - 1 ) . second ; } public static void main ( String [ ] args ) { int arr [ ] = { 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 } ; int n = arr . length ; int k = 3 ; int ans = largestCompositeDigitProduct ( arr , n , k ) ; System . out . print ( ans + "NEW_LINE"); } }
Split array into K | Java implementation for the above approach ; Function to find the minimum sum of 2 nd smallest elements of each subset ; Sort the array ; Stores minimum sum of second elements of each subset ; Traverse first K 2 nd smallest elements ; Update their sum ; Print the sum ; Driver Code ; Given Array ; Given length of array ; Given subset lengths
import java . io . * ; import java . util . * ; class GFG { public static void findMinimum ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 1 ; i < 2 * ( N / K ) ; i += 2 ) { ans += arr [ i ] ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 } ; int N = arr . length ; int K = 5 ; findMinimum ( arr , N , K ) ; } }
Find two non | Java program for the above approach ; Function to check if two non - intersecting subarrays with equal sum exists or not ; Sort the given array ; Traverse the array ; Check for duplicate elements ; If no duplicate element is present in the array ; Driver code ; Given array ; Size of array
import java . util . * ; class GFG { static void findSubarrays ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int i = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 0 , 1 , 2 , 0 } ; int N = arr . length ; findSubarrays ( arr , N ) ; } }
Selection Sort VS Bubble Sort | Java program for the above approach ; Function for bubble sort ; Iterate from 1 to n - 1 ; Iterate from 0 to n - i - 1 ; Driver Code
import java . io . * ; class GFG { static void Bubble_Sort ( int [ ] arr , int n ) { boolean flag ; for ( int i = 1 ; i < n ; ++ i ) { flag = false ; 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 ; flag = true ; } } if ( flag == false ) break ; } } 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 ] + " ▁ " ) ; } }
Print all array elements appearing more than N / K times | Java program to implement the above approach ; Function to print all array elements whose frequency is greater than N / K ; Sort the array , arr [ ] ; Traverse the array ; Stores frequency of arr [ i ] ; Traverse array elements which is equal to arr [ i ] ; Update cnt ; Update i ; If frequency of arr [ i ] is greater than ( N / K ) ; Driver Code
import java . util . * ; class GFG { static void NDivKWithFreq ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; ) { int cnt = 1 ; while ( ( i + 1 ) < N && arr [ i ] == arr [ i + 1 ] ) { cnt ++ ; i ++ ; } if ( cnt > ( N / K ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } i ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = arr . length ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; } }
Sort M elements of given circular array starting from index K | Java program for the above approach ; Function to print the circular array ; Print the array ; Function to sort m elements of diven circular array starting from index k ; Traverse M elements ; Iterate from index k to k + m - 1 ; Check if the next element in the circular array is less than the current element ; Swap current element with the next element ; Print the circular array ; Driver Code ; Function Call
class GFG { static void printCircularArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void sortCircularArray ( int arr [ ] , int n , int k , int m ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = k ; j < k + m - 1 ; j ++ ) { if ( arr [ j % n ] > arr [ ( j + 1 ) % n ] ) { int t = arr [ j % n ] ; arr [ j % n ] = arr [ ( j + 1 ) % n ] ; arr [ ( j + 1 ) % n ] = t ; } } } printCircularArray ( arr , n ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 6 , 5 , 3 } ; int K = 2 , M = 3 ; int N = arr . length ; sortCircularArray ( arr , N , K , M ) ; } }
Minimize cost required to complete all processes | Java program for the above approach ; Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver code ; Function Call
import java . util . * ; class GFG { static int minimumCostReqToCompthePrcess ( int [ ] [ ] arr ) { Arrays . sort ( arr , ( a , b ) -> b [ 1 ] - a [ 1 ] ) ; int n = arr . length ; int minCost = 0 ; int minCostInit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] [ 1 ] > minCostInit ) { minCost += ( arr [ i ] [ 1 ] - minCostInit ) ; minCostInit = arr [ i ] [ 1 ] ; } minCostInit -= arr [ i ] [ 0 ] ; } return minCost ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 2 } , { 2 , 4 } , { 4 , 8 } } ; System . out . println ( minimumCostReqToCompthePrcess ( arr ) ) ; } }
Split array into equal length subsets with maximum sum of Kth largest element of each subset | Java program to implement the above approach ; Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver code
import java . util . * ; import java . util . Collections ; class GFG { static int maximumKthLargestsumPart ( int [ ] arr , int N , int M , int K ) { int maxSum = 0 ; if ( N % M != 0 ) return - 1 ; int sz = ( N / M ) ; if ( K > sz ) return - 1 ; Arrays . sort ( arr ) ; int i , k , t ; for ( i = 0 ; i < N / 2 ; i ++ ) { t = arr [ i ] ; arr [ i ] = arr [ N - i - 1 ] ; arr [ N - i - 1 ] = t ; } for ( i = 1 ; i <= M ; i ++ ) { maxSum += arr [ i * K - 1 ] ; } return maxSum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int M = 2 ; int K = 1 ; int N = arr . length ; System . out . println ( maximumKthLargestsumPart ( arr , N , M , K ) ) ; } }