0
import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone {
    public static void main(String[] args) throws java.lang.Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        for (int i = 0; i < n; i++) {
            arr[i] = Math.abs(arr[i + 0] - arr[i + 1]);
        }
        Arrays.sort(arr);
        System.out.println(arr[arr.length - 1]);
    }
}

I was trying to find the maximum difference between consecutive array elements, but while running my IDE is giving a runtime error.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
swaap
  • 1
  • Could you share a link to your program on ideone? – Sergey Kalinichenko Jun 28 '16 at 19:59
  • When you are evaluating the absolute difference, think about the case where `i = n - 1` (the last one). The issue is that there are no elements after `arr [n - 1]`. – Tunaki Jun 28 '16 at 20:01
  • What's the point of `i + 0`? – bcsb1001 Jun 28 '16 at 20:05
  • yeah i got it.but the thing is during evaluating absolute difference where i = n -1..what will be second operand?because if i = n - 1 then arr[ n - 1 ] = arr [ n - 1 ] - arr[ n ] which will cause ArrayIndexOutOfBoundsException..@Tunaki – swaap Jul 09 '16 at 09:07

0 Answers0