Java Challenge - Max Product Finder

This community-built FAQ covers the “Max Product Finder” code challenge in Java. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Java challenge Max Product Finder

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

private static int getArray(int array, int except) {
int newArray = new int[array.length - 1];
for (int i = 0; i < array.length; i++) {
if (i < except) {
newArray[i] = array[i];
} else if (i > except) {
newArray[i - 1] = array[i];
}
}
return newArray;
}

public static void product(int array, int k, int value, List list) {
if (k == 0) {
list.add(value);
}
for (int i = 0; i < array.length; i++) {
product(getArray(array, i), k - 1, value * array[i], list);
}
}

public static int maxProductFinderK(int array, int k) {
List list = new ArrayList<>();
product(array, k, 1, list);
list.sort((a1, a2) → a2.compareTo(a1));
return list.get(0);
}

import java.util.Arrays;
public class MaxProductFinder {
public static void main(String args) {
int arr = new int{-8, 6, -7, 3, 2, 1, -9};
int result = maxProductFinderK(arr, 3);
System.out.println(result);
}

public static int maxProductFinderK(int[] array, int k) {
    int product = 1, len = array.length;

    //special cases
    if (k < 1) {
        return 0;
    } else if (k == 1) {
        Arrays.sort(array);
        return array[len - 1];
    } else if (k >= len) {
        for(int i = 0; i < k; i++){
            product *= array[i];
        }
        return product;
    }

    //create 2 arrays: for positive and negative numbers
    int negCount = 0;
    for(int i = 0; i < len; i++) {
        if (array[i] < 0) negCount++;
    }
    int[] negArr = new int[negCount];
    int[] posArr = new int[len - negCount];
    //...and fill them
    int posInd = -1, negInd = -1;
    for(int i = 0; i < len; i++) {
        if (array[i] < 0) {
            negArr[++negInd] = -array[i];
        } else {
            posArr[++posInd] = array[i];
        }
    }
    Arrays.sort(negArr);
    Arrays.sort(posArr);

    int posProd, negProd;
    for(int i = k; i > 0; i-=2){
        posProd = -1; negProd = -1;
        if (i==1) {
            if (posInd >= 0) posProd = posArr[posInd];
            else negProd = -negArr[0];
        } else {
            if (posInd > 1) posProd = posArr[posInd] * posArr[posInd - 1];
            if (negInd > 1) negProd = negArr[negInd] * negArr[negInd - 1];

        }
        if (posProd > negProd) {
            product *= posProd;
            posInd-=2;
        } else {
            product *= negProd;
            negInd-=2;
        }
    }
    return product;
}

}

/* O(n) */
public static void product(int array, int k, int value, Integer max) {
if (k == 0) {
if (max[0] < value) {
max[0] = value;
}
}
for (int i = 0; i < array.length; i++) {
product(getArray(array, i), k - 1, value * array[i], max);
}
}

public static int maxProductFinderK(int array, int k) {
List list = new ArrayList<>();
Integer max = new Integer{Integer.MIN_VALUE};
product(array, k, 1, max);
return max[0];
}

private static int getArray(int array, int except) {
int newArray = new int[array.length - 1];
for (int i = 0; i < array.length; i++) {
if (i < except) {
newArray[i] = array[i];
} else if (i > except) {
newArray[i - 1] = array[i];
}
}
return newArray;
}

O(n log n) because of sort.


import java.util.Arrays;
public class MaxProductFinder {
  public static void main(String[] args) {
    int[] arr = new int[]{-8, 6, -7, 3, 2, 1, -9};
    int result = maxProductFinderK(arr, 3);
    System.out.println(result);
  }

  public static int maxProductFinderK(int[] array, int k) {
    // Write your code here

  int res = 1;
    
  Arrays.sort(array);

  int n = array.length-1;
  int i = 0;
  int j = k; 

  // the goal is to check which one is the max product between the smallest two integers or the highest two integers. 

  while (j > 1) {
    if (array[i]*array[i+1] > array[n]*array[n-1]){
      res *=array[i]*array[i+1];
      i = i + 2;
    }
    else {
      res *= array[n]*array[n-1];
      n = n-2;
    }
    j = j - 2;
	}

// if k is odd, we need to do one more product with the highest integer unused.`

if (j == 1)
		res *= array[n];

    return res;    
  }
}

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MaxProductFinder {
public static void main(String args) {
int arr = new int{-8, 6, -7, 3, 2, 1, -9};
int result = maxProductFinderK(arr, 3);
System.out.println(result);
}

public static int maxProductFinderK(int[] array, int k) {
    if(k > array.length || k < 1) {
        return 0;
    } else if (k == array.length) {
        int product = 1;
        for(int i = 0; i < array.length; i++) {
            product = product * array[i];
        }
        return product;
    }

    List<List<Integer>> resultList = new ArrayList<>();
    ArrayList<Integer> accumulatorList = new ArrayList<>();
    int index = 0;
    List<Integer> defaultArray = new ArrayList<>();
    for(int i = 0; i<array.length; i++) {
        defaultArray.add(i, array[i]);
    }
    combinationsList(defaultArray, k, resultList, accumulatorList, index);
    int[] completeResultsList = new int[resultList.size()];
    int product = 1;
    for (int i = 0; i < resultList.size(); i++) {
        for (int x = 0; x < resultList.get(i).size(); x++) {
            product = product * resultList.get(i).get(x);
        }
        completeResultsList[i] = product;
        product = 1;
    }
    Arrays.sort(completeResultsList);
    return completeResultsList[completeResultsList.length -1];
}

private static List<List<Integer>> combinationsList(
        List<Integer> inputSet, int k, List<java.util.List<Integer>> results, ArrayList<Integer> accumulator, int index) {

    int needToAccumulate = k - accumulator.size();
    int canAccumulate = inputSet.size() - index;

    if (accumulator.size() == k) {
        results.add(new ArrayList<>(accumulator));
    } else if (needToAccumulate <= canAccumulate) {
        combinationsList(inputSet, k, results, accumulator, index + 1);
        accumulator.add(inputSet.get(index));
        combinationsList(inputSet, k, results, accumulator, index + 1);
        accumulator.remove(accumulator.size() - 1);
    }
    return results;
}

}