To remove the indices of those elements whose values are higher than the previous element
-
That's the question. How to remove the indexes of that element, the values of which are greater than the previous element (from the second). The only solution to this task comes to mind, but the programme does not compare the last element with the penultimate, I cannot understand what it is. Thank you.
public class test { public static void main(String[] args) { int[] a; // массив целых чисел int n; // количество элементов в массиве Scanner in = new Scanner(System.in); System.out.println("Введите количество эл-ов"); n = in.nextInt(); a = new int[n];
for (int i = 0; i < n; i++) { a[i] = (int) (-20 + (Math.random() * (40 + 20))); } for (int i = 0; i < a.length; i++) { System.out.println(i + ": " + a[i]); } /* интересуемая часть */ int y = n; for (int i = 1; i < y - 1; i++) { if (a[i + 1] > a[i]) System.out.print((i) + " "); } }
}
-
If it's the second, it's all right, cycle one. But according to the description, we need to remove those who are more than the previous one. So try this:
/* интересуемая часть */ for (int i = 1; i < n ; i++) { if (a[i] > a[i - 1]) { System.out.print((i) + " "); } }