There is an array with a random number of elements (even / odd). Suppose 50 < размер массива < 100 .
How to derive all the elements of an array by one according to the principle of "damped pendulum", namely: if 85, then:

 1, 85, 2, 84, 3, 83... 

And so to the last in the middle. It is required to calculate the difference between 1 and 85 , 2 and 84 , etc. to bind to the stream delay (no stream is needed). In the absence of a pair of the last element does not take into account the difference.

  • Where to bring all the elements of the array? On the screen, or in a new array, or somewhere else? At what point do you need to calculate the difference between pairs and where to save it? And what, in fact, the problem arose when you decide? - Regent
  • @Regent: on the screen, I'll link it there, the difference is the same. Two figures move towards each other, the length is random. - TimurVI
  • And the numbers in the array go in a row from 0 to длина - 1 , or are they random? - Regent
  • @Regent in a row, and there already work with the beginning and the end - TimurVI
  • By the way, a sample of 50, 100, 49, 99, 48, 98 does not resemble a damped pendulum, because there are always exactly 49 elements between the elements. Either it should be 1, 100, 2, 99, 3, 98 , or you picked up the wrong comparison. - Regent

2 answers 2

Given the special processing of the middle element in an array of odd length:

 public static void main(String[] args) { int[] values = new int[85]; for (int i = 0; i < values.length; i++) { values[i] = i + 1; } for (int i = 0; i < (values.length + 1) / 2; i++) { int first = values[i]; System.out.print(first + " "); if (2 * (i + 1) <= values.length) { int second = values[values.length - i - 1]; System.out.print(second + " " + (first - second)); } System.out.println(); } } 
  • one
    Thanks you. Your decision helped a lot. - TimurVI

Try this:

 int[] array = new int[100]; for (int i = 0, j = array.length - 1; i <= j; i++, j--) System.out.println(i + ":" + j + " = " + (jarray[j] - iarray[i])); 
  • And if the numbers in the array are not in a row and not from 0 to length - 1 ? Because in your version you don't need an array at all: you could just do j = 99 and that's it. - Regent
  • then i + ":" + j replaced with array[i] + ":" + array[j] . - Regent
  • And there is also an interesting moment about the absence of a pair of the middle element in an array of odd length: in theory, by condition, and the pair does not need to be derived, and the difference. - Regent
  • @Artem Konovalov: your example . The difference in zeros. But anyway, thanks a lot, butting with two cycles. - TimurVI
  • @TimurVI it is obvious that there the difference will be 0, the array is empty. it needs to be filled. But you yourself I think you can do. - Artem Konovalov