Rearrange the numbers of an array A such that the property of A holds A[0] less than or equal A[1] greater than or equal A[2] less than or equal A[3] .... O(n) time complexity.
Пікірлер: 4
@mith95012 жыл бұрын
public int[] maxMinAlteration(int ar[]) { for (var i = 0; i < ar.length; i++) { if (i == ar.length - 1) break; if (i % 2 == 0) { if (!(ar[i] = ar[i + 1])) swap(ar, i, i + 1); } } return ar; } private void swap(int[] ar, int i, int j) { var temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; }
@codesmart7602 жыл бұрын
Thanks for posting the code. I didn’t know KZbin allows posting code in comment section.
@zum43422 жыл бұрын
Hello, thanks for the well-explained theory. Would it also be possible to show how to translate the theory into code, preferably Java or Python?
@codesmart7602 жыл бұрын
Sure. I will link GitHub in the description, keep an eye on that.