codeparrot/xlcost-text-to-code huggingface dataset:
Minimum index i such that all the elements from index i to given index are equal | Java implementation of the approach ; Function to return the minimum required index ; Short - circuit more comparisions as found the border point ; For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not point to the correct index . ; Driver code ; System . out . println ( minIndex ( arr , 2 ) ) ; Should be 1 System . out . println ( minIndex ( arr , 3 ) ) ; Should be 1 System . out . println ( minIndex ( arr , 4 ) ) ; Should be 4
class GFG { static int minIndex ( int arr [ ] , int pos ) { int low = 0 ; int high = pos ; int i = pos ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] != arr [ pos ] ) { low = mid + 1 ; } else { high = mid - 1 ; i = mid ; if ( mid > 0 && arr [ mid - 1 ] != arr [ pos ] ) { break ; } } } return arr [ low ] == arr [ pos ] ? low : i ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 1 , 5 , 2 } ; } }
Count of strings that become equal to one of the two strings after one removal | Java implementation of the approach ; Function to return the count of the required strings ; Searching index after longest common prefix ends ; Searching index before longest common suffix ends ; If str1 = str2 ; If only 1 character is different in both the strings ; Checking remaining part of string for equality ; Searching in right of string h ( g to h ) ; Driver code
import java . util . * ; class GFG { static int findAnswer ( String str1 , String str2 , int n ) { int l = 0 , r = 0 ; int ans = 2 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { l = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { r = i ; break ; } } if ( r < l ) return 26 * ( n + 1 ) ; else if ( l == r ) return ans ; else { for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i - 1 ) ) { ans -- ; break ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i - 1 ) != str2 . charAt ( i ) ) { ans -- ; break ; } } return ans ; } } public static void main ( String args [ ] ) { String str1 = " toy " , str2 = " try " ; int n = str1 . length ( ) ; System . out . println ( findAnswer ( str1 , str2 , n ) ) ; } }
Minimize the maximum minimum difference after one removal from array | Java implementation of the approach ; Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code
import java . util . * ; class solution { static int findMinDifference ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int diff1 = arr [ n - 1 ] - arr [ 1 ] ; int diff2 = arr [ n - 2 ] - arr [ 0 ] ; return Math . min ( diff1 , diff2 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . length ; System . out . print ( findMinDifference ( arr , n ) ) ; } }
Minimize the maximum minimum difference after one removal from array | Java implementation of the approach ; Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code
public class GFG { static int findMinDifference ( int arr [ ] , int n ) { int min , secondMin , max , secondMax ; min = secondMax = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 0 ] : arr [ 1 ] ; max = secondMin = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 1 ] : arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } else if ( arr [ i ] < min ) { secondMin = min ; min = arr [ i ] ; } else if ( arr [ i ] < secondMin ) { secondMin = arr [ i ] ; } } int diff = Math . min ( max - secondMin , secondMax - min ) ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinDifference ( arr , n ) ) ; } }
Integers from the range that are composed of a single distinct digit | Java implementation of the approach ; Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code
import java . io . * ; class GFG { static boolean checkDistinct ( int x ) { int last = x % 10 ; while ( x > 0 ) { if ( x % 10 != last ) return false ; x = x / 10 ; } return true ; } static int findCount ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( checkDistinct ( i ) ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int L = 10 , R = 50 ; System . out . println ( findCount ( L , R ) ) ; } }
Smallest Pair Sum in an array | Java program to print the sum of the minimum pair ; Function to return the sum of the minimum pair from the array ; If found new minimum ; Minimum now becomes second minimum ; Update minimum ; If current element is > min and < secondMin ; Update secondMin ; Return the sum of the minimum pair ; Driver code
import java . io . * ; class GFG { static int smallest_pair ( int [ ] a , int n ) { int min = Integer . MAX_VALUE , secondMin = Integer . MAX_VALUE ; for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] < min ) { secondMin = min ; min = a [ j ] ; } else if ( ( a [ j ] < secondMin ) && a [ j ] != min ) secondMin = a [ j ] ; } return ( secondMin + min ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( smallest_pair ( arr , n ) ) ; } }
Longest subarray with elements divisible by k | Java program of above approach ; function to find longest subarray ; this will contain length of longest subarray found ; Driver code
import java . io . * ; class GFG { static int longestsubarray ( int arr [ ] , int n , int k ) { int current_count = 0 ; int max_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) current_count ++ ; else current_count = 0 ; max_count = Math . max ( current_count , max_count ) ; } return max_count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 11 , 32 , 64 , 88 } ; int n = arr . length ; int k = 8 ; System . out . println ( longestsubarray ( arr , n , k ) ) ; } }
Remove elements that appear strictly less than k times | Java program to remove the elements which appear strictly less than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear more than or equal to k times . ; Driver code
import java . util . HashMap ; class geeks { public static void removeElements ( int [ ] arr , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 1 ) ; else { int x = mp . get ( arr [ i ] ) ; mp . put ( arr [ i ] , ++ x ) ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) >= k ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; removeElements ( arr , n , k ) ; } }
Check if a string contains a palindromic sub | Java program to check if there is a substring palindrome of even length . ; function to check if two consecutive same characters are present ; Driver Code
class GFG { static boolean check ( String s ) { for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " xzyyz " ; if ( check ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Remove elements from the array which appear more than k times | Java program to remove the elements which appear more than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear less than or equal to k times . ; Driver code
import java . util . HashMap ; import java . util . Map ; class GFG { static void RemoveElements ( int arr [ ] , int n , int k ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( arr [ i ] ) && mp . get ( arr [ i ] ) <= k ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; RemoveElements ( arr , n , k ) ; } }
Find the smallest after deleting given elements | Java program to find the smallest number from the array after n deletions ; Returns minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the smallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it smallestElement ; Driver Code
import java . util . * ; class GFG { static int findSmallestAfterDel ( int arr [ ] , int m , int del [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( del [ i ] ) ) { mp . put ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . put ( del [ i ] , 1 ) ; } } int smallestElement = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . remove ( arr [ i ] ) ; } else smallestElement = Math . min ( smallestElement , arr [ i ] ) ; } return smallestElement ; } public static void main ( String [ ] args ) { int array [ ] = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . length ; int del [ ] = { 12 , 4 , 56 , 5 } ; int n = del . length ; System . out . println ( findSmallestAfterDel ( array , m , del , n ) ) ; } }
Find the largest after deleting the given elements | Java program to find the largest number from the array after n deletions ; Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Driver Code
import java . util . * ; class GFG { static int findlargestAfterDel ( int arr [ ] , int m , int del [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( del [ i ] ) ) { mp . put ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . put ( del [ i ] , 1 ) ; } } int largestElement = Integer . MIN_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . remove ( arr [ i ] ) ; } else largestElement = Math . max ( largestElement , arr [ i ] ) ; } return largestElement ; } public static void main ( String [ ] args ) { int array [ ] = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . length ; int del [ ] = { 12 , 33 , 56 , 5 } ; int n = del . length ; System . out . println ( findlargestAfterDel ( array , m , del , n ) ) ; } }
Number of anomalies in an array | A simple java solution to count anomalies in an array . ; Driver code
class GFG { static int countAnomalies ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && Math . abs ( arr [ i ] - arr [ j ] ) <= k ) break ; if ( j == n ) res ++ ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 1 , 8 } , k = 5 ; int n = arr . length ; System . out . println ( countAnomalies ( arr , n , k ) ) ; } }
Count majority element in a matrix | Java program to find count of all majority elements in a Matrix ; Function to find count of all majority elements in a Matrix ; Store frequency of elements in matrix ; loop to iteratre through map ; check if frequency is greater than or equal to ( N * M ) / 2 ; Driver Code
import java . util . * ; class GFG { static int majorityInMatrix ( int arr [ ] [ ] ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . containsKey ( arr [ i ] [ j ] ) ) mp . put ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) ; else mp . put ( arr [ i ] [ j ] , 1 ) ; } } int countMajority = 0 ; Iterator < HashMap . Entry < Integer , Integer > > itr = mp . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { HashMap . Entry < Integer , Integer > entry = itr . next ( ) ; if ( entry . getValue ( ) >= ( ( N * M ) / 2 ) ) { countMajority ++ ; } } return countMajority ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 2 } , { 1 , 3 , 2 } , { 1 , 2 , 6 } } ; System . out . println ( majorityInMatrix ( mat ) ) ; } }
Find pair with maximum difference in any column of a Matrix | Java program to find column with max difference of any pair of elements ; Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; Driver Code
public class GOC1 { static int colMaxDiff ( int mat [ ] [ ] ) { int max_diff = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int max_val = mat [ 0 ] [ i ] , min_val = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < N ; j ++ ) { max_val = Math . max ( max_val , mat [ j ] [ i ] ) ; min_val = Math . min ( min_val , mat [ j ] [ i ] ) ; } max_diff = Math . max ( max_diff , max_val - min_val ) ; } return max_diff ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 5 , 3 , 5 , 4 , 0 } , { 5 , 6 , 7 , 8 , 9 } , { 0 , 6 , 3 , 4 , 12 } , { 9 , 7 , 12 , 4 , 3 } , } ; System . out . println ( " Max ▁ difference ▁ : ▁ " + colMaxDiff ( mat ) ) ; } }
Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted array of distinct elements within limited range . ; Driver Code
import java . io . * ; class GFG { static int search ( int ar [ ] , int size ) { int a = 0 , b = size - 1 ; int mid = 0 ; while ( ( b - a ) > 1 ) { mid = ( a + b ) / 2 ; if ( ( ar [ a ] - a ) != ( ar [ mid ] - mid ) ) b = mid ; else if ( ( ar [ b ] - b ) != ( ar [ mid ] - mid ) ) a = mid ; } return ( ar [ a ] + 1 ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 8 } ; int size = ar . length ; System . out . println ( " Missing ▁ number : ▁ " + search ( ar , size ) ) ; } }
Delete array element in given index range [ L | Java code to delete element in given range ; Delete L to R elements ; Return size of Array after delete element ; Driver Code
class GFG { static int deleteElement ( int A [ ] , int L , int R , int N ) { int i , j = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( i <= L i >= R ) { A [ j ] = A [ i ] ; j ++ ; } } return j ; } public static void main ( String args [ ] ) { int A [ ] = new int [ ] { 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 } ; int L = 2 , R = 7 ; int n = A . length ; int res_size = deleteElement ( A , L , R , n ) ; for ( int i = 0 ; i < res_size ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
Find the only missing number in a sorted array | Java program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver code
class GFG { static int findmissing ( int [ ] ar , int N ) { int l = 0 , r = N - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] != mid + 1 && ar [ mid - 1 ] == mid ) return ( mid + 1 ) ; if ( ar [ mid ] != mid + 1 ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int N = arr . length ; System . out . println ( findmissing ( arr , N ) ) ; } }
Find index of first occurrence when an unsorted array is sorted | Java program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ;  ; Driver Code
import java . util . * ; class GFG { static int findFirst ( int arr [ ] , int n , int x ) { Arrays . sort ( arr ) ; int ptr = lowerBound ( arr , 0 , n , x ) ; return ( arr [ ptr ] != x ) ? - 1 : ( ptr ) ; } static int lowerBound ( 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 ; } public static void main ( String [ ] args ) { int x = 20 , arr [ ] = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . length ; System . out . println ( findFirst ( arr , n , x ) ) ; } }
Find index of first occurrence when an unsorted array is sorted | Java program to find index of first occurrence of x when array is sorted . ; Driver main
public class GFG { static int findFirst ( int arr [ ] , int n , int x ) { int count = 0 ; boolean isX = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { isX = true ; } else if ( arr [ i ] < x ) { count ++ ; } } return ( isX == false ) ? - 1 : count ; } public static void main ( String [ ] args ) { int x = 20 , arr [ ] = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . length ; System . out . println ( findFirst ( arr , n , x ) ) ; } }
Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | Java code to find the repeated elements in the array where every other is present once ; Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver Code
import java . util . * ; class GFG { public static int findDuplicate ( int [ ] arr ) { int slow = arr [ 0 ] ; int fast = arr [ 0 ] ; do { slow = arr [ slow ] ; fast = arr [ arr [ fast ] ] ; } while ( slow != fast ) ; int ptr1 = arr [ 0 ] ; int ptr2 = slow ; while ( ptr1 != ptr2 ) { ptr1 = arr [ ptr1 ] ; ptr2 = arr [ ptr2 ] ; } return ptr1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 1 } ; System . out . println ( " " + findDuplicate ( arr ) ) ; System . exit ( 0 ) ; } }
Number of Larger Elements on right side in a string | Java program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code
public class GFG { final static int MAX_CHAR = 26 ; static void printGreaterCount ( String str ) { int len = str . length ( ) ; int ans [ ] = new int [ len ] , count [ ] = new int [ MAX_CHAR ] ; for ( int i = len - 1 ; i >= 0 ; i -- ) { count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int j = str . charAt ( i ) - ' a ' + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }
Print all pairs with given sum | Java implementation of simple method to find count of pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map m ; Traverse through all elements ; Search if a pair can be formed with arr [ i ] . ; Driver code
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n , int sum ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = sum - arr [ i ] ; if ( mp . containsKey ( rem ) ) { int count = mp . get ( rem ) ; for ( int j = 0 ; j < count ; j ++ ) System . out . print ( " ( " + rem + " , ▁ " + arr [ i ] + " ) " + "NEW_LINE"); } if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
Maximum product quadruple ( sub | A O ( n ) Java program to find maximum quadruple in an array . ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver Code
class GFG { static int maxProduct ( int arr [ ] , int n ) { if ( n < 4 ) { return - 1 ; } int maxA = Integer . MIN_VALUE , maxB = Integer . MIN_VALUE , maxC = Integer . MIN_VALUE , maxD = Integer . MIN_VALUE ; int minA = Integer . MAX_VALUE , minB = Integer . MAX_VALUE , minC = Integer . MAX_VALUE , minD = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxD = maxC ; maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxD = maxC ; maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) { maxD = maxC ; maxC = arr [ i ] ; } else if ( arr [ i ] > maxD ) { maxD = arr [ i ] ; } if ( arr [ i ] < minA ) { minD = minC ; minC = minB ; minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) { minD = minC ; minC = minB ; minB = arr [ i ] ; } else if ( arr [ i ] < minC ) { minD = minC ; minC = arr [ i ] ; } else if ( arr [ i ] < minD ) { minD = arr [ i ] ; } } int x = maxA * maxB * maxC * maxD ; int y = minA * minB * minC * minD ; int z = minA * minB * maxA * maxB ; return Math . max ( x , Math . max ( y , z ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 4 , 3 , - 6 , 7 , 0 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . println ( " No ▁ Quadruple ▁ Exists " ) ; else System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }