VijayD
VijayD

Reputation: 820

Finding peak values from an array in a given range

The method getPeakCount takes an int array and a range (int) as an input and returns the number of integers that are greater than all the elements to either side for the given range.

For example, consider an array {1,4,2,6,4,5,10,8,7,11} and range 2. The result should be 3, as {..,4,2,6,4,5,..}, {..,4,5,10,8,7,..} and {..,8,7,11} satisfy this condition. These satisfy the condition because 6, 10 and 11 are all greater than the 2 elements to both their left and right.

Note that for the the corner elements like 1 and 11, there's no need to check the left and right side respectively.

My code is below, but it is not correct.

static int getPeakCount(int[] arr, int R) {
        int result=0;
        for(int i=0;i<arr.length;i++){
        if(i==0){
            if(arr[i]>arr[i+1]&&arr[i]>arr[i+2]){
                result++;
            }
             } //-----> closing if(i==0) condition
            else if(i==arr.length-1){
                if(arr[i]>arr[i-1]&&arr[i]>arr[i-2]){
                    result++;
                }

            }
            else if(i+R>arr.length){
                if(arr[i]>arr[i-R] && arr[i]>arr[i-R+1]){
                    System.out.println(arr[i]);
                    result++;
                }
            }
            else{

                if(arr[i]>arr[i+1] && arr[i]>arr[i+2] && arr[i]>arr[i-R] && arr[i]>arr[i-R+1]){
                    System.out.println(arr[i]);
                    result++;
            }
        }
    }
    return result;
}

I don't know whether I'm going in the right direction or not, and for last if condition it's throwing an java.lang.ArrayIndexOutOfBoundsException.

P.S. Don't consider this code as solution to remove errors from this. This is just the attempt I tried.

Upvotes: 2

Views: 4378

Answers (3)

timbo
timbo

Reputation: 1543

I think the right idea, and devnull is right. You just need to check the center, so change the loop to start at 1 and end 1 before the end. I commented out the end conditions. I think this does what you were asking, though not 100% sure I understood what you were after.

I should add, I use variables like l (left), r (right) and c (center) for clarity. You can make this much faster if you have large arrays. There is also redundancy in that it checks conditions it should know are already false (if I find a peak, I should skip the next value, as it can't also be a peak).

public class PeakChecker {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] array = new int[]{1, 4, 2, 6, 4, 5, 10, 8, 7, 11};

        System.out.println(nPeaks(array, 2));
    }

    static int nPeaks(int[] array, int range) {

        // Check for special cases
        if (array == null) {
            return 0;
        }

        int result = 0, l, r;

        // Check main body
        for (int i = 0; i < array.length; i++) {
            boolean isPeak = true;
            // Check from left to right
            l = Math.max(0, i - range);
            r = Math.min(array.length - 1, i + range);
            for (int j = l; j <= r; j++) {
                // Skip if we are on current
                if (i == j) {
                    continue;
                }
                if (array[i] < array[j]) {
                    isPeak = false;
                    break;
                }
            }

            if (isPeak) {
                System.out.println("Peak at " + i + " = " + array[i]);
                result++;
                i += range;
            }
        }

        return result;
    }
}

Upvotes: 3

djechlin
djechlin

Reputation: 60758

If you read the ArrayIndexOutOfBoundsException stack trace, it will tell you a line of code the error happened on. Look on that line of code and you'll probably see arr[i+1] or arr[i-1] or something. Certainly, at least one access on that line will be out of bounds. That's the problem.

Upvotes: 0

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

The last if condition shall throw exception when i == arr.length - 2.

This is because arr[i+2] in that case is out of bounds.

Upvotes: 0

Related Questions