Sort an Array of 0's, 1's, and 2's Problem Statement
Given an integer array ARR
of size 'N' containing only the values 0, 1, and 2, the task is to sort this array.
The goal is to achieve the sorting in a single pass through the array.
Example:
Input:
T = 2
N = 5
ARR = [0, 2, 1, 2, 0]
N = 3
ARR = [0, 1, 0]
Output:
[0, 0, 1, 2, 2]
[0, 0, 1]
Note:
The solution must involve iterating over the array just once.
Constraints:
- 1 <= T <= 10
- 1 <= N <= (5 * 105)
- 0 <= ARR[i] <= 2
- You need to sort the given array/list itself. No need to return or print anything explicitly.
AnswerBot
4d
Sort an array of 0's, 1's, and 2's in a single pass.
Use three pointers to keep track of 0's, 1's, and 2's positions while iterating through the array.
Swap elements based on the current element's value...read more
Help your peers!
Add answer anonymously...
Top Capgemini Engineering Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Capgemini Engineering Software Engineer
>
Capgemini Engineering Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app