codeparrot/xlcost-text-to-code huggingface dataset:
Minimize difference between maximum and minimum array elements by exactly K removals | Java program for the above approach ; Function to minimize the difference of the maximum and minimum array elements by removing K elements ; Base Condition ; Sort the array ; Initialize left and right pointers ; Iterate for K times ; Removing right element to reduce the difference ; Removing the left element to reduce the difference ; Print the minimum difference ; Driver Code ; Function Call
import java . util . * ; class GFG { static void minimumRange ( int arr [ ] , int N , int K ) { if ( K >= N ) { System . out . print ( 0 ) ; return ; } Arrays . sort ( arr ) ; int left = 0 , right = N - 1 , i ; for ( i = 0 ; i < K ; i ++ ) { if ( arr [ right - 1 ] - arr [ left ] < arr [ right ] - arr [ left + 1 ] ) right -- ; else left ++ ; } System . out . print ( arr [ right ] - arr [ left ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 12 , 14 , 21 , 54 , 61 } ; int N = arr . length ; int K = 4 ; minimumRange ( arr , N , K ) ; } }
Split array into K subsets to maximize sum of their second largest elements | Java program for the above approach ; Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Drive Code ; Given array arr [ ] ; Size of array ; Function Call
import java . io . * ; import java . util . Arrays ; class GFG { static void splitArray ( int arr [ ] , int n , int K ) { Arrays . sort ( arr ) ; int i = n - 1 ; int result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } System . out . print ( result ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 5 , 1 , 3 } ; int N = arr . length ; int K = 2 ; splitArray ( arr , N , K ) ; } }
Count subsequences for every array element in which they are the maximum | Java program for the above approach ; Function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; If a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; Else add indices [ j ] ; Add remaining elements ; Add remainging elements ; Recursive function to divide the array into to parts ; Recursive call for elements before mid ; Recursive call for elements after mid ; Merge the two sorted arrays ; Function to find the number of subsequences for each element ; Sorting the indices according to array arr [ ] ; Array to store output numbers ; Initialize subseq ; B [ i ] is 2 ^ i ; Doubling the subsequences ; Print the final output , array B [ ] ; Driver Code ; Given array ; Given length ; Function call
import java . util . * ; class GFG { static void merge ( int [ ] indices , int [ ] a , int l , int mid , int r ) { int [ ] temp_ind = new int [ r - l + 1 ] ; int j = mid + 1 ; int i = 0 , temp_l = l , k ; while ( l <= mid && j <= r ) { if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) temp_ind [ i ++ ] = indices [ l ++ ] ; else temp_ind [ i ++ ] = indices [ j ++ ] ; } while ( l <= mid ) temp_ind [ i ++ ] = indices [ l ++ ] ; while ( j <= r ) temp_ind [ i ++ ] = indices [ j ++ ] ; for ( k = 0 ; k < i ; k ++ ) indices [ temp_l ++ ] = temp_ind [ k ] ; } static void divide ( int [ ] indices , int [ ] a , int l , int r ) { if ( l >= r ) return ; int mid = l / 2 + r / 2 ; divide ( indices , a , l , mid ) ; divide ( indices , a , mid + 1 , r ) ; merge ( indices , a , l , mid , r ) ; } static void noOfSubsequences ( int arr [ ] , int N ) { int [ ] indices = new int [ N ] ; int i ; for ( i = 0 ; i < N ; i ++ ) indices [ i ] = i ; divide ( indices , arr , 0 , N - 1 ) ; int [ ] B = new int [ N ] ; int subseq = 1 ; for ( i = 0 ; i < N ; i ++ ) { B [ indices [ i ] ] = subseq ; subseq *= 2 ; } for ( i = 0 ; i < N ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 } ; int N = arr . length ; noOfSubsequences ( arr , N ) ; } }
Find the order of execution of given N processes in Round Robin Scheduling | Java program for the above approach ; Function to sort the array order [ ] on the basis of the array freq [ ] ; If order [ i ] th is less than order [ temp ] th process ; Otherwise ; Add the left half to tempOrder [ ] ; Add right half to tempOrder [ ] ; Copy the tempOrder [ ] array to order [ ] array ; Utility function to sort the array order [ ] on the basis of freq [ ] ; Base Case ; Divide array into 2 parts ; Sort the left array ; Sort the right array ; Merge the sorted arrays ; Function to find the order of processes in which execution occurs ; Store the frequency ; Find elements in array freq [ ] ; Store the order of completion of processes ; Initialize order [ i ] as i ; Function Call to find the order of execution ; Print order of completion of processes ; Driver Code ; Burst Time of the processes ; Quantum Time ; Function Call
import java . util . * ; class GFG { static void merge ( int order [ ] , int freq [ ] , int i , int mid , int j ) { int tempOrder [ ] = new int [ j - i + 1 ] ; int temp = mid + 1 , index = - 1 ; while ( i <= mid && temp <= j ) { if ( freq [ order [ i ] ] <= freq [ order [ temp ] ] ) { tempOrder [ ++ index ] = order [ i ++ ] ; } else { tempOrder [ ++ index ] = order [ temp ++ ] ; } } while ( i <= mid ) { tempOrder [ ++ index ] = order [ i ++ ] ; } while ( temp <= j ) { tempOrder [ ++ index ] = order [ temp ++ ] ; } int ind = index ; for ( index = ind ; index >= 0 ; index -- ) { order [ j -- ] = tempOrder [ index ] ; } } static void divide ( int order [ ] , int freq [ ] , int i , int j ) { if ( i >= j ) return ; int mid = i / 2 + j / 2 ; divide ( order , freq , i , mid ) ; divide ( order , freq , mid + 1 , j ) ; merge ( order , freq , i , mid , j ) ; } static void orderProcesses ( int A [ ] , int N , int q ) { int i = 0 ; int freq [ ] = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { freq [ i ] = ( A [ i ] / q ) ; if ( A [ i ] % q > 0 ) freq [ i ] += 1 ; } int order [ ] = new int [ 4 ] ; for ( i = 0 ; i < N ; i ++ ) { order [ i ] = i ; } divide ( order , freq , 0 , N - 1 ) ; for ( i = 0 ; i < N ; i ++ ) { System . out . print ( order [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 4 } ; int Q = 3 ; int N = arr . length ; orderProcesses ( arr , N , Q ) ; } }
Maximize cost to empty given array by repetitively removing K array elements | Java program for the above approach ; Function to find the maximum cost to remove all array elements ; Stores maximum cost to remove array elements ; Sort array in ascending order ; Traverse the array ; Update maxCost ; Drive Code
import java . io . * ; import java . util . Arrays ; class GFG { static int maxCostToRemove ( int arr [ ] , int N , int K ) { int maxCost = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; i += K ) { maxCost += arr [ i + 1 ] ; } return maxCost ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 } ; int N = arr . length ; int K = 4 ; System . out . print ( maxCostToRemove ( arr , N , K ) ) ; } }
Print all numbers up to N in words in lexicographical order | Java program for the above approach ; Function to convert a number to words ; Stores the digits ; Base cases ; Stores strings of unit place ; Stores strings for corner cases ; Stores strings for ten 's place digits ; Stores strings for powers of 10 ; If given number contains a single digit ; Iterate over all the digits ; Represent first 2 digits in words ; Represent last 2 digits in words ; Explicitly handle corner cases [ 10 , 19 ] ; Explicitly handle corner case 20 ; For rest of the two digit numbers i . e . , 21 to 99 ; Function to print all the numbers up to n in lexicographical order ; Convert all numbers in words ; Sort all strings ; Print answer ; Driver Code
import java . util . * ; class GFG { static String convert_to_words ( int n ) { char num [ ] = String . valueOf ( n ) . toCharArray ( ) ; int len = num . length ; String ans = " " ; if ( len == 0 ) { ans += " Empty ▁ String " ; return ans ; } String [ ] single_digits = new String [ ] { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String [ ] two_digits = new String [ ] { " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; String [ ] tens_multiple = new String [ ] { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; String [ ] tens_power = new String [ ] { " hundred " , " thousand " } ; if ( len == 1 ) { ans += single_digits [ num [ 0 ] - '0' ] ; return ans ; } int x = 0 ; while ( x < num . length ) { if ( len >= 3 ) { if ( num [ x ] - '0' != 0 ) { ans += single_digits [ num [ x ] - '0' ] ; ans += " ▁ " ; ans += tens_power [ len - 3 ] ; ans += " ▁ " ; } -- len ; } else { if ( num [ x ] - '0' == 1 ) { int sum = num [ x ] - '0' + num [ x ] - '0' ; ans += two_digits [ sum ] ; return ans ; } else if ( num [ x ] - '0' == 2 && num [ x + 1 ] - '0' == 0 ) { ans += " twenty " ; return ans ; } else { int i = ( num [ x ] - '0' ) ; if ( i > 0 ) { ans += tens_multiple [ i ] ; ans += " ▁ " ; } else ans += " " ; ++ x ; if ( num [ x ] - '0' != 0 ) ans += single_digits [ num [ x ] - '0' ] ; } } ++ x ; } return " " ; } static void lexNumbers ( int n ) { Vector < String > s = new Vector < String > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { s . add ( convert_to_words ( i ) ) ; } Collections . sort ( s ) ; Vector < String > ans = new Vector < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ans . add ( s . get ( i ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( ans . get ( i ) + " , ▁ " ) ; System . out . print ( ans . get ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; lexNumbers ( n ) ; } }
Minimize cost to convert all array elements to 0 | Java program for the above approach ; Function to calculate the minimum cost of converting all array elements to 0 s ; Stores subarrays of 0 s only ; Traverse the array ; If consecutive 0 s occur ; Increment if needed ; Push the current length of consecutive 0 s in a vector ; Update lengths as 0 ; Sorting List ; Stores the number of subarrays consisting of 1 s ; Traverse the array ; If current element is 1 ; Otherwise ; Increment count of consecutive ones ; Stores the minimum cost ; Traverse the array ; First element ; Traverse the subarray sizes ; Update cost ; Cost of performing X and Y operations ; Find the minimum cost ; Print the minimum cost ; Driver code ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static void minimumCost ( int [ ] binary , int n , int a , int b ) { List < Integer > groupOfZeros = new ArrayList < Integer > ( ) ; int len = 0 , i = 0 ; boolean increment_need = true ; while ( i < n ) { increment_need = true ; while ( i < n && binary [ i ] == 0 ) { len ++ ; i ++ ; increment_need = false ; } if ( increment_need == true ) { i ++ ; } if ( len != 0 ) { groupOfZeros . add ( len ) ; } len = 0 ; } Collections . sort ( groupOfZeros ) ; i = 0 ; boolean found_ones = false ; int NumOfOnes = 0 ; while ( i < n ) { found_ones = false ; while ( i < n && binary [ i ] == 1 ) { i ++ ; found_ones = true ; } if ( found_ones == false ) i ++ ; else NumOfOnes ++ ; } int ans = Integer . MAX_VALUE ; for ( int i1 = 0 ; i1 < n ; i1 ++ ) { int curr = 0 , totalOnes = NumOfOnes ; if ( i1 == 0 ) { curr = totalOnes * a ; } else { int mark = i1 , num_of_changes = 0 ; for ( int x : groupOfZeros ) { if ( mark >= x ) { totalOnes -- ; mark -= x ; num_of_changes += x ; } else { break ; } } curr = ( num_of_changes * b ) + ( totalOnes * a ) ; } ans = Math . min ( ans , curr ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 1 } ; int N = 6 ; int X = 10 , Y = 4 ; minimumCost ( arr , N , X , Y ) ; } }
Reduce array to longest sorted array possible by removing either half of given array in each operation | Java program to implement the above approach ; Function to check if the subarray arr [ i . . j ] is a sorted subarray or not ; Traverse all elements of the subarray arr [ i ... j ] ; If the previous element of the subarray exceeds current element ; Return 0 ; Return 1 ; Recursively partition the array into two equal halves ; If atmost one element is left in the subarray arr [ i . . j ] ; Checks if subarray arr [ i . . j ] is a sorted subarray or not ; If the subarray arr [ i ... j ] is a sorted subarray ; Stores middle element of the subarray arr [ i . . j ] ; Recursively partition the current subarray arr [ i . . j ] into equal halves ; Driver Code
import java . util . * ; class GFG { static int isSortedparitions ( int arr [ ] , int i , int j ) { for ( int k = i + 1 ; k <= j ; k ++ ) { if ( arr [ k ] < arr [ k - 1 ] ) { return 0 ; } } return 1 ; } static int partitionsArr ( int arr [ ] , int i , int j ) { if ( i >= j ) return 1 ; int flag = ( int ) isSortedparitions ( arr , i , j ) ; if ( flag != 0 ) { return ( j - i + 1 ) ; } int mid = ( i + j ) / 2 ; int X = partitionsArr ( arr , i , mid ) ; int Y = partitionsArr ( arr , mid + 1 , j ) ; return Math . max ( X , Y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 } ; int N = arr . length ; System . out . print ( partitionsArr ( arr , 0 , N - 1 ) ) ; } }
Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array | Java program to implement the above approach ; Function to check if it is possible to merge the two given arrays with given conditions ; Stores index of the array A [ ] ; Stores index of the array B [ ] ; Check if the previous element are from the array A [ ] or from the array B [ ] ; Check if it is possible to merge the two given sorted arrays with given conditions ; Traverse both the arrays ; If A [ i ] is less than B [ j ] and previous element are not from A [ ] ; Update prev ; Update i ; If B [ j ] is less than A [ i ] and previous element are not from B [ ] ; Update prev ; Update j ; If A [ i ] equal to B [ j ] ; If previous element are not from B [ ] ; Update prev ; Update j ; If previous element is not from A [ ] ; Update prev ; Update i ; If it is not possible to merge two sorted arrays with given conditions ; Update flag ; Driver Code
import java . io . * ; class GFG { static boolean checkIfPossibleMerge ( int [ ] A , int [ ] B , int N ) { int i = 0 ; int j = 0 ; int prev = - 1 ; boolean flag = true ; while ( i < N && j < N ) { if ( A [ i ] < B [ j ] && prev != 0 ) { prev = 0 ; i ++ ; } else if ( B [ j ] < A [ i ] && prev != 1 ) { prev = 1 ; j ++ ; } else if ( A [ i ] == B [ j ] ) { if ( prev != 1 ) { prev = 1 ; j ++ ; } else { prev = 0 ; i ++ ; } } else { flag = false ; break ; } } return flag ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 5 , 8 } ; int [ ] B = { 2 , 4 , 6 } ; int N = A . length ; if ( checkIfPossibleMerge ( A , B , N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | Java program to implement the above approach ; Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Perform K operations ; Stores smallest element in the array ; Stores index of the smallest array element ; Stores largest element in the array ; Stores index of the largest array element ; Traverse the array elements ; If current element exceeds largest_element ; Update the largest element ; Update index of the largest array element ; If current element is smaller than smallest_element ; Update the smallest element ; Update index of the smallest array element ; Stores the value of smallest element by given operations ; Stores the value of largest element by given operations ; If the value of a + b less than the sum of smallest and largest element of the array ; Update smallest element of the array ; Update largest element of the array ; Stores sum of elements of the array by given operations ; Traverse the array ; Update ans ; Driver Code
import java . util . * ; class GFG { static int minimum_possible_sum ( int arr [ ] , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = arr [ 0 ] ; int smallest_pos = 0 ; int largest_element = arr [ 0 ] ; int largest_pos = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] >= largest_element ) { largest_element = arr [ j ] ; largest_pos = j ; } if ( arr [ j ] < smallest_element ) { smallest_element = arr [ j ] ; smallest_pos = j ; } } int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { arr [ smallest_pos ] = a ; arr [ largest_pos ] = b ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = arr . length ; System . out . print ( minimum_possible_sum ( arr , n , K ) ) ; } }
Rearrange array elements excluded by given ranges to maximize sum of subarrays starting from the first index | Java program for the above approach ; Function that finds the maximum sum all subarrays from the starting index after rearranging the array ; Stores elements after rearranging ; Keeps track of visited elements ; Traverse the queries ; Mark elements that are not allowed to rearranged ; Stores the indices ; Get indices and elements that are allowed to rearranged ; Store the current index and element ; Sort vector v in descending order ; Insert elements in array ; Stores the resultant sum ; Stores the prefix sums ; Traverse the given array ; Return the maximum sum ; Driver Code ; Given array ; Given size ; Queries ; Number of queries ; Function Call
import java . util . * ; class GFG { static int maxSum ( int n , int a [ ] , int [ ] [ ] l , int q ) { Vector < Integer > v = new Vector < > ( ) ; int [ ] d = new int [ n ] ; for ( int i = 0 ; i < q ; i ++ ) { for ( int x = l [ i ] [ 0 ] ; x <= l [ i ] [ 1 ] ; x ++ ) { if ( d [ x ] == 0 ) { d [ x ] = 1 ; } } } HashSet < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] == 0 ) { v . add ( a [ i ] ) ; st . add ( i ) ; } } Collections . sort ( v ) ; Collections . reverse ( v ) ; int c = 0 ; for ( int it : st ) { a [ it ] = v . get ( c ) ; c ++ ; } int pref_sum = 0 ; int temp_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp_sum += a [ i ] ; pref_sum += temp_sum ; } return pref_sum ; } public static void main ( String [ ] args ) { int [ ] arr = { - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 } ; int N = arr . length ; int [ ] [ ] q = { { 0 , 0 } , { 4 , 5 } } ; int queries = q . length ; System . out . print ( maxSum ( N , arr , q , queries ) ) ; } }
Count pairs with Bitwise XOR greater than both the elements of the pair | Java program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; Arrays . sort ( A ) ; int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( ( 1 << j ) & A [ i ] ) == 0 ) { count += bits [ j ] ; } } ++ bits [ ( int ) ( ( int ) ( Math . log ( A [ i ] ) / Math . log ( 2 ) ) ) ] ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
Maximum occurred integers after M circular operations in given range | Java program for the above approach ; Function to find the maximum occurred integer after completing all circular operations ; Stores the highest visit count for any element ; Stors the number of times an element is visited ; Iterate over the array ; Iterate over the range circularly form start to end ; Count number of times an element is visited ; Increment frequency of N ; Update maxVisited ; Increment frequency of start ; Update maxVisited ; Increment the start ; Increment the count for the last visited element ; Print most visited elements ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void mostvisitedsector ( int N , ArrayList < Integer > A ) { int maxVisited = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < A . size ( ) - 1 ; i ++ ) { int start = A . get ( i ) % N ; int end = A . get ( i + 1 ) % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . containsKey ( N ) ) mp . put ( N , mp . get ( N ) + 1 ) ; else mp . put ( N , 1 ) ; if ( mp . get ( N ) > maxVisited ) { maxVisited = mp . get ( N ) ; } } else { if ( mp . containsKey ( start ) ) mp . put ( start , mp . get ( start ) + 1 ) ; else mp . put ( start , 1 ) ; if ( mp . get ( start ) > maxVisited ) { maxVisited = mp . get ( start ) ; } } start = ( start + 1 ) % N ; } } int last = A . get ( A . size ( ) - 1 ) ; if ( mp . containsKey ( last ) ) mp . put ( last , mp . get ( last ) + 1 ) ; else mp . put ( last , 1 ) ; if ( mp . get ( last ) > maxVisited ) { maxVisited = mp . get ( last ) ; } for ( Map . Entry x : mp . entrySet ( ) ) { if ( ( int ) x . getValue ( ) == maxVisited ) { System . out . print ( x . getKey ( ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 4 ; ArrayList < Integer > arr = new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , 1 , 2 ) ) ; mostvisitedsector ( N , arr ) ; } }
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points | Java program for the above approach ; Function to return the area of the largest rectangle formed by lines parallel to X and Y axis from given set of points ; Initialize two arrays ; Store x and y coordinates ; Sort arrays ; Initialize max differences ; Find max adjacent differences ; Return answer ; Driver Code ; Given points ; Total points ; Function call
import java . util . * ; class GFG { static int maxRectangle ( int [ ] [ ] sequence , int size ) { int [ ] X_Cord = new int [ size ] ; int [ ] Y_Cord = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { X_Cord [ i ] = sequence [ i ] [ 0 ] ; Y_Cord [ i ] = sequence [ i ] [ 1 ] ; } Arrays . sort ( X_Cord ) ; Arrays . sort ( Y_Cord ) ; int X_Max = 0 , Y_Max = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { X_Max = Math . max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] ) ; Y_Max = Math . max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] ) ; } return X_Max * Y_Max ; } public static void main ( String [ ] args ) { int [ ] [ ] point = { { - 2 , 0 } , { 2 , 0 } , { 4 , 0 } , { 4 , 2 } } ; int n = point . length ; System . out . print ( maxRectangle ( point , n ) ) ; } }