0

I having a really hard time finding a plateau within an array. What I'm looking for is length and location of the longest sequence of equal values where the numbers on both sides are smaller. Now I know there could be two or more valid plateaus with same length ideally I would like to print the plateau with a higher value.

I have created an array[40] and a Random obj. To fill the array, once I have filled it I know I will need a loop to check the indexes. But thats where the confusion comes in. I have tried using a for loop to find the plateau but my results would just increase the value stored within the index. Any points in the right direction I would greatly appreciate it.

import java.util.Random;
public class Plateau {
public static void main(String[]args)
{
    int[] array1 = new int[40];
    Random genrator = new Random();

    for (int i = 0; i < array1.length; i++)
    {
        array1[i] = genrator.nextInt(21 - 1);
    }
    for(int i = 0; i < array1.length; i++)
    {
        System.out.print(array1[i] + " ");
    }
    System.out.println("\n");

    for (int i = 0; i < array1.length; i++)
    {
        if (array1[i] < array1[i] + 1)
        {

            System.out.println(array1[i] + " ");
        }

    }



}

}
1
  • 1
    Write it down on paper before turning it into code. How do you know when a plateau might start? Well, you know it if the previous value was smaller than the current value. Mark that as the beginning of a plateau, if the next value is bigger, the logic will again work. Then you just need to identify the end of the plateau. I'll leave that up to you.
    – Kayaman
    Commented Apr 3, 2014 at 12:06

2 Answers 2

0

In pseudo-code:

V value=first value
P position=1
L length=0
Scan the values from position Q=2
  Compare current value C to V
  If C is less than V
    if this sequence Q-P is longer than L 
      save length as L
      save P as location R
    update V and P to current value
  If C is greater than V
    update V and P to current value

I don't have a compiler for pseudo-code so it might not be exactly right, but it should be a start.

0
public class plateauv2 {
    
    public static void main(String[] args) {
        int[] arr = {1,1,2,3,4,5,1,2,4,3,3,3,3,3,5,2,2,2,2,31};
        plateau(arr);
    }

    public static void plateau(int arr[]) {
        int longestStartIdx = -1;
        int longestEndIdx = -1;
        
        int currL = 1;
        int maxL = 1;
        int currStartIdx = -1;
        int currEndIdx = -1;    
        for (int i = 0 , j = i + 1; j < arr.length; i++, j++) {
            boolean flag = false;
            if (arr[i] == arr[j]) {
                flag = true;
            } else {
                flag = false;
                currL = 1;
                currStartIdx = j;
            }
            if (flag) {
                currL++;
                currEndIdx=j;
                if (currL > maxL) {
                    maxL = currL;
                    longestStartIdx = currStartIdx;
                    longestEndIdx = currEndIdx;
                }     
            }
        }

        if (longestStartIdx != 1 && longestEndIdx !=1 ) {
               System.out.println("=================================");
               System.out.println("Longest Plateau Lenght: " +maxL );
               System.out.println("Plateau Number: " +arr[longestEndIdx]);
               System.out.println("Plateau starts at Index: " +longestStartIdx ); 
               System.out.println("Plateau ends at Index: "+longestEndIdx);           
               
               System.out.println("======================================");
               System.out.println(""); 
        } else {
            System.out.println("No Plateau Found.");
        }
    }

}
1
  • As I already stated in my comment to another of your answers, you should add, at least, what your code produces so that the OP can verify whether he gets the same result when he runs your code. By the way, the code in your other answer was also poorly formatted (before I edited it). Is this how you actually format your code? Or are you having trouble copying it to here?
    – Abra
    Commented Feb 21, 2024 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.