Problem Β· String

Closest Color

Learn this problem
● EasyTwo SigmaOA

Problem statement

A color is represented as a 24-bit integer, with 8 bits each for red (R), green (G), and blue (B) components. Each component ranges from 0 for low intensity to 255 for high intensity.

The distance between two colors with RGB values (r1, g1, b1) and (r2, g2, b2) is:

d = sqrt((r1 - r2)^2 + (g1 - g2)^2 + (b1 - b2)^2)

Pure colors

Compare each pixel with these five pure colors:

RGB values of the pure colors
Pure colorRGB
Black000
White255255255
Red25500
Green02550
Blue00255

Given a list of 24-bit binary strings representing pixel color values, determine which pure color each pixel is closest to.

If a pixel is equally close to multiple colors, use "Ambiguous".

Function

closestColor(pixels: String[]) β†’ String[]

Examples

Example 1

pixels = ["000000001111111100111100"]return = ["Green"]

Split the binary string into three components of 8 bits each:

  • R = "00000000", which is 0.
  • G = "11111111", which is 255.
  • B = "00111100", which is 60.

Thus, the pixel's RGB value is (0, 255, 60).

The smallest distance is to Green, so the result is "Green".

Constraints

  • Each value in pixels is a 24-bit binary string.
  • Each component is represented by 8 bits and ranges from 0 to 255.

More Two Sigma problems

drafts saved locally
public String[] closestColor(String[] pixels) {
  // write your code here
}
pixels["000000001111111100111100"]
expected["Green"]
checking account