[Solved] Saurav and his friend playing cards HackerRank

Saurav and his friend playing cards. Both are going to pick four cards from the deck and show the cards. If anyone got higher number of even or odd number cards then he will win the game. Write a program to check who wins the game or game get tie.

Input Format

First line shows four space separated values of four cards picked by Saurav.
Second line will contain four space separated values of four cards picked by Saurav’s Friend.

Constraints

Values will be between 1-13

Output Format

Output will be Saurav Won or Saurav Lost or Tie

Sample Input 0

4 5 13 2
8 6 9 10

Sample Output 0

Saurav Lost

Sample Input 1

5 7 9 13
2 4 8 10

Sample Output 1

Tie

Sample Input 2

5 7 9 15
12 11 10 7

Sample Output 2

Invalid Input

Solution

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
       Scanner sc = new Scanner(System.in);

        int[] sauravCards = new int[4];
        int[] friendCards = new int[4];

        // Input for Saurav's cards
        for (int i = 0; i < 4; i++) {
            sauravCards[i] = sc.nextInt();
            if (sauravCards[i] < 1 || sauravCards[i] > 13) {
                System.out.println("Invalid Input");
                return;
            }
        }

        // Input for friend's cards
        for (int i = 0; i < 4; i++) {
            friendCards[i] = sc.nextInt();
            if (friendCards[i] < 1 || friendCards[i] > 13) {
                System.out.println("Invalid Input");
                return;
            }
        }
        int sauravEven = 0;
        int sauravOdd = 0;
        int friendEven = 0;
        int friendOdd = 0;

        // Count even and odd numbers for Saurav's cards
        for (int i = 0; i < 4; i++) {
            if (sauravCards[i] % 2 == 0) {
                sauravEven++;
            } else {
                sauravOdd++;
            }
        }

        int sauravMax = Math.max(sauravEven,sauravOdd);

        // Count even and odd numbers for friend's cards
        for (int i = 0; i < 4; i++) {
            if (friendCards[i] % 2 == 0) {
                friendEven++;
            } else {
                friendOdd++;
            }
        }

        // sauravhathi

        int friendMax = Math.max(friendEven,friendOdd);

        // Saurav Won or Saurav Lost or Tie or Invalid Input
        if (sauravMax > friendMax) {
            System.out.println("Saurav Won");
        } else if (sauravMax < friendMax) {
            System.out.println("Saurav Lost");
        } else {
            System.out.println("Tie");
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *