How to Solve Sort the Colors Problem?

How to Solve Sort the Colors Problem?

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

Introduction

Hey guys, Today is day 40 of the 100 Days to** LinkedIn Challenge.**

Image for post

Free For Kindle Readers

If you are Preparing for your Interview. Even if you are settled down in your job, keeping yourself up-to-date with the latest Interview Problems *is essential for your *career growth. Start your prep *from *Here!

Last month, I have been researching to find out the Frequently asked problems from these Companies. I have compiled *100 *of these questions, I am not promising you that you will get these questions in your interview but I am confident that most of these “interview questions” have similar logic and employs the same way of thinking from these set of challenges.

Before we move on to the first problem, If you are wondering why I chose LinkedIn, Yahoo and Oracle over FAANG are because I have completed a challenge Focusing on Amazon and Facebook Interview.

New Day, New Strength, New Thoughts

Day 40 — Sort Colors🏁

AIM

Given an array nums with n objects colored red, white, or blue, sort them in-placeso that objects of the same color are adjacent, with the colors in the order red, white, and blue.

Here, we will use the integers 01, and 2 to represent the color red, white, and blue respectively.

Follow up:

  • Could you solve this problem without using the library’s sort function?
  • Could you come up with a one-pass algorithm using only O(1) constant space?

Example🕶

Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Input: nums = [2,0,1]
Output: [0,1,2]

FollowHouse of Codes_ for keeping up to date in the programming interview world._

Code👇

    class Solution {
        public void sortColors(int[] nums) {
            int count0=0, count1=0, count2=0;
            for(int i=0; i<nums.length; i++){
                if(nums[i]==0){count0++;}
                else if(nums[i]==1){count1++;}
                else if(nums[i]==2){count2++;}
            }
            for(int i=0; i<nums.length; i++){
                if(i<count0){nums[i]=0;}
                else if(i<count0+count1){nums[i]=1;}
                else {nums[i]=2;}
            }
        }
    }

software-development interview coding programming java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Breadth-First Search (BFS)— Examples, Java Code, and Coding Interview

Breadth-first search, or BFS, is one of my must know algorithms for coding interviews. This article will go over what BFS is, show you how to code BFS in Java, and run through some coding interview tips for BFS.