Skip to main content

TCS

TCS OA-14 2024

Problem Description

You are given an array nums consisting of n objects that are colored red, white, or blue. The colors are represented by the following integers:

  • Red: 3
  • White: 6
  • Blue: 7

Your task is to sort the array in-place such that:

  1. All objects of the same color are adjacent.
  2. The colors are arranged in the following order: red (3), white (6), and blue (7).

You must solve this problem without using the library's sort function.

Input Format

  • The first line contains an integer N, representing the array's length.
  • The second line contains N space-separated integers, representing colors.

Output Format

Print the sorted array according to the requirement mentioned.

Constraints

  • 1 ≤ arr.size() ≤ 10⁴

Example

Sample Input:

7
[6, 3, 7, 3, 6, 7, 3]

Sample Output:

[3, 3, 3, 6, 6, 7, 7]

Explanation:

After sorting, all 3s (red) are at the front, followed by all 6s (white), and finally all 7s (blue).