Parth's OCD Challenge

Parth, a budding programmer, has received an array 'ARR' of 'N' positive integers. His OCD is triggered whenever an odd number appears at an even index or an even number at an odd index in the array. Your task is to reorder 'ARR' so that these conditions are not violated and help Parth keep his peace.

Example:

Input:
ARR = [7, 4, 21, 1, 8, 18]
Output:
[4, 7, 8, 21, 18, 1]

Constraints:

  • The array has equal numbers of odd integers and odd-index positions and vice-versa.
  • The problem input assumes a 0-based index.
Note:

Your solution does not need to print anything; the array is modified in place.

Example:

Alternate outputs could be [18, 1, 8, 21, 4, 21], among others.

Constraints:

  • 1 <= T <= 10^2
  • 1 <= N <= 10^3
  • 0 <= A[i] <= 10^9
Note:

You don’t need to print anything; it has already been handled.

Follow Up:

Is it possible to achieve this in O(1) auxiliary space?

VenkataHemant Kumar Reddy Challa
1y
student at
Amrita School of Engineering, Bangalore

Take two pointers say p1, p2. Run p1 through odd positions, p2 through even positions, swap mismatches. (key : odd mismatches happen in even positions and even mismatches happen in odd positions, we j...read more

Help your peers!
Add answer anonymously...
Paytm Software Developer Intern 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