Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Algorithm to calculate the maximum product of any n-1 elements in the array in O(n) time complexity for only positive integers

What I have tried:

I have tried solving using Brute force method. The complexity comes out to be o(n^2)
Posted
Updated 26-Oct-21 21:14pm
Comments
OriginalGriff 7-Apr-19 8:30am    
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Quote:
Algorithm to calculate the maximum product of any n-1 elements in the array in O(n) time complexity for only positive integers

This is your job to find/create algorithms that solve problems, homeworks is made for practicing.
Your task is to find an efficient algorithm.
Quote:
I have tried solving using Brute force method. The complexity comes out to be o(n^2)

Brute force is the most inefficient algorithm, but at least you did a solution.
But the whole thing is about making you using your brain, and explorer the problem from different aspects until you find something useful.
- First study/master the subject "multiplication", I hope you have mastered it a few years ago.
- think of different ways to ask the question. Which integer do I have to remove from the list to get maximum product?
- Take a sheet of paper and a pencil and start with a small sample. Solve by hand.
Which integer should I remove to get maximum product? 1 2 3 4 5
Why removing this integer is the solution?
Do you need to calculate all 5 five products to find the answer?
What if list is 1 2 3 4 ... 96 97 98 99.

by this point, you should have found that the solution is so simple that you wonder why you didn't find alone.

- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 
Comments
CPallini 27-Oct-21 3:23am    
5.
Patrice T 27-Oct-21 6:13am    
Thank you
Java
public static int maxProduct(int[] inp){
    int len = inp.length;
    
    int leastPositive = Integer.MAX_VALUE, maxNegative = Integer.MIN_VALUE, 
    leastNegative = 0;
    
    
    int numOfNegativeIntegers = 0;
    
    for(int i=0; i<len; i++){
      int currNum = inp[i];
      if(currNum < 0){
        numOfNegativeIntegers++;
        maxNegative = Math.max(maxNegative, currNum);
        leastNegative = Math.min(leastNegative, currNum);
      }else if(currNum >= 0){
        leastPositive = Math.min(leastPositive, currNum);
      }
    }
    
    if(numOfNegativeIntegers %2 == 0){
      if(numOfNegativeIntegers == len){
        return leastNegative;
      }
      else{
        return leastPositive;
      }
    }
    else{
      return maxNegative;
    }
    
  } 
 
Share this answer
 
Comments
Richard Deeming 27-Oct-21 6:16am    
An unexplained code-dump is not a solution to anything.

And you don't help anyone by doing their homework assignment for them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900