java - Minimum gap between adjacent elements in an array -


i'm working on method prints minimum gap between 2 adjacent values in array full of integers. code compiles think formula might wrong. end result should 1 minimum value. suggestions?

public static void mingap(int[] list) { //pass array of random integers     int min = 0;     int gap = 0;     (int = 0; < list.length; i++) { //cycle through array         (int j = 0; j < list.length; j++) {             gap = ((i + 1) - i);         }         system.out.println("the minimum gap between 2 adjacent values " + gap + " ");     } } 

public static void mingap(int[] list) { //pass array of random integers    // initialize gap difference of first 2 elements of array                 int gap = math.abs(list[1] - list[0]);    // start array iteration second element have difference of first 2 elements.    for(int = 2; < list.length; i++) { //cycle through array       // calculate difference between 2 adjacent element.        int absdiff = math.abs(list[i] - list[i-1]);        // if difference less prior minimum becomes new minimum.       if(gap > absdiff) {                     gap = absdiff;       }    }    system.out.println("the minimum gap between 2 adjacent values " + gap + " ");  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -