Day 3: HackerRank 30 Days Of Code Solution by CodingHuman | Intro to Conditional Statements

Day 3: HackerRank 30 Days Of Code Solution by CodingHuman | Intro to Conditional Statements

Day 3 introduces if, else if and else. You are given a number and you have to label it "Weird" or "Not Weird" using a small set of rules. The rules themselves are arbitrary, nobody actually calls 18 weird. The point is to practise turning a list of English conditions into code that checks them in the right order and never leaves a case unhandled.

This is a skill you will use in every program you ever write, from form validation to pricing logic. The trick is not memorising syntax. It is learning to read rules carefully, notice which cases overlap, and arrange your checks so each number lands in exactly one branch.

Problem

You are given a positive integer n. Decide its label like this:

  • If n is odd, it is Weird.
  • If n is even and between 2 and 5 (both included), it is Not Weird.
  • If n is even and between 6 and 20 (both included), it is Weird.
  • If n is even and greater than 20, it is Not Weird.

Input format: one line with the integer n.

Output format: print Weird or Not Weird.

Constraints: 1 ≤ n ≤ 100.

Input 1:
3
Output 1:
Weird

Input 2:
24
Output 2:
Not Weird

Approach

Look at the rules again and notice something: three of the four rules start with "if n is even". That tells you the first question to ask is "odd or even?". If the number is odd, you are done immediately. If it is even, you only need to work out which range it falls in.

  1. Check n % 2 != 0. The % operator gives the remainder after division. An odd number leaves remainder 1 when divided by 2. If this is true, print Weird.
  2. Otherwise n must be even. Check if it is from 2 to 5. Print Not Weird.
  3. Otherwise check if it is from 6 to 20. Print Weird.
  4. Anything left is even and above 20. Print Not Weird.

Because each else if only runs when all the checks above it failed, you do not need to repeat "and n is even" in every condition. The order of checks is doing that work for you.

Dry run with n = 3: 3 % 2 is 1, so the first check passes and we print Weird. With n = 24: 24 % 2 is 0, so we skip the first branch. 24 is not in 2..5, not in 6..20, so we reach the final else and print Not Weird.

Solution in Java

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.close();

        String answer;
        if (n % 2 != 0) {
            answer = "Weird";              // every odd number
        } else if (n >= 2 && n <= 5) {
            answer = "Not Weird";          // even, 2 to 5
        } else if (n >= 6 && n <= 20) {
            answer = "Weird";              // even, 6 to 20
        } else {
            answer = "Not Weird";          // even, above 20
        }
        System.out.println(answer);
    }
}

Some notes on the Java used:

  • scanner.nextInt() reads the next whole number from input and skips any spaces or newline before it.
  • && means "and". Both sides must be true. n >= 2 && n <= 5 is how you write "2 ≤ n ≤ 5" in Java, since Java does not allow the chained mathematical form.
  • Storing the result in answer and printing once at the end is a small habit that keeps output in one place. When a problem has many branches, this makes it harder to print twice by accident.

The earlier version of this solution checked evenness inside every branch and used N >= 20 in the third condition. It still gave correct output, because 20 was already caught by the 6..20 branch, but the overlapping ranges make the code harder to trust. The version above has ranges that do not overlap.

I tested it on 1, 2, 3, 4, 6, 18, 20, 22, 24 and 100. The boundary values 2, 6, 20 and 22 are the ones that matter most, since that is where off-by-one mistakes hide. All gave the expected labels.

Output

Weird

That is for input 3. For input 24 the program prints Not Weird.

Complexity

O(1) time and O(1) space. At most four comparisons run, whatever the input.

Common mistakes

  • Using < where the problem says "inclusive". n < 20 misses 20, which should be Weird.
  • Writing separate if statements instead of else if. Then a number can match more than one block and you print two lines.
  • Checking n % 2 == 1 for odd. It works here because n is positive, but in Java -3 % 2 is -1, so != 0 is the safer habit.
  • Small typos in the output, like Not weird with a lowercase w. The judge compares exact text.

The same problem appears in the Java track as Java If-Else. Next in this series: Day 4: Class vs. Instance.

Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.

More HackerRank solutions All coding problems MCQ practice

Comments

Answered 0/0Correct 0