codeparrot/xlcost-text-to-code huggingface dataset:
Print the indices for every row of a grid from which escaping from the grid is possible | Java program for the above approach ; Function to find the row index for every row of a matrix from which one can exit the grid after entering from left ; Iterate over the range [ 0 , M - 1 ] ; Stores the direction from which a person enters a cell ; Row index from which one enters the grid ; Column index from which one enters the grid ; Iterate until j is atleast N - 1 ; If Mat [ i ] [ j ] is equal to 1 ; If entry is from left cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from upper cell ; Decrement j by 1 ; Assign ' R ' to dir ; If entry is from right cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from bottom cell ; Increment j by 1 ; Assign ' L ' to dir ; Otherwise , ; If entry is from left cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from upper cell ; Increment j by 1 ; Assign ' L ' to dir ; If entry is from right cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from lower cell ; Decrement j by 1 ; Assign ' R ' to dir ; If i or j is less than 0 or i is equal to M or j is equal to N ; If j is equal to N ; Otherwise ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static void findPath ( int [ ] [ ] arr , int M , int N ) { for ( int row = 0 ; row < M ; row ++ ) { char dir = ' L ' ; int i = row ; int j = 0 ; while ( j < N ) { if ( arr [ i ] [ j ] == 1 ) { if ( dir == ' L ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' U ' ) { j -- ; dir = ' R ' ; } else if ( dir == ' R ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' D ' ) { j ++ ; dir = ' L ' ; } } else { if ( dir == ' L ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' U ' ) { j ++ ; dir = ' L ' ; } else if ( dir == ' R ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' D ' ) { j -- ; dir = ' R ' ; } } if ( i < 0 i == M j < 0 j == N ) break ; } if ( j == N ) System . out . print ( i + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; int M = arr . length ; int N = arr [ 0 ] . length ; findPath ( arr , M , N ) ; } }
Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | Java approach for the above approach ; Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
class GFG { static int getSum ( int n ) { int ans = 0 ; char [ ] arr = ( String . valueOf ( n ) ) . toCharArray ( ) ; for ( char ch : arr ) { ans += Character . getNumericValue ( ch ) ; } return ans ; } static void getMax ( int [ ] arr ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; for ( int i : arr ) { int digitSum = getSum ( i ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . max ( maxRem0 , i ) ; else if ( digitSum % 3 == 1 ) rem1 += i ; else rem2 += i ; } System . out . print ( maxRem0 + Math . max ( rem1 , rem2 ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 23 , 12 , 43 , 3 , 56 } ; getMax ( arr ) ; } }
Minimum edge reversals to make a root | Java program to find min edge reversal to make every node reachable from root ; pair class ; method to dfs in tree and populates disRev values ; visit current node ; looping over all neighbors ; distance of v will be one more than distance of u ; initialize back edge count same as parent node 's count ; if there is a reverse edge from u to i , then only update ; return total reversal in subtree rooted at u ; method prints root and minimum number of edge reversal ; number of nodes are one more than number of edges ; data structure to store directed tree ; disRev stores two values - distance and back edge count from root node ; add 0 weight in direction of u to v ; add 1 weight in reverse direction ; initialize all variables ; dfs populates disRev data structure and store total reverse edge counts ; for ( int i = 0 ; i < V ; i ++ ) { cout << i << " ▁ : ▁ " << disRev [ i ] . first << " ▁ " << disRev [ i ] . second << endl ; } ; loop over all nodes to choose minimum edge reversal ; ( reversal in path to i ) + ( reversal in all other tree parts ) ; choose minimum among all values ; print the designated root and total edge reversal made ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; pair ( int a , int b ) { first = a ; second = b ; } } static int dfs ( Vector < Vector < pair > > g , pair disRev [ ] , boolean visit [ ] , int u ) { visit [ u ] = true ; int totalRev = 0 ; for ( int i = 0 ; i < g . get ( u ) . size ( ) ; i ++ ) { int v = g . get ( u ) . get ( i ) . first ; if ( ! visit [ v ] ) { disRev [ v ] . first = disRev [ u ] . first + 1 ; disRev [ v ] . second = disRev [ u ] . second ; if ( g . get ( u ) . get ( i ) . second != 0 ) { disRev [ v ] . second = disRev [ u ] . second + 1 ; totalRev ++ ; } totalRev += dfs ( g , disRev , visit , v ) ; } } return totalRev ; } static void printMinEdgeReverseForRootNode ( int edges [ ] [ ] , int e ) { int V = e + 1 ; Vector < Vector < pair > > g = new Vector < Vector < pair > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) g . add ( new Vector < pair > ( ) ) ; pair disRev [ ] = new pair [ V ] ; for ( int i = 0 ; i < V ; i ++ ) disRev [ i ] = new pair ( 0 , 0 ) ; boolean visit [ ] = new boolean [ V ] ; int u , v ; for ( int i = 0 ; i < e ; i ++ ) { u = edges [ i ] [ 0 ] ; v = edges [ i ] [ 1 ] ; g . get ( u ) . add ( new pair ( v , 0 ) ) ; g . get ( v ) . add ( new pair ( u , 1 ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { visit [ i ] = false ; disRev [ i ] . first = disRev [ i ] . second = 0 ; } int root = 0 ; int totalRev = dfs ( g , disRev , visit , root ) ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < V ; i ++ ) { int edgesToRev = ( totalRev - disRev [ i ] . second ) + ( disRev [ i ] . first - disRev [ i ] . second ) ; if ( edgesToRev < res ) { res = edgesToRev ; root = i ; } } System . out . println ( root + " ▁ " + res ) ; } public static void main ( String args [ ] ) { int edges [ ] [ ] = { { 0 , 1 } , { 2 , 1 } , { 3 , 2 } , { 3 , 4 } , { 5 , 4 } , { 5 , 6 } , { 7 , 6 } } ; int e = edges . length ; printMinEdgeReverseForRootNode ( edges , e ) ; } }
Check if 2 * K + 1 non | Java program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void checkString ( String s , int k ) { int n = s . length ( ) ; if ( 2 * k + 1 > n ) { System . out . println ( " No " ) ; return ; } String a = s . substring ( 0 , k ) ; String b = s . substring ( n - k , n ) ; StringBuffer str = new StringBuffer ( b ) ; str . reverse ( ) ; b = str . toString ( ) ; if ( a . equals ( b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; } }
Minimum number of socks required to picked to have at least K pairs of the same color | Java program for the above approach ; Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code
import java . io . * ; class GFG { public static int findMin ( int [ ] arr , int N , int k ) { int pairs = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return - 1 ; else return 2 * k + N - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 6 } ; int K = 3 ; int N = arr . length ; System . out . println ( findMin ( arr , N , K ) ) ; } }
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | Java program for the above approach ; Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minimumFlips ( int A , int B , int C ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int x = 0 , y = 0 , z = 0 ; if ( ( A & ( 1 << i ) ) != 0 ) { x = 1 ; } if ( ( B & ( 1 << i ) ) != 0 ) { y = 1 ; } if ( ( C & ( 1 << i ) ) != 0 ) { z = 1 ; } if ( z == 0 ) { if ( x == 1 ) { res ++ ; } if ( y == 1 ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int A = 2 , B = 6 , C = 5 ; System . out . println ( minimumFlips ( A , B , C ) ) ; } }
Queries to calculate Bitwise AND of an array with updates | Java program for the above approach ; Store the number of set bits at each position ; Function to precompute the prefix count array ; Iterate over the range [ 0 , 31 ] ; Set the bit at position i if arr [ 0 ] is set at position i ; Traverse the array and take prefix sum ; Update prefixCount [ i ] [ j ] ; Function to find the Bitwise AND of all array elements ; Stores the required result ; Iterate over the range [ 0 , 31 ] ; Stores the number of set bits at position i ; If temp is N , then set ith position in the result ; Print the result ; Function to update the prefix count array in each query ; Iterate through all the bits of the current number ; Store the bit at position i in the current value and the new value ; If bit2 is set and bit1 is unset , then increase the set bits at position i by 1 ; If bit1 is set and bit2 is unset , then decrease the set bits at position i by 1 ; Function to find the bitwise AND of the array after each query ; Fill the prefix count array ; Traverse the queries ; Store the index and the new value ; Store the current element at the index ; Update the array element ; Apply the changes to the prefix count array ; Print the bitwise AND of the modified array ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int prefixCount [ ] [ ] ; static void findPrefixCount ( int arr [ ] , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { prefixCount [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < size ; j ++ ) { prefixCount [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefixCount [ i ] [ j ] += prefixCount [ i ] [ j - 1 ] ; } } } static void arrayBitwiseAND ( int size ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int temp = prefixCount [ i ] [ size - 1 ] ; if ( temp == size ) result = ( result | ( 1 << i ) ) ; } System . out . print ( result + " ▁ " ) ; } static void applyQuery ( int currentVal , int newVal , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { int bit1 = ( ( currentVal >> i ) & 1 ) ; int bit2 = ( ( newVal >> i ) & 1 ) ; if ( bit2 > 0 && bit1 == 0 ) prefixCount [ i ] [ size - 1 ] ++ ; else if ( bit1 > 0 && bit2 == 0 ) prefixCount [ i ] [ size - 1 ] -- ; } } static void findbitwiseAND ( int queries [ ] [ ] , int arr [ ] , int N , int M ) { prefixCount = new int [ 32 ] [ 10000 ] ; findPrefixCount ( arr , N ) ; for ( int i = 0 ; i < M ; i ++ ) { int id = queries [ i ] [ 0 ] ; int newVal = queries [ i ] [ 1 ] ; int currentVal = arr [ id ] ; arr [ id ] = newVal ; applyQuery ( currentVal , newVal , N ) ; arrayBitwiseAND ( N ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int queries [ ] [ ] = { { 0 , 2 } , { 3 , 3 } , { 4 , 2 } } ; int N = arr . length ; int M = queries . length ; findbitwiseAND ( queries , arr , N , M ) ; } }
Find the winner of a game of donating i candies in every i | Java Program for above approach ; Function to find the winning player in a game of donating i candies to opponent in i - th move ; Steps in which number of candies of player A finishes ; Steps in which number of candies of player B finishes ; If A 's candies finishes first ; Otherwise ; Driver code ; Candies possessed by player A ; Candies possessed by player B
class GFG { static void stepscount ( int a , int b ) { int chanceA = 2 * a - 1 ; int chanceB = 2 * b ; if ( chanceA < chanceB ) { System . out . print ( " B " ) ; } else if ( chanceB < chanceA ) { System . out . print ( " A " ) ; } } public static void main ( String [ ] args ) { int A = 2 ; int B = 3 ; stepscount ( A , B ) ; } }
Check if a point is inside , outside or on a Hyperbola | Java program for the above approach ; Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( int ) ( Math . pow ( ( x - h ) , 2 ) / Math . pow ( a , 2 ) ) - ( int ) ( Math . pow ( ( y - k ) , 2 ) / Math . pow ( b , 2 ) ) ; if ( p > 1 ) { System . out . println ( " Outside " ) ; } else if ( p == 1 ) { System . out . println ( " On ▁ the ▁ Hyperbola " ) ; } else { System . out . println ( " Inside " ) ; } } public static void main ( String [ ] args ) { int h = 0 , k = 0 , x = 2 ; int y = 1 , a = 4 , b = 5 ; checkpoint ( h , k , x , y , a , b ) ; } }
Count subarrays having even Bitwise XOR | Java program for the above approach ; Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Driver Code ; Given array ; Stores the size of the array ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void evenXorSubarray ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int XOR = 0 ; for ( int j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; evenXorSubarray ( arr , N ) ; } }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Java program for the above approach ; Function to find the Kth element in the required permutation ; Store the required result ; If K is in the first N / 2 elements , print K * 2 ; Otherwise , K is greater than N / 2 ; If N is even ; If N is odd ; Print the required result ; Driver code ; functions call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findKthElement ( int N , int K ) { int ans = 0 ; if ( K <= N / 2 ) { ans = K * 2 ; } else { if ( N % 2 == 0 ) { ans = ( K * 2 ) - N - 1 ; } else { ans = ( K * 2 ) - N ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; } }
Minimize replacements with values up to K to make sum of two given arrays equal | Java program for the above approach ; Function to find minimum number of replacements required to make the sum of two arrays equal ; Stores the sum of a [ ] ; Stores the sum of b [ ] ; Calculate sum of the array a [ ] ; Calculate sum of the array b [ ] ; Stores the difference between a [ ] and b [ ] ; Left and Right pointer to traverse the array a [ ] ; Left and Right pointer to traverse the array b [ ] ; Stores the count of moves ; Sort the arrays in ascending order ; Iterate while diff != 0 and l1 <= r1 or l2 <= r2 ; If diff is greater than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; If diff is less than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; Increment count of res by one ; If diff is 0 , then return res ; Otherwise , return - 1 ; Driver Code
import java . util . * ; class GFG { public static int makeSumEqual ( int [ ] a , int [ ] b , int K , int M , int N ) { int sum1 = 0 ; int sum2 = 0 ; for ( int el : a ) sum1 += el ; for ( int el : b ) sum2 += el ; int diff = sum1 - sum2 ; int l1 = 0 , r1 = M - 1 ; int l2 = 0 , r2 = N - 1 ; int res = 0 ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; while ( l1 <= r1 l2 <= r2 ) { if ( diff == 0 ) { break ; } if ( diff > 0 ) { if ( l2 <= r2 && l1 <= r1 ) { if ( K - b [ l2 ] < a [ r1 ] - 1 ) { int sub = Math . min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else if ( l1 <= r1 ) { int sub = Math . min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else { if ( l1 <= r1 && l2 <= r2 ) { if ( K - a [ l1 ] < b [ r2 ] - 1 ) { int sub = Math . min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . min ( K - a [ l1 ] , - 1 * diff ) ; diff += sub ; a [ l1 ] -= sub ; l1 ++ ; } } else if ( l2 <= r2 ) { int sub = Math . min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . min ( K - a [ l1 ] , diff ) ; diff += sub ; a [ l1 ] += sub ; l1 ++ ; } } res ++ ; } if ( diff == 0 ) return res ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 4 , 3 } ; int [ ] B = { 6 , 6 , 6 } ; int M = A . length ; int N = B . length ; int K = 6 ; System . out . println ( makeSumEqual ( A , B , K , M , N ) ) ; } }
Modify a Binary String by flipping characters such that any pair of indices consisting of 1 s are neither co | Java program for the above approach ; Function to modify a string such that there doesn 't exist any pair  of indices consisting of 1s, whose  GCD is 1 and are divisible by each other ; Flips characters at indices 4 N , 4 N - 2 , 4 N - 4 . ... upto N terms ; Print the string ; Driver Code ; Initialize the string S ; function call
import java . io . * ; class GFG { public static void findString ( char S [ ] , int N ) { int strLen = 4 * N ; for ( int i = 1 ; i <= N ; i ++ ) { S [ strLen - 1 ] = '1' ; strLen -= 2 ; } System . out . println ( S ) ; } public static void main ( String [ ] args ) { int N = 2 ; char S [ ] = new char [ 4 * N ] ; for ( int i = 0 ; i < 4 * N ; i ++ ) S [ i ] = '0' ; findString ( S , N ) ; } }
Check if a string can be split into two substrings with equal number of vowels | Java program for the above approach ; Function to check if any character is a vowel or not ; Lowercase vowels ; Uppercase vowels ; Otherwise ; Function to check if string S can be split into two substrings with equal number of vowels ; Stores the count of vowels in the string S ; Traverse over the string ; If S [ i ] is vowel ; Stores the count of vowels upto the current index ; Traverse over the string ; If S [ i ] is vowel ; If vowelsTillNow and totalVowels are equal ; Otherwise ; Driver Code
import java . io . * ; class GFG { public static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; if ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) return true ; return false ; } public static String containsEqualStrings ( String S ) { int totalVowels = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( isVowel ( S . charAt ( i ) ) ) totalVowels ++ ; } int vowelsTillNow = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( isVowel ( S . charAt ( i ) ) ) { vowelsTillNow ++ ; totalVowels -- ; if ( vowelsTillNow == totalVowels ) { return " Yes " ; } } } return " No " ; } public static void main ( String [ ] args ) { String S = " geeks " ; System . out . println ( containsEqualStrings ( S ) ) ; } }
Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | Java program to implement the above approach ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Driver Code ; Given matrix ; Dimensions of matrix
import java . io . * ; class GFG { static void getMaxSum ( int [ ] [ ] A , int M , int N ) { int sum = 0 ; int negative = 0 ; int minVal = Integer . MAX_VALUE ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum += Math . abs ( A [ i ] [ j ] ) ; if ( A [ i ] [ j ] < 0 ) { negative ++ ; } minVal = Math . min ( minVal , Math . abs ( A [ i ] [ j ] ) ) ; } } if ( negative % 2 != 0 ) { sum -= 2 * minVal ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 4 , - 8 , 6 } , { 3 , 7 , 2 } } ; int M = A . length ; int N = A [ 0 ] . length ; getMaxSum ( A , M , N ) ; } }
Calculate total wall area of houses painted | Java code of above approach ; Function to find the total area of walls painted in N row - houses ; Stores total area of N row - houses that needs to be painted ; Traverse the array of wall heights ; Update total area painted ; Update total ; Traverse all the houses and print the shared walls ; Update total ; Print total area needs to paint ; Driver code ; Given N , W & L ; Given heights of houses ; Function Call
import java . util . * ; class GFG { static void areaToPaint ( int N , int W , int L , int Heights [ ] ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += 2 * Heights [ i ] * W ; } total += L * ( Heights [ 0 ] + Heights [ N - 1 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { total += L * Math . abs ( Heights [ i ] - Heights [ i - 1 ] ) ; } System . out . print ( total ) ; } public static void main ( String [ ] args ) { int N = 7 , W = 1 , L = 1 ; int Heights [ ] = { 4 , 3 , 1 , 2 , 3 , 4 , 2 } ; areaToPaint ( N , W , L , Heights ) ; } }
Generate a matrix with each row and column of given sum | Java program for the above approach ; Function to generate a matrix with sum of each row equal to sum of r [ ] and sum of each column equal to sum of c [ ] ; Initialize a matrix ; Traverse each cell ( i , j ) of the matrix ; Assign the minimum of the row or column value ; Subtract the minimum from both row and column sum ; Print the matrix obtained ; Driver Code
import java . io . * ; class GFG { static int [ ] [ ] restoreGem ( int [ ] r , int [ ] c ) { int [ ] [ ] dp = new int [ r . length ] [ c . length ] ; for ( int i = 0 ; i < r . length ; i ++ ) { for ( int j = 0 ; j < c . length ; j ++ ) { int m = Math . min ( r [ i ] , c [ j ] ) ; dp [ i ] [ j ] = m ; r [ i ] -= m ; c [ j ] -= m ; } } return dp ; } static void printMatrix ( int [ ] [ ] ans , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( ans [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] rowSum = { 5 , 7 , 10 } ; int [ ] colSum = { 8 , 6 , 8 } ; int [ ] [ ] ans = restoreGem ( rowSum , colSum ) ; printMatrix ( ans , rowSum . length , colSum . length ) ; } }