CF 102697118 - Colors

The problem works with RGB colors. A color is represented by three integers, R, G, and B, each between 0 and 255. We are given one such triple and must identify which of four named colors it represents: red, green, blue, or yellow.

CF 102697118 - Colors

Rating: -
Tags: -
Solve time: 1m 42s
Verified: yes

Solution

Problem Understanding

The problem works with RGB colors. A color is represented by three integers, R, G, and B, each between 0 and 255. We are given one such triple and must identify which of four named colors it represents: red, green, blue, or yellow. The four relevant RGB triples are (255, 0, 0), (0, 255, 0), (0, 0, 255), and (255, 255, 0). The output is the corresponding color name in uppercase.

There is only one input triple, and every component has a tiny fixed range of 256 possible values. Even an algorithm that examined every possible RGB triple would have only 256^3 = 16,777,216 combinations, although there is no reason to do that here. Since the required colors are explicitly known, the natural solution performs only a constant number of comparisons. The time and memory requirements are consequently independent of the input values.

The main edge case is distinguishing yellow from the three primary colors. For example, the input

255 255 0

must produce

YELLOW

A careless implementation that checks only whether R == 255 could classify this as red, because yellow also has its red component at the maximum value. The complete triple has to be compared.

Another useful boundary case is blue:

0 0 255

which produces

BLUE

Checking only whether one component is 255 is insufficient because every valid color has at least one component equal to 255. The positions of all three components determine the answer.

The statement describes inputs corresponding to one of the four named colors, so triples such as (0, 0, 0) or (255, 255, 255) are outside the problem's valid input domain. There is no required output for such triples, and a solution should not invent an extra color name for them.

Approaches

A brute-force interpretation would enumerate every possible RGB triple from (0, 0, 0) through (255, 255, 255) and compare each candidate against the input. There are exactly 256^3 = 16,777,216 candidates, so this performs up to roughly seventeen million checks in the worst case. It is far more work than the problem requires.

The brute-force works because the set of possible RGB values is finite and the four target colors are known. The observation that only four triples can ever be answers lets us remove the entire enumeration. We simply compare the input triple with those four known triples and print the matching name. At most four comparisons are needed.

This is essentially a direct lookup problem. There is no graph, dynamic programming state, search space, or arithmetic optimization hidden behind the RGB representation. The three numbers are just a compact encoding of one of four fixed values.

Approach Time Complexity Space Complexity Verdict
Brute Force O(256^3) O(1) Correct but unnecessarily slow
Optimal O(1) O(1) Accepted

Algorithm Walkthrough

  1. Read the three integers R, G, and B. They describe the red, green, and blue intensity components of the input color.
  2. Compare (R, G, B) with (255, 0, 0). If they are equal, the color is red, so print RED.
  3. Otherwise compare it with (0, 255, 0). Equality means the color is green, so print GREEN.
  4. Otherwise compare it with (0, 0, 255). Equality means the color is blue, so print BLUE.
  5. If none of those three triples matched, the valid-input guarantee leaves (255, 255, 0) as the remaining possibility. Print YELLOW.

Why it works

The algorithm checks the input against every possible valid answer. The four RGB triples are distinct, so an input cannot match two different colors. Since the problem guarantees that the input represents one of these four colors, exactly one comparison must identify it. The printed name is consequently the name associated with the input RGB triple.

Python Solution

import sys
input = sys.stdin.readline

def solve():
    r, g, b = map(int, input().split())

    if (r, g, b) == (255, 0, 0):
        print("RED")
    elif (r, g, b) == (0, 255, 0):
        print("GREEN")
    elif (r, g, b) == (0, 0, 255):
        print("BLUE")
    else:
        print("YELLOW")

if __name__ == "__main__":
    solve()

The first line reads the only input line and converts its three components to integers. Since there is exactly one test case, there is no test-case loop.

Each condition compares all three components simultaneously. This avoids the common mistake of identifying a color from only one component. For example, R == 255 is true for both red and yellow, so checking only R cannot distinguish them.

The final else is safe because the input is guaranteed to represent one of the four colors. It is also slightly cleaner than writing a fourth explicit equality test. There are no indexing or boundary issues, and Python integers easily handle the entire allowed range.

Worked Examples

Sample 1

Input:

255 0 0

The algorithm processes the following state.

R G B Comparison Result
255 0 0 (255, 0, 0) matches RED

The first comparison succeeds immediately, so no later condition is evaluated. The output is RED.

Sample 2

Input:

255 255 0

The state changes as follows.

R G B Comparison Result
255 255 0 (255, 0, 0) does not match Continue
255 255 0 (0, 255, 0) does not match Continue
255 255 0 (0, 0, 255) does not match Continue
255 255 0 Remaining valid color YELLOW

This example demonstrates why all three components must be considered. Both R and G are at their maximum, which distinguishes yellow from red and green.

Complexity Analysis

Measure Complexity Explanation
Time O(1) At most four RGB comparisons are performed
Space O(1) Only three input integers and a few constants are stored

The input domain is already tiny, but the constant-time solution is even simpler than enumerating that domain. It comfortably fits the stated 1 second time limit and 256 MB memory limit.

Test Cases

The official samples are the two cases shown in the statement. The custom cases below cover the other named colors and the boundaries of the RGB component range. Since the valid input domain contains only the four specified colors, an all-equal triple such as (0, 0, 0) is not a legal test case and cannot have a defined expected output.

import sys
import io

def solve():
    r, g, b = map(int, input().split())

    if (r, g, b) == (255, 0, 0):
        print("RED")
    elif (r, g, b) == (0, 255, 0):
        print("GREEN")
    elif (r, g, b) == (0, 0, 255):
        print("BLUE")
    else:
        print("YELLOW")

def run(inp: str) -> str:
    old_stdin = sys.stdin
    old_stdout = sys.stdout

    sys.stdin = io.StringIO(inp)
    sys.stdout = io.StringIO()

    solve()
    result = sys.stdout.getvalue()

    sys.stdin = old_stdin
    sys.stdout = old_stdout

    return result

# Provided samples
assert run("255 0 0\n") == "RED\n", "sample 1"
assert run("255 255 0\n") == "YELLOW\n", "sample 2"

# Custom cases
assert run("0 255 0\n") == "GREEN\n", "green"
assert run("0 0 255\n") == "BLUE\n", "blue"
assert run("255 255 0\n") == "YELLOW\n", "yellow boundary"
assert run("255 0 0\n") == "RED\n", "red boundary"
Test input Expected output What it validates
0 255 0 GREEN Green with the red and blue components at their minimum
0 0 255 BLUE Blue and the ordering of the RGB components
255 255 0 YELLOW Two components simultaneously at the maximum
255 0 0 RED Maximum red component with the other components at the minimum

The input (0, 0, 0) would be an all-equal-value case, but it is not one of the colors defined by the problem. Testing it would require inventing an output that the judge does not specify, so it should not be included in a correctness test for the submitted solution.

Edge Cases

The yellow case is the most likely source of a logical mistake. For the exact input

255 255 0

the first comparison against red fails because the green component is 255 instead of 0. The green comparison also fails because the red component is 255. The blue comparison fails because neither the red nor green components have the required values. The algorithm reaches the final branch and prints YELLOW, which is correct.

The blue case tests the position of the maximum component. For

0 0 255

the red comparison fails because R is 0, and the green comparison fails because G is 0. The blue comparison matches all three components and prints BLUE. A solution that checked only whether some component equals 255 would fail on this distinction.

The minimum component boundary is also covered by the primary colors. In

0 255 0

two components are exactly at their minimum value of 0, while the middle component is 255. The algorithm compares the entire triple and correctly prints GREEN.

Finally, the maximum RGB boundary appears in

255 255 0

where two components are simultaneously 255. The algorithm does not assume that exactly one component is maximal, so it correctly recognizes yellow.