> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/wave-array.md).

# Wave Array

Given an array of integers, sort the array into a wave like array and return it,\
In other words, arrange the elements into a sequence such that `a1 >= a2 <= a3 >= a4 <= a5.....`

**Example**

```
Given [1, 2, 3, 4]

One possible answer : [2, 1, 4, 3]
Another possible answer : [4, 1, 3, 2]
```

> **NOTE :** If there are multiple answers possible, return the one thats lexicographically smallest.\
> So, in example case, you will return `[2, 1, 4, 3]`&#x20;

```java
class Solution {
    public int[] wave(int[] A) {
        Arrays.sort(A);
        for (int i = 0; i < A.length; i += 2) {
            if (i == A.length - 1)
                break;
            int temp = A[i];
            A[i] = A[i + 1];
            A[i + 1] = temp;
        }
        return A;
    }
}
```
