Respuesta :
Answer:
Follows are the code to this question:
import java.util.*;//import package for user input
public class Main//defining a class Main
{
public static int minGap(int[] arr)//defining a static method minGap that holds an array in its parameters
{
int m,i,gap;//defining integer variables
if(arr.length <2)//use if block to check array element value
return (0); // return value 0
else//defining else block
{
m= 100;//use m to assign value
for (i=0;i<arr.length-1;i++)//use for loop to calculate difference
{
gap= arr[i]-arr[i+1];//holding difference values
if(gap<0)//use if to check gap less then 0
gap = gap * -1; // convert value into positive .
if( gap < m) // Finding the minimum difference
m = gap;//holding gap value
}
}
return (m);//return gaps
}
public static void main(String[] as)//main method
{
int n,i,diff;//defining integer variable
Scanner ox=new Scanner (System.in);//creating Scanner class object
System.out.println(" Enter the total value you want to insert in the array: ");//print message
n=ox.nextInt();//input value
int arr[]=new int[n];//defining an integer array
System.out.println("Enter array elements: ");//print message
for (i=0;i<arr.length;i++)//use for loop for input value
{
arr[i]=ox.nextInt();//input value in array
}
diff= minGap(arr);//calling a method minGap
System.out.println("Minimum Gap: "+diff);
}
}
Output:
Enter the total value you want to insert in the array:
5
Enter array elements:
1
3
6
5
12
Minimum Gap: 1
Explanation:
In this code, a static method "minGap" is declared, that accepts an array in its parameter and uses a conditional statement to check its minimum difference that can be defined as follows:
In the first if it checks the length of the array, that is less than 2 so, it will return the value 0, otherwise, it will go in else block, in this, it uses the loop to count the difference of array and use if block to check and return its minimum difference value.
In the main method, an integer variable and an array are declared, which is used for input value from the user end, and pass the value into the method and call and print its return value.