Javatpoint Logo

Can you please make program - Multiplication of 3 highest element in given array (Array contains Negative number as well)

By: preety*** On: Wed Jun 06 01:06:47 IST 2018     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
i have write below code but it give some unexpected result, negative number shows in output, though 0 is greater then any negative number

public class Amazon_productNo
{
public static void MaxProduct(int arr[], int l)
{
int a = arr[0];
int b = arr[1];
int c = arr[2];

for (int i=0;i<l;i++)
for(int j=i+1;j<l;j++)
for(int k=j+1;k<l;k++)
if(arr[i]*arr[j]*arr[k]>a*b*c)
{
a=arr[i];
b=arr[j];
c=arr[k];
}
System.out.println("Max product pair is:-- " +a+ ", " +b+","+c);
System.out.println("Max product pair is:-- " +a*b*c);
}

public static void main(String[] args)
{
int arr[] = {3,4,6,7,8,8,9,-1,-5,-55};
int l = arr.length;
MaxProduct(arr,l);
}
}
Up0Down
core java  x  328Tags