Knowee
Questions
Features
Study Tools

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.We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.You must solve this problem without using the library's sort function.

Question

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.We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.You must solve this problem without using the library's sort function.

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is known as the Dutch National Flag problem. Here is a Python solution using a two-pointer approach:

def sortColors(nums):
    red, white, blue = 0, 0, len(nums)-1

    while white <= blue:
        if nums[white] == 0:
            nums[red], nums[white] = nums[white], nums[red]
            white += 1
            red += 1
        elif nums[white] == 1:
            white += 1
        else:
            nums[white], nums[blue] = nums[blue], nums[white]
            blue -= 1
    return nums

Here's how it works:

  1. We initialize three pointers: red, white, and blue. red is positioned at the start of the array, white is positioned at the start as well, and blue is positioned at the end of the array.

  2. We start a while loop that continues until the white pointer is not greater than the blue pointer.

  3. Inside the loop, we check the value at the white index of the array. If it's 0 (red), we swap the values at the red and white indices, then increment both red and white. This is because we know the value at red must have been 1 (white), as we've already moved all the 0s to the start of the array.

  4. If the value at the white index is 1 (white), we don't need to swap anything, but we do increment white.

  5. If the value at the white index is 2 (blue), we swap the values at the white and blue indices, then decrement blue. We don't increment white here, as we need to re-check the value at the white index on the next iteration (as it's now a new value).

  6. The loop continues until white > blue, at which point we know the array is sorted. We return the sorted array.

This problem has been solved

Similar Questions

Which problem can be efficiently solved by applying the "Dutch National Flag algorithm," a variation of a sorting algorithm?Options: Pick one correct answer from belowTop K ElementsMeeting Rooms IISort ColorsIntersection of Two Arrays

Write a program to count the minimum number of front moves to sort an array.Note:Create an interface Move and declare a method, the class should implement the interface.Input format :The first line of input consists of the number of elements in the array.The second line of input consists of the array elements, separated by space.Output format :The output displays the minimum number of front moves to sort the given array.Code constraints :N- integer type(Natural numbers)Sample test cases :Input 1 :52 3 1 4 5Output 1 :1Input 2 :75 6 3 2 4 1 7Output 2 :4

You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:colors[i] == 0 means that tile i is red.colors[i] == 1 means that tile i is blue.Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.Return the number of alternating groups.Note that since colors represents a circle, the first and the last tiles are considered to be next to each other. Example 1:Input: colors = [1,1,1]Output: 0Explanation:Example 2:Input: colors = [0,1,0,0,1]Output: 3Explanation:Alternating groups: Constraints:3 <= colors.length <= 1000 <= colors[i] <= 1

Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.