Posts

Showing posts from September, 2015

Java program to find the highest product of 3 elements in an array

This blog post is about java program to find the highest product of 3 elements in an array. Introduction: When this question is asked, the usual solution which comes to our mind will be multiplying each element with other elements and finding the maximum product out of those, which is the brute force solution. The function for brute force  approach is as follows: /* * Method to return the highest product of three elements. * return type: Integer * input: int arr[] */ public static Integer returnHighestProduct( int arr[]) { if ((arr== null )||(arr.length<3)) { return null ; } else { int product = Integer.MIN_VALUE; for ( int i=0;i<arr.length;i++) { for ( int j=i+1;j<arr.length;j++) { for ( int k=j+1;k<arr.length;k++) { int max_product = arr[i]*arr[j]*arr[k]; if (max_product>=product) { product = max_product; } } } } return product; } } Bu