Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

10. Sort Colors

MediumDay 3 of 60Arrays & Hashing

You are given an array nums where each value is 0, 1 or 2 (think red, white and blue). Sort it in place so that all 0s come first, then all 1s, then all 2s. Do not use a library sort.

Example 1
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2
Input: nums = [2,0,1]
Output: [0,1,2]
Constraints
  • 1 <= nums.length <= 300
  • nums[i] is 0, 1 or 2
Hints
Frequently asked at: Microsoft, Amazon, Adobe, Flipkart, Samsung
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min