Category Archives: Hackerrank Problem

Weird Numbers

Given an integer as input, can you check the following:

  • If   N is odd then print “Weird”
  • If N is even and, in between range 2 and 5(inclusive), print “Not Weird”
  • If   N is even and, in between range 6 and 20(inclusive), print “Weird”
  • If is even and ,N>20  print “Not Weird”

Java solution for this problem is given below:

 

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n%2 !=0){
            System.out.println("Weird");
        }
        else if((n>=2 &&n<=5)&& n%2==0){
            System.out.println("Not Weird");
        }
        else if(n>=6 && n<=20 && n%2==0){
            System.out.println("Weird");
        }else{
            System.out.println("Not Weird");
        }
    }
}

 

Advertisements

Set .add()

Task

Apply your knowledge of the .add() operation to help your friend Rupal.

Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of N country stamps.

Find the total number of distinct country stamps.

Input Format

The first line contains an integer N, the total number of country stamps.
The next N lines contains the name of the country where the stamp is from.

 

num = int(input())
country_name = set()
for i in range(num):
    country_name.add(input())
print(len(country_name))

List Comprehensions

Let’s learn about list comprehensions! You are given three integers X,Y and Z representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by  (i,j,k) on a 3D grid where the sum of  i+j+k  is not equal to N . Here, 0<=i<=X;0<=j<=Y;0<=k<=Z

Input Format

Four integers  X,Y,Z and N  each on four separate lines, respectively.

Constraints

Print the list in lexicographic increasing order.

x,y,z,n=[int(input())for i in range(4)]
p=[[a,b,c]for a in range(x+1)for b in range(y+1)for c in range(z+1) if a+b+c!=n]
print(p)

Game of Stones

Two players (numbered 1 and 2 ) are playing a game with n stones. Player 1 always plays first, and the two players move in alternating turns. The game’s rules are as follows:

  • In a single move, a player can remove either 2 ,3 , or 5 stones from the game board.
  • If a player is unable to make a move, that player loses the game.

Given the number of stones, find and print the name of the winner (i.e., First or Second) on a new line. Each player plays optimally, meaning they will not make a move that causes them to lose the game if some better, winning move exists.

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt();
            if(n%7==0||n%7==1){
                System.out.println("Second");
            }else{
                System.out.println("First");
            }
        }
    }
}

Mini-Max Sum

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

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

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long arr[]=new long[5];
arr[0]=in.nextLong();
long min=arr[0],max=arr[0],sum=arr[0];

for(int i=1;i<5;i++){
arr[i]=in.nextLong();
if(maxarr[i])
min=arr[i];
sum=sum+arr[i];
}
System.out.println((sum-max)+” “+(sum-min));

}
}

Matching Specific String

Task

You have a test string S. Your task is to match the string hackerrank. This is case sensitive.

Note

This is a regex only challenge. You are not required to write code.
You only have to fill in the regex pattern in the blank (_________).

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Regex_Test tester = new Regex_Test();
        tester.checker("hackerrank");
    }
}
class Regex_Test {
    public void checker(String Regex_Pattern){
        Scanner Input = new Scanner(System.in);
        String Test_String = Input.nextLine();
        Pattern p = Pattern.compile(Regex_Pattern);
        Matcher m = p.matcher(Test_String);
        int Count = 0;
        while(m.find()){
            Count += 1;
        }
        System.out.format("Number of matches : %d",Count);
    }
}

CamelCase

Alice wrote a sequence of words in CamelCase as a string of letters,s , having the following properties:

1. It is a concatenation of one or more words consisting of English letters.
2. All letters in the first word are lowercase.
3. For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, print the number of words in s on a new line.

Input Format:

A single line containing string s.

Constraints:
1<=|s|<=10^5

Output Format:

Print the number of words in string s.

Sample Input:

saveChangesInTheEditor

Sample Output

5

Explanation:

String s contains five words:

1. save
2. Changes
3. In
4. The
5. Editor

Thus, we print 5 on a new line.

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

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String[]arr=s.split("[A-Z]");
System.out.println(arr.length);

}
}