Segregate Odd-Even Problem Statement

In a wedding ceremony at NinjaLand, attendees are divided into two groups: bride’s side and groom’s side. Attendees from the bride’s side hold odd numbers, while those from the groom’s side hold even numbers. Your task is to rearrange them so that all attendees from the bride’s side come first, followed by those from the groom’s side, while maintaining the original order within each group.

Input:

The first line contains an integer T, the number of test cases. Each test case consists of a single line with elements of a singly linked list separated by spaces and terminated with -1.

Output:

For each test case, output the bride’s side attendees followed by the groom’s side attendees, separated by spaces and terminated by -1, each test case output on a new line.

Example:

Input:
1 4 3 -1
Output:
1 3 4 -1
Explanation:

The input is a linked list 1 -> 4 -> 3. After rearranging, the list becomes 1 -> 3 -> 4, maintaining the original order of odd and even elements.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5 * 104
  • 0 <= data <= 104 and data ≠ -1
  • Time Limit: 1 sec

Note:

You don't need to print anything; just implement the function as the printing is already handled.
AnswerBot
4mo

Rearrange attendees from bride's side and groom's side while maintaining original order within each group.

  • Iterate through the linked list and separate odd and even numbers into two separate lists.

  • Merg...read more

Help your peers!
Select
Add answer anonymously...

ThoughtWorks Software Engineer interview questions & answers

A Software Engineer was asked 5mo agoQ. Explain cloud concepts.
A Software Engineer was asked Q. Which tech stack would you use for which case?
A Software Engineer was asked Q. Describe a real-time situation and how you would code it using OOP concepts.

Popular interview questions of Software Engineer

A Software Engineer was asked 5mo agoQ1. Explain cloud concepts.
A Software Engineer was asked Q2. Describe a real-time situation and how you would code it using OOP concepts.
A Software Engineer was asked Q3. Write a program to print the HCF of numbers as output.
ThoughtWorks Software Engineer 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