Problem Β· String
Closest Color
Learn this problemProblem 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:
| Pure color | R | G | B |
|---|---|---|---|
| Black | 0 | 0 | 0 |
| White | 255 | 255 | 255 |
| Red | 255 | 0 | 0 |
| Green | 0 | 255 | 0 |
| Blue | 0 | 0 | 255 |
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 is0.G = "11111111", which is255.B = "00111100", which is60.
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
pixelsis a 24-bit binary string. - Each component is represented by
8bits and ranges from0to255.
More Two Sigma problems
- Balanced Split String with WildcardsOA Β· Seen Jul 2026
- Linear InterpolatorOA Β· Seen Jun 2026
- Online No-Intercept Linear RegressionOA Β· Seen Jun 2026
- Piecewise Linear Interpolation and ExtrapolationOA Β· Seen May 2026
- Sewer Drainage PartitionOA Β· Seen Mar 2026
- Calculate y/x using PatchOA Β· Seen Nov 2025
- Nums That Are Divisible by N πSeen Mar 2024
- Replacing Val πSeen Mar 2024