Skip to main content
Tweeted twitter.com/StackCodeReview/status/1299180018405384198
Became Hot Network Question

Question: Given an array numbers = {2, 7, 8, 5, 1, 6, 3, 9, 4}. Check $$a := [2, 7, 8, 5, 1, 6, 3, 9, 4]$$ Check the below conditions, both the conditions should be satisfied.

a[i] > a[i-1] or if first element a[i] > a[i+1] a[i] > a[i+1] or if last element a[lastelement] > a[lastelement - 1] 1st Iteration - 8, 6, 9 are peak values. Remove the smallest ele. Remove 6. New arr {2, 7, 8, 5, 1, 3, 9, 4}. Output Arr - {6}

2nd Iteration - 8, 9 are peak values. Remove the smallest ele. Remove 8. New arr {2, 7, 5, 1, 3, 9, 4}. Output Arr - {6, 8}

3rd Iteration - 7, 9 are peak values. Remove the smallest ele. Remove 7. New arr {2, 5, 1, 3, 9, 4}. Output Arr - {6, 7, 8}

4th Iteration - 5, 9 are peak values. Remove the smallest ele. Remove 5. New arr {2, 1, 3, 9, 4}. Output Arr - {6, 7, 8, 5}

5th Iteration - 2, 9 are peak values. Remove the smallest ele. Remove 2. New arr {1, 3, 9, 4}. Output Arr - {6, 7, 8, 5, 2}

6th Iteration - 9 are peak values. Remove the smallest ele. Remove 9. New arr {1, 3, 4}. Output Arr - {6, 7, 8, 5, 2, 9}

7th Iteration - 4 are peak values. Remove the smallest ele. Remove 4. New arr {1, 3}. Output Arr - {6, 7, 8, 5, 2, 9, 4}\begin{gather} \text{If }a[i] > a[i-1]\text{ or if first element }a[i] > a[i+1] \end{gather}

8th Iteration - 3 are peak values. Remove the smallest ele. Remove 3. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}\begin{gather} \text{If }a[i] > a[i+1]\text{ or if last element }a[LastIndex] > a[LastIndex - 1] \end{gather}

9th Iteration - 1 are peak values. Remove the smallest ele. Remove 1. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}

  • 1st Iteration - 8, 6, 9 are peak values.

    • Remove the smallest ele.
    • Remove 6.
    • New arr {2, 7, 8, 5, 1, 3, 9, 4}.
    • Output Arr - {6}
  • 2nd Iteration - 8, 9 are peak values.

    • Remove the smallest ele.
    • Remove 8.
    • New arr {2, 7, 5, 1, 3, 9, 4}.
    • Output Arr - {6, 8}
  • 3rd Iteration - 7, 9 are peak values.

    • Remove the smallest ele.
    • Remove 7. New arr {2, 5, 1, 3, 9, 4}.
    • Output Arr - {6, 7, 8}
  • 4th Iteration - 5, 9 are peak values.

    • Remove the smallest ele.
    • Remove 5.
    • New arr {2, 1, 3, 9, 4}.
    • Output Arr - {6, 7, 8, 5}
  • 5th Iteration - 2, 9 are peak values.

    • Remove the smallest ele.
    • Remove 2.
    • New arr {1, 3, 9, 4}.
    • Output Arr - {6, 7, 8, 5, 2}
  • 6th Iteration - 9 are peak values.

    • Remove the smallest ele.
    • Remove 9.
    • New arr {1, 3, 4}.
    • Output Arr - {6, 7, 8, 5, 2, 9}
  • 7th Iteration - 4 are peak values.

    • Remove the smallest ele.
    • Remove 4.
    • New arr {1, 3}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4}
  • 8th Iteration - 3 are peak values.

    • Remove the smallest ele.
    • Remove 3.
    • New arr {1}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}
  • 9th Iteration - 1 are peak values.

    • Remove the smallest ele.
    • Remove 1.
    • New arr {1}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}

Output: {6, 8, 7, 5, 2, 9, 4, 3, 1}{6, 8, 7, 5, 2, 9, 4, 3, 1}

Question: Given an array numbers = {2, 7, 8, 5, 1, 6, 3, 9, 4}. Check the below conditions, both the conditions should be satisfied.

a[i] > a[i-1] or if first element a[i] > a[i+1] a[i] > a[i+1] or if last element a[lastelement] > a[lastelement - 1] 1st Iteration - 8, 6, 9 are peak values. Remove the smallest ele. Remove 6. New arr {2, 7, 8, 5, 1, 3, 9, 4}. Output Arr - {6}

2nd Iteration - 8, 9 are peak values. Remove the smallest ele. Remove 8. New arr {2, 7, 5, 1, 3, 9, 4}. Output Arr - {6, 8}

3rd Iteration - 7, 9 are peak values. Remove the smallest ele. Remove 7. New arr {2, 5, 1, 3, 9, 4}. Output Arr - {6, 7, 8}

4th Iteration - 5, 9 are peak values. Remove the smallest ele. Remove 5. New arr {2, 1, 3, 9, 4}. Output Arr - {6, 7, 8, 5}

5th Iteration - 2, 9 are peak values. Remove the smallest ele. Remove 2. New arr {1, 3, 9, 4}. Output Arr - {6, 7, 8, 5, 2}

6th Iteration - 9 are peak values. Remove the smallest ele. Remove 9. New arr {1, 3, 4}. Output Arr - {6, 7, 8, 5, 2, 9}

7th Iteration - 4 are peak values. Remove the smallest ele. Remove 4. New arr {1, 3}. Output Arr - {6, 7, 8, 5, 2, 9, 4}

8th Iteration - 3 are peak values. Remove the smallest ele. Remove 3. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}

9th Iteration - 1 are peak values. Remove the smallest ele. Remove 1. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}

Output: {6, 8, 7, 5, 2, 9, 4, 3, 1}

Question: Given an array numbers $$a := [2, 7, 8, 5, 1, 6, 3, 9, 4]$$ Check the below conditions, both the conditions should be satisfied.

\begin{gather} \text{If }a[i] > a[i-1]\text{ or if first element }a[i] > a[i+1] \end{gather}

\begin{gather} \text{If }a[i] > a[i+1]\text{ or if last element }a[LastIndex] > a[LastIndex - 1] \end{gather}

  • 1st Iteration - 8, 6, 9 are peak values.

    • Remove the smallest ele.
    • Remove 6.
    • New arr {2, 7, 8, 5, 1, 3, 9, 4}.
    • Output Arr - {6}
  • 2nd Iteration - 8, 9 are peak values.

    • Remove the smallest ele.
    • Remove 8.
    • New arr {2, 7, 5, 1, 3, 9, 4}.
    • Output Arr - {6, 8}
  • 3rd Iteration - 7, 9 are peak values.

    • Remove the smallest ele.
    • Remove 7. New arr {2, 5, 1, 3, 9, 4}.
    • Output Arr - {6, 7, 8}
  • 4th Iteration - 5, 9 are peak values.

    • Remove the smallest ele.
    • Remove 5.
    • New arr {2, 1, 3, 9, 4}.
    • Output Arr - {6, 7, 8, 5}
  • 5th Iteration - 2, 9 are peak values.

    • Remove the smallest ele.
    • Remove 2.
    • New arr {1, 3, 9, 4}.
    • Output Arr - {6, 7, 8, 5, 2}
  • 6th Iteration - 9 are peak values.

    • Remove the smallest ele.
    • Remove 9.
    • New arr {1, 3, 4}.
    • Output Arr - {6, 7, 8, 5, 2, 9}
  • 7th Iteration - 4 are peak values.

    • Remove the smallest ele.
    • Remove 4.
    • New arr {1, 3}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4}
  • 8th Iteration - 3 are peak values.

    • Remove the smallest ele.
    • Remove 3.
    • New arr {1}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}
  • 9th Iteration - 1 are peak values.

    • Remove the smallest ele.
    • Remove 1.
    • New arr {1}.
    • Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}

Output: {6, 8, 7, 5, 2, 9, 4, 3, 1}

Source Link

Find minimum peak elements in an array

Question: Given an array numbers = {2, 7, 8, 5, 1, 6, 3, 9, 4}. Check the below conditions, both the conditions should be satisfied.

a[i] > a[i-1] or if first element a[i] > a[i+1] a[i] > a[i+1] or if last element a[lastelement] > a[lastelement - 1] 1st Iteration - 8, 6, 9 are peak values. Remove the smallest ele. Remove 6. New arr {2, 7, 8, 5, 1, 3, 9, 4}. Output Arr - {6}

2nd Iteration - 8, 9 are peak values. Remove the smallest ele. Remove 8. New arr {2, 7, 5, 1, 3, 9, 4}. Output Arr - {6, 8}

3rd Iteration - 7, 9 are peak values. Remove the smallest ele. Remove 7. New arr {2, 5, 1, 3, 9, 4}. Output Arr - {6, 7, 8}

4th Iteration - 5, 9 are peak values. Remove the smallest ele. Remove 5. New arr {2, 1, 3, 9, 4}. Output Arr - {6, 7, 8, 5}

5th Iteration - 2, 9 are peak values. Remove the smallest ele. Remove 2. New arr {1, 3, 9, 4}. Output Arr - {6, 7, 8, 5, 2}

6th Iteration - 9 are peak values. Remove the smallest ele. Remove 9. New arr {1, 3, 4}. Output Arr - {6, 7, 8, 5, 2, 9}

7th Iteration - 4 are peak values. Remove the smallest ele. Remove 4. New arr {1, 3}. Output Arr - {6, 7, 8, 5, 2, 9, 4}

8th Iteration - 3 are peak values. Remove the smallest ele. Remove 3. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}

9th Iteration - 1 are peak values. Remove the smallest ele. Remove 1. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}

Output: {6, 8, 7, 5, 2, 9, 4, 3, 1}

My solution is working but I am looking for optimized solution. Please let me know.

Here is my code:

public int[] findMinimumPeaks(int[] arr){
        List<Integer> list1 = new ArrayList<Integer>(arr.length);
        int[] output = new int[arr.length];
        for(int i: arr)
            list1.add(i);
        
        for(int i =0; i<arr.length; i++){
            int minIndex = minimumPeakElement(list1);
            output[i] = list1.get(minIndex);
            list1.remove(minIndex);
        }
        return output;
    }
    
    public int minimumPeakElement(List<Integer> list1){
        int minIndex = 0, peakStart = Integer.MAX_VALUE, peakEnd = Integer.MAX_VALUE;
        int peak = Integer.MAX_VALUE, minPeak = Integer.MAX_VALUE;
        
        if(list1.size() >= 2){
            if(list1.get(0) > list1.get(1)) peakStart = list1.get(0);
            if(list1.get(list1.size() - 1) > list1.get(list1.size() - 2)) peakEnd = list1.get(list1.size() - 1);
            if(peakStart < peakEnd){
                minPeak = peakStart;
                minIndex = 0;
            }
            else if(peakEnd < peakStart){
                minPeak = peakEnd;
                minIndex = list1.size() - 1;
            }
        }
        
        for(int i=1; i<list1.size() - 1; i++){
            if(list1.get(i) > list1.get(i + 1) && list1.get(i) > list1.get(i-1)) peak = list1.get(i);
            if(peak < minPeak){
                minPeak = peak;
                minIndex = i;
            } 
        }
        return minIndex;
    }

```