X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Rearrange array such that even positioned are greater than odd
Easy Accuracy: 46.78% Submissions: 1477 Points: 2

Given an array A of n elements, rearrange the array according to the following relations :
, if i is even.
, if i is odd.[Considering 1-indexed array]
Return the resultant array.

Example:

Input 1:
A[] = {1, 2, 2, 1}
Output:
1 2 1 2
Explanation:
Both 2 are at even positions and 1 at odd satisfying
given condition

Input 2:
A[] = {1, 3, 2}
Output:
1 3 2
Explanation:
Here, the array is already sorted as per the conditions.

This is a function problem. The input is already taken care of by the driver code. You only need to complete the function assign() that takes an array (arr), sizeOfArray (n), and return the resultant array.
Note: Generated output will depict whether your returned answer is correct or wrong

Expected Time Complexity: O(NLOG(N)).
Expected Auxiliary Space: O(1).

Constraints:

1 ≤ N ≤ 105
1 ≤ arr[i] ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.