codeparrot/xlcost-text-to-code huggingface dataset:
Maximize difference between the sum of absolute differences of each element with the remaining array | Java program for the above approach ; Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Print the result ; Driven Code
import java . util . * ; class GFG { static void findMaxDifference ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int Leftsum = 0 ; int Totalsum = 0 ; int Min = Integer . MAX_VALUE , Max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int leftNumbers = i ; int rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; int sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Min = Math . min ( Min , sum ) ; Max = Math . max ( Max , sum ) ; Leftsum += arr [ i ] ; } System . out . print ( Max - Min ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 7 } ; int N = arr . length ; findMaxDifference ( arr , N ) ; } }
Minimum pairs required to be removed such that the array does not contain any pair with sum K | Java program for the above approach ; Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
import java . util . * ; class GFG { static int maxcntPairsSumKRemoved ( int [ ] arr , int k ) { int cntPairs = 0 ; if ( arr . length <= 1 ) return cntPairs ; Arrays . sort ( arr ) ; int left = 0 ; int right = arr . length - 1 ; while ( left < right ) { int s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; System . out . println ( maxcntPairsSumKRemoved ( arr , K ) ) ; } }
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | Java program to implement the above approach ; reverses an array ; Function to find the minimum cost to split array into K subsets ; Sort the array in descending order ; Stores minimum cost to split the array into K subsets ; Stores position of elements of a subset ; Iterate over the range [ 1 , N ] ; Calculate the cost to select X - th element of every subset ; Update min_cost ; Update X ; Driver code ; Function call
import java . util . * ; class GFG { static void reverse ( int a [ ] , int n ) { int i , k , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int getMinCost ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; reverse ( arr , n ) ; int min_cost = 0 ; int X = 0 ; for ( int i = 0 ; i < n ; i += k ) { for ( int j = i ; j < i + k && j < n ; j ++ ) { min_cost += arr [ j ] * ( X + 1 ) ; } X ++ ; } return min_cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 20 , 7 , 8 } ; int K = 2 ; int N = arr . length ; System . out . println ( getMinCost ( arr , N , K ) ) ; } }
Minimize difference between the largest and smallest array elements by K replacements | Java program for the above approach ; Function to find minimum difference between largest and smallest element after K replacements ; Sort array in ascending order ; Length of array ; Minimum difference ; Check for all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Prints the minimum possible difference
import java . util . * ; class GFG { static int minDiff ( int [ ] A , int K ) { Arrays . sort ( A ) ; int n = A . length ; if ( n <= K ) return 0 ; int mindiff = A [ n - 1 ] - A [ 0 ] ; if ( K == 0 ) return mindiff ; for ( int i = 0 , j = n - 1 - K ; j < n ; ) { mindiff = Math . min ( mindiff , A [ j ] - A [ i ] ) ; i ++ ; j ++ ; } return mindiff ; } public static void main ( String [ ] args ) { int A [ ] = { - 1 , 3 , - 1 , 8 , 5 , 4 } ; int K = 3 ; System . out . println ( minDiff ( A , K ) ) ; } }
Minimize difference between the largest and smallest array elements by K replacements | Java program for above approach ; Function to find minimum difference between the largest and smallest element after K replacements ; Create a MaxHeap ; Create a MinHeap ; Update maxHeap and MinHeap with highest and smallest K elements respectively ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Store all max element from maxHeap ; Store all min element from minHeap ; Generating all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Function call
import java . util . * ; import java . lang . * ; class GFG { static int minDiff ( int [ ] A , int K ) { if ( A . length <= K + 1 ) return 0 ; PriorityQueue < Integer > maxHeap = new PriorityQueue < > ( ) ; PriorityQueue < Integer > minHeap = new PriorityQueue < > ( Collections . reverseOrder ( ) ) ; for ( int n : A ) { maxHeap . add ( n ) ; if ( maxHeap . size ( ) > K + 1 ) maxHeap . poll ( ) ; minHeap . add ( n ) ; if ( minHeap . size ( ) > K + 1 ) minHeap . poll ( ) ; } List < Integer > maxList = new ArrayList < > ( ) ; while ( maxHeap . size ( ) > 0 ) maxList . add ( maxHeap . poll ( ) ) ; List < Integer > minList = new ArrayList < > ( ) ; while ( minHeap . size ( ) > 0 ) minList . add ( minHeap . poll ( ) ) ; int mindiff = Integer . MAX_VALUE ; for ( int i = 0 ; i <= K ; i ++ ) { mindiff = Math . min ( mindiff , maxList . get ( i ) - minList . get ( K - i ) ) ; } return mindiff ; } public static void main ( String [ ] args ) { int A [ ] = { - 1 , 3 , - 1 , 8 , 5 , 4 } ; int K = 3 ; System . out . println ( minDiff ( A , K ) ) ; } }
Check if all K | Java program to implement the above approach ; Function reverses the elements of the array ; Function to check all subset - sums of K - length subsets in A [ ] is greater that that in the array B [ ] or not ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of A [ ] ; Stores sum of first K elements of B [ ] ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
import java . util . * ; class GFG { static void reverse ( int myArray [ ] ) { Collections . reverse ( Arrays . asList ( myArray ) ) ; } static boolean checkSubsetSum ( int A [ ] , int B [ ] , int N , int K ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; reverse ( B ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum1 += A [ i ] ; sum2 += B [ i ] ; } if ( sum1 > sum2 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int A [ ] = { 12 , 11 , 10 , 13 } ; int B [ ] = { 7 , 10 , 6 , 2 } ; int N = A . length ; int K = 3 ; if ( checkSubsetSum ( A , B , N , K ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }
Sort given array to descending | Java program to implement the above approach ; Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Sort last ( N - K ) array elements in ascending order ; Print array elements ; Driver Code
import java . util . * ; class GFG { static void sortArrayInDescAsc ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; for ( int i = 0 , j = N - 1 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } Arrays . sort ( arr , K , N ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 } ; int N = arr . length ; int K = 6 ; sortArrayInDescAsc ( arr , N , K ) ; } }
Median of all non | Java program to implement the above approach ; Function to calculate the median of all possible subsets by given operations ; Stores sum of elements of arr [ ] ; Traverse the array arr [ ] ; Update sum ; Sort the array ; DP [ i ] [ j ] : Stores total number of ways to form the sum j by either selecting ith element or not selecting ith item . ; Initialize all the DP states ; Base case ; Fill dp [ i ] [ 0 ] ; Base case ; Fill all the DP states based on the mentioned DP relation ; If j is greater than or equal to arr [ i ] ; Update dp [ i ] [ j ] ; Update dp [ i ] [ j ] ; Stores all possible subset sum ; Traverse all possible subset sum ; Stores count of subsets whose sum is j ; Itearate over the range [ 1 , M ] ; Insert j into sumSub ; Stores middle element of sumSub ; Driver Code
import java . util . * ; class GFG { static int findMedianOfsubSum ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } Arrays . sort ( arr ) ; int [ ] [ ] dp = new int [ N ] [ sum + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < sum + 1 ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } dp [ 0 ] [ arr [ 0 ] ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j >= arr [ i ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } Vector < Integer > sumSub = new Vector < Integer > ( ) ; for ( int j = 1 ; j <= sum ; j ++ ) { int M = dp [ N - 1 ] [ j ] ; for ( int i = 1 ; i <= M ; i ++ ) { sumSub . add ( j ) ; } } int mid = sumSub . get ( sumSub . size ( ) / 2 ) ; return mid ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 3 } ; int N = arr . length ; System . out . print ( findMedianOfsubSum ( arr , N ) ) ; } }
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Java program for the above approach ; Function to print the index from where the operation can be started ; Initialize B [ ] ; Initialize save ; Make B [ ] equals to arr [ ] ; Sort the array B [ ] ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array arr [ ] ; Length of array ; Function Call
import java . util . * ; class GFG { static void printIndex ( int arr [ ] , int N ) { int [ ] B = new int [ N ] ; int save = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } Arrays . sort ( B ) ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { System . out . print ( - 1 + "NEW_LINE"); return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { System . out . print ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { System . out . print ( N ) ; } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { System . out . print ( i + 1 ) ; break ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 4 , 4 , 5 } ; int N = arr . length ; printIndex ( arr , N ) ; } }
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 ; Generate all subsequence of length N - 1 ; Store main value of String str ; Sort the vector ; Print first element of vector ; Driver Code ; Given String S ; Function Call
import java . util . * ; class GFG { static void firstSubsequence ( String s ) { Vector < String > allsubseq = new Vector < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { String k = " " ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( i != j ) { k += s . charAt ( j ) ; } } allsubseq . add ( k ) ; } Collections . sort ( allsubseq ) ; System . out . print ( allsubseq . get ( 0 ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; firstSubsequence ( S ) ; } }
Maximum even sum subsequence of length K | Java program to implement the above approach ; Function to find the maximum even sum of any subsequence of length K ; If count of elements is less than K ; Stores maximum even subsequence sum ; Stores Even numbers ; Stores Odd numbers ; Traverse the array ; If current element is an odd number ; Insert odd number ; Insert even numbers ; Sort Odd [ ] array ; Sort Even [ ] array ; Stores current index Of Even [ ] array ; Stores current index Of Odd [ ] array ; If K is odd ; If count of elements in Even [ ] >= 1 ; Update maxSum ; Update i ; If count of elements in Even [ ] array is 0. ; Update K ; If count of elements in Even [ ] and odd [ ] >= 2 ; Update maxSum ; Update j ; Update maxSum ; Update i ; Update K ; If count of elements in Even [ ] array >= 2. ; Update maxSum ; Update i ; Update K ; If count of elements in Odd [ ] array >= 1 ; Update maxSum ; Update i . ; Update K . ; Driver code
import java . util . * ; class GFG { static int evenSumK ( int arr [ ] , int N , int K ) { if ( K > N ) { return - 1 ; } int maxSum = 0 ; ArrayList < Integer > Even = new ArrayList < Integer > ( ) ; ArrayList < Integer > Odd = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { Odd . add ( arr [ i ] ) ; } else { Even . add ( arr [ i ] ) ; } } Collections . sort ( Odd ) ; Collections . sort ( Even ) ; int i = Even . size ( ) - 1 ; int j = Odd . size ( ) - 1 ; while ( K > 0 ) { if ( K % 2 == 1 ) { if ( i >= 0 ) { maxSum += Even . get ( i ) ; i -- ; } else { return - 1 ; } K -- ; } else if ( i >= 1 && j >= 1 ) { if ( Even . get ( i ) + Even . get ( i - 1 ) <= Odd . get ( j ) + Odd . get ( j - 1 ) ) { maxSum += Odd . get ( j ) + Odd . get ( j - 1 ) ; j -= 2 ; } else { maxSum += Even . get ( i ) + Even . get ( i - 1 ) ; i -= 2 ; } K -= 2 ; } else if ( i >= 1 ) { maxSum += Even . get ( i ) + Even . get ( i - 1 ) ; i -= 2 ; K -= 2 ; } else if ( j >= 1 ) { maxSum += Odd . get ( j ) + Odd . get ( j - 1 ) ; j -= 2 ; K -= 2 ; } } return maxSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 10 , 3 , 5 } ; int N = arr . length ; int K = 3 ; System . out . println ( evenSumK ( arr , N , K ) ) ; } }
Program to find weighted median of a given array | Java program for the above approach ; Function to calculate weighted median ; Store pr of arr [ i ] and W [ i ] ; Sort the list of pr w . r . t . to their arr [ ] values ; If N is odd ; Traverse the set pr from left to right ; Update sums ; If sum becomes > 0.5 ; If N is even ; For lower median traverse the set pr from left ; Update sums ; When sum >= 0.5 ; For upper median traverse the set pr from right ; Update sums ; When sum >= 0.5 ; Driver Code ; Given array arr [ ] ; Given weights W [ ] ; Function Call
import java . util . * ; class GFG { static class Pair implements Comparable < Pair > { int first ; double second ; Pair ( int f , double s ) { first = f ; second = s ; } @ Override public int compareTo ( Pair o ) { if ( this . second > o . second ) return 1 ; else if ( this . second == o . second ) return 0 ; return - 1 ; } } static void weightedMedian ( Vector < Integer > arr , Vector < Double > W ) { Vector < Pair > pr = new Vector < > ( ) ; for ( int index = 0 ; index < arr . size ( ) ; index ++ ) pr . add ( new Pair ( arr . get ( index ) , W . get ( index ) ) ) ; Collections . sort ( pr ) ; if ( arr . size ( ) % 2 != 0 ) { float sums = 0 ; for ( Pair element : pr ) { sums += element . second ; if ( sums > 0.5 ) System . out . print ( " The ▁ Weighted ▁ Median ▁ is ▁ element ▁ " + element . first + "NEW_LINE"); } } else { double sums = 0 ; for ( Pair element : pr ) { sums += element . second ; if ( sums <= 0.5 ) { System . out . print ( " Lower ▁ Weighted ▁ Median ▁ is ▁ element ▁ " + element . first + "NEW_LINE"); break ; } } sums = 0 ; for ( int index = pr . size ( ) - 1 ; index >= 0 ; index -- ) { int element = pr . get ( index ) . first ; double weight = pr . get ( index ) . second ; sums += weight ; if ( sums >= 0.5 ) { System . out . print ( " Upper ▁ Weighted ▁ Median ▁ is ▁ element ▁ " + element ) ; break ; } } } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < > ( ) ; arr . add ( 4 ) ; arr . add ( 1 ) ; arr . add ( 3 ) ; arr . add ( 2 ) ; Vector < Double > W = new Vector < > ( ) ; W . add ( 0.25 ) ; W . add ( 0.49 ) ; W . add ( 0.25 ) ; W . add ( 0.01 ) ; weightedMedian ( arr , W ) ; } }
Kth smallest element from an array of intervals | Java program to implement the above approach ; Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Driver Code ; Intervals given ; Size of the arr
import java . util . * ; import java . io . * ; class GFG { public static int KthSmallestNum ( int arr [ ] [ ] , int n , int k ) { PriorityQueue < int [ ] > pq = new PriorityQueue < > ( ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . add ( new int [ ] { arr [ i ] [ 0 ] , arr [ i ] [ 1 ] } ) ; } int cnt = 1 ; while ( cnt < k ) { int [ ] interval = pq . poll ( ) ; if ( interval [ 0 ] < interval [ 1 ] ) { pq . add ( new int [ ] { interval [ 0 ] + 1 , interval [ 1 ] } ) ; } cnt ++ ; } return pq . peek ( ) [ 0 ] ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 5 , 11 } , { 10 , 15 } , { 12 , 20 } } ; int n = arr . length ; int k = 12 ; System . out . println ( KthSmallestNum ( arr , n , k ) ) ; } }
Maximum Manhattan distance between a distinct pair from N coordinates | Java program for the above approach ; Pair class ; Function to calculate the maximum Manhattan distance ; Stores the maximum distance ; Find Manhattan distance using the formula | x1 - x2 | + | y1 - y2 | ; Updating the maximum ; Driver Code ; Given Co - ordinates ; Function call
import java . io . * ; import java . util . * ; class GFG { public static class Pair { int x ; int y ; Pair ( int x , int y ) { this . x = x ; this . y = y ; } } static void MaxDist ( ArrayList < Pair > A , int N ) { int maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { sum = Math . abs ( A . get ( i ) . x - A . get ( j ) . x ) + Math . abs ( A . get ( i ) . y - A . get ( j ) . y ) ; maximum = Math . max ( maximum , sum ) ; } } System . out . println ( maximum ) ; } public static void main ( String [ ] args ) { int n = 3 ; ArrayList < Pair > al = new ArrayList < > ( ) ; Pair p1 = new Pair ( 1 , 2 ) ; al . add ( p1 ) ; Pair p2 = new Pair ( 2 , 3 ) ; al . add ( p2 ) ; Pair p3 = new Pair ( 3 , 4 ) ; al . add ( p3 ) ; MaxDist ( al , n ) ; } }
Longest increasing subsequence which forms a subarray in the sorted representation of the array | Java program to implement the above approach ; Function to find the length of the longest increasing sorted sequence ; Stores the count of all elements ; Store the original array ; Sort the array ; If adjacent element are not same ; Increment count ; Store frequency of each element ; Initialize a DP array ; Iterate over the array ar [ ] ; Length of the longest increasing sorted sequence ; Iterate over the array ; Current element ; If the element has been encountered the first time ; If all the x - 1 previous elements have already appeared ; Otherwise ; If all x - 1 elements have already been encountered ; Increment the count of the current element ; Update maximum subsequence size ; Return the maximum subsequence size ; Driver code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int LongestSequence ( int a [ ] , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; int ar [ ] = new int [ n + 1 ] ; int i = 0 ; int j = 0 ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = a [ i - 1 ] ; } Arrays . sort ( a ) ; int c = 1 ; m . put ( a [ 0 ] , c ) ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { c ++ ; m . put ( a [ i ] , c ) ; } } HashMap < Integer , Integer > cnt = new HashMap < > ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ 3 ] ; cnt . put ( 0 , 0 ) ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = m . get ( ar [ i ] ) ; if ( cnt . containsKey ( ar [ i ] ) ) cnt . put ( ar [ i ] , cnt . get ( ar [ i ] ) + 1 ) ; else cnt . put ( ar [ i ] , 1 ) ; } int ans = 0 , x ; for ( i = 1 ; i <= n ; i ++ ) { x = ar [ i ] ; if ( dp [ x ] [ 0 ] == 0 ) { if ( dp [ x - 1 ] [ 0 ] == cnt . get ( x - 1 ) ) { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 1 ] ; dp [ x ] [ 2 ] = dp [ x - 1 ] [ 1 ] ; } else { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 0 ] ; } } dp [ x ] [ 2 ] = Math . max ( dp [ x - 1 ] [ 0 ] , dp [ x ] [ 2 ] ) ; if ( dp [ x - 1 ] [ 0 ] == cnt . get ( x - 1 ) ) { dp [ x ] [ 2 ] = Math . max ( dp [ x ] [ 2 ] , dp [ x - 1 ] [ 1 ] ) ; } for ( j = 0 ; j < 3 ; j ++ ) { dp [ x ] [ j ] ++ ; ans = Math . max ( ans , dp [ x ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 4 , 8 , 2 , 9 } ; int n = arr . length ; System . out . println ( LongestSequence ( arr , n ) ) ; } } System . out . println ( LongestSequence ( arr , n ) ) ; }
Maximize the sum of Kth column of a Matrix | Java program to implement the above approach ; Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code
class GFG { static int [ ] [ ] findMatrix ( int N , int K ) { int [ ] [ ] mat = new int [ N ] [ N ] ; int element = 1 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < K - 1 ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = K - 1 ; j < N ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } return mat ; } static void printMatrix ( int [ ] [ ] mat , int N ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; int [ ] [ ] mat = findMatrix ( N , K ) ; printMatrix ( mat , N ) ; } }