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?

AnswerBot
4mo

Reorder array to avoid odd numbers at even indices and even numbers at odd indices.

  • Swap odd numbers at even indices with the nearest odd number at an odd index, and vice versa.

  • Use two pointers approac...read more

Help your peers!
Select
Add answer anonymously...

Paytm Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q2. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q3. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more
Paytm Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits