Alternating Largest Problem Statement

Given a list of numbers, rearrange them such that every second element is greater than its adjacent elements. Implement a function to achieve this rearrangement.

Input:

The first line contains an integer T, indicating the number of test cases.
Each test case consists of a line with integer N, representing the number of elements.
The next line contains N integers describing the array.

Output:

For each test case, output the rearranged array on a single line.

Example:

Input:
1
5
3 1 2 5 4
Output:
1 3 2 5 4

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 10^5
  • 0 ≤ array[i] ≤ 10^5
  • Time limit: 1 second
Note:

No need to handle printing of the results; focus only on implementing the solution.

AnswerBot
6d

Rearrange a list of numbers such that every second element is greater than its adjacent elements.

  • Iterate through the array and swap elements if needed to satisfy the condition

  • Keep track of the current...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter