instagram

Given array find a subsequence of length 3 having maximum product

Given a sequence of non-negative integers find a subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order.

Example:
Input: 6 7 8 1 2 3 9 10
Ouput: 8 9 10

Algorithm
import java.util.Arrays;

public class MaxProductOfThree {

	public static void main(String[] args) {

		int[] a = { 6, 7, 8, 1, 2, 3, 9, 10 };

		for (int i : maxProduct(a, 3)) {
			System.out.print(i + " ");
		}

	}

	public static int[] maxProduct(int a[], int n) {

		Arrays.sort(a);

		int[] r = new int[n];

		int j = n - 1;
		for (int i = a.length - 1; j >= 0; i--, j--) {
			r[j] = a[i];
		}

		return r;

	}

}
Share