Day 10 - Java

Problem Statement: Missing Number Color

You are given a list of numbers representing colors of the rainbow, except one number is missing. You need to find the missing number and print it, along with the colors of the rainbow excluding the missing number.

Write a Java program that takes the list of colors as input and outputs the missing number along with the remaining colors of the rainbow.

Here's how your program should work:

Read a line of input containing space-separated numbers representing colors of the rainbow.

Find the missing number from the rainbow.

Print the missing number.

Print the colors of the rainbow except for the missing number.

Print the colors of the rainbow separated by spaces.

Sample Input:

1 2 3 5 6 7

Sample Output:

The missing number is: 4

Colors without the missing number:

Red

Orange 

Yellow 


Blue 

Indigo 

Violet 


RAINBOW

Red Orange Yellow Green Blue Indigo Violet


Your program should implement the findMissingNumber, getColorForNumber, and getRainbowColors methods

findMissingNumber(List<Integer> numbers):

Calculate the missing number in the rainbow sequence given a list of numbers representing colors.

getColorForNumber(int number):

Return the color corresponding to the given number in the rainbow sequence.

getRainbowColors():

Return a list of all colors in the rainbow sequence.


Note: Assume that the colors of the rainbow are represented by numbers 1 to 7 in order: Red (1), Orange (2), Yellow (3), Green (4), Blue (5), Indigo (6), Violet (7).


SOLUTION

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import java.util.function.Consumer;


public class MissingNumberColor {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String arr = sc.nextLine();

        

        String[] str = arr.split(" ");

        List<Integer> num = new ArrayList<>();

        for (String i : str) {

            num.add(Integer.parseInt(i));

        }

        

        int missingNumber = findMissingNumber(num);

        System.out.println("The missing number is: " + missingNumber);

        

        Consumer<String> res1 = color -> {

            if (!color.equals(getColorForNumber(missingNumber))) {

                System.out.print(color + " ");

            }

            System.out.println();

        };

        

        System.out.println("Colors without the missing number:");

        getRainbowColors().forEach(res1);

        System.out.println("\nRAINBOW");

        getRainbowColors().forEach(color -> System.out.print(color + " "));

    }


    private static int findMissingNumber(List<Integer> numbers) {

        int n = numbers.size() + 1;

        int total = n * (n + 1) / 2;

        int sum = numbers.stream().mapToInt(Integer::intValue).sum();

        return total - sum;

    }


    private static String getColorForNumber(int number) {

        switch (number) {

            case 1: return "Red";

            case 2: return "Orange";

            case 3: return "Yellow";

            case 4: return "Green";

            case 5: return "Blue";

            case 6: return "Indigo";

            case 7: return "Violet";

            default: return "Unknown";

        }

    }


    private static List<String> getRainbowColors() {

        return List.of("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");

    }

}


Insights:
  • This program uses an ArrayList to store the parsed integers from the input string. This list is then used to find the missing number.
  • The findMissingNumber method calculates the missing number in the rainbow sequence using a mathematical formula based on the sum of the series.
  • The getColorForNumber method returns the color corresponding to a given number in the rainbow sequence using a switch-case statement.
  • The getRainbowColors method returns a list containing all the colors of the rainbow in order.
  • It utilizes the Consumer functional interface to print the colors without the missing number.
  • Lambda expressions are used to implement the Consumer interface for printing colors without the missing number and printing the colors of the rainbow separated by spaces.
  • The program prints the missing number. It also prints the remaining colors without the missing number and the entire rainbow sequence.
Colorful Coding! :) 🌈

Comments