19.7k views
1 vote
Write a java program to read an array of positive numbers. The program should find the difference between the alternate numbers in the array and find the index position of the smallest element with largest difference. If more than one pair has the same largest difference consider the first occurrence.

User Colonder
by
6.2k points

1 Answer

3 votes

Answer:

int s;

int[] num= new int[s];

Scanner sc = new Scanner(System.in);

System.out.printIn("Enter array size");

size = sc.nextInt();

for (i = 0; i < s; i++){

num[i] = sc.nextInt();

}

int[] difference;

int count = 0;

for (i = 0; i < num.length(); i= i + 2){

if (num[i] > num[i+1]){

difference[count] = num[i] -num[i+1];

}else{

difference = num[i+1] - num[i];

}

}

int max = 0;

for (int i : difference) {

if ( i > max){

max = i;

}

}

int result = Arrays.stream(difference).boxed().collect(Collectors.toList().indexOf(max);

System.out.printIn(result);

Step-by-step explanation:

The Java source code uses procedural method to execute a task. The program prompts for user inputs for the array size and the items in the array. Assuming the array has an even length, the adjacent items of the array are subtracted and stored in another array called difference. The index of the maximum value of the difference array is printed on screen.

User Fgiraldeau
by
5.2k points