-2

Problem Statement

Task

Given an integer,n, perform the following conditional actions:

If n is odd, print Weird If n is even and in the inclusive range of 2 to 5, print Not Weird If n is even and in the inclusive range of 6 to 20, print Weird If n is even and greater than 20, print Not Weird Complete the stub code provided in your editor to print whether or not n is weird.

Input Format

A single line containing a positive integer,n.

Constraints

1<=n<=100

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0

3

Sample Output 0

Weird

Sample Input 1

24

Sample Output 1

Not Weird

Explanation

Sample Case 0: n=3 n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n=24 n>20 and n is even, so it isn't weird. Thus, we print Not Weird.

My solution to the problem.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        if(N%2==0){
            if(N>1){
                if(N<6){
                    System.out.println("Not Weird");
                }
                else if(N<21){
                    System.out.println("Weird");
                }
                else{
                    System.out.println("Not Weird");
                }
            }
        }
        else{
            System.out.println("Weird");
        }

        scanner.close();
    }
}

Can I get suggestions regarding the solution. Thank You.

KamalAres
  • 11
  • 1
  • 5
  • 6
    If the code works as intended, it might be better suited for https://codereview.stackexchange.com/. – Turing85 Mar 07 '21 at 16:53
  • Redefining the requirements will give you a much shorter piece of code. Think about how the two "Not Weird" conditions are all part of the same check, and how `[6, 20]` contains the only difference in that domain – Rogue Mar 07 '21 at 16:55
  • There's also plenty of well articulated explanations in the answers section of each problem on HackerRank. Sometimes there's a tab on the question itself too that explains different approaches, although there might be a paywall. – bdehmer Mar 07 '21 at 17:01
  • What's a "better solution" is the part of your question that would need to be refined. If it is to be evaluated in terms of ease of maintenance, I find yours pretty legible, and as such quite good : it reflects business rules, so it's quite easy to update if requirements were to change. As opposed to one-liners, for instance. And performancewise, no input value requires more than 4 tests ; one-liners wouldn't test less, I guess. Maybe create and use constants for strings, as they might change in bulk. My 20 cents. Follow suggestions by @Turing85 and bdehmer – Alain BECKER May 25 '22 at 21:08

2 Answers2

1

The conditions should be merged: the result is Weird for any number in the range 6 <= n <= 20 OR n is odd, otherwise it is Not Weird:

System.out.println((6 <= n && n <= 20 || n % 2 != 0) ? "Weird" : "Not Weird");
// or
System.out.println((5 < n && n < 21 || n % 2 != 0) ? "Weird" : "Not Weird");
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Actually, I am getting wrong output for your code. Since it is a necessary condition for N to be even then only we can check for the range. Here is the problem link can you try and come with better code. https://www.hackerrank.com/challenges/java-if-else/problem – KamalAres Mar 07 '21 at 17:17
  • You could have run a code with a typo, which I edited out. – Nowhere Man Mar 07 '21 at 17:24
  • Oh, yes thank you. Now I got the correct answer. Previously I wrote this System.out.println((5 – KamalAres Mar 07 '21 at 17:30
  • System.out.println((6 <= n && n <= 20 || n % 2 != 0) ? "Weird" : "Not Weird"); so you have combined odd part and inclusive range of 6 and 20. Another one question is it good to use <=20 or <21. Which is better? – KamalAres Mar 07 '21 at 17:35
  • `< 21` is shorter, other than that there's no difference for integer numbers – Nowhere Man Mar 07 '21 at 18:15
0
 if (~N % 2 == 0 || N <= 20 && N >= 6) {
   System.out.println("Weird");
} else if (N % 2 == 0 && N > 20 || (N >= 2 && 5 <= N)) {
   System.out.println("not Weird");
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P May 22 '22 at 13:29