Find a pair in Array with second largest product | Java program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Print second largest product pair ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static void maxProduct ( int arr [ ] , int N ) { if ( N < 3 ) { return ; } Arrays . sort ( arr ) ; int smallest1 = arr [ 0 ] ; int smallest3 = arr [ 2 ] ; int largest1 = arr [ N - 1 ] ; int largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { System . out . print ( smallest1 + " ▁ " + smallest3 ) ; } else { System . out . print ( largest1 + " ▁ " + largest3 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . length ; maxProduct ( arr , N ) ; } }