Memorise Me! | Competitive Programming Questions with Answers in Java

Competitive Programming Questions with Answers in Java

Memorise Me!

Problem

Arijit is a brilliant boy. He likes memory games. He likes to participate alone but this time he has to have a partner. So he chooses you.

In this Game , your team will be shown N numbers for few minutes . You will have to memorize these numbers.

Now, the questioner will ask you Q queries, in each query He will give you a number , and you have to tell him the total number of occurrences of that number in the array of numbers shown to your team . If the number is not present , then you will have to say “NOT PRESENT” (without quotes).

Input and Output
The first line of input will contain N, an integer, which is the total number of numbers shown to your team.

The second line of input contains N space separated integers .

The third line of input contains an integer Q , denoting the total number of integers.

The Next Q lines will contain an integer denoting an integer, Bi , for which you have to print the number of occurrences of that number (Bi) in those N numbers on a new line.

If the number Bi isn’t present then Print “NOT PRESENT” (without quotes) on a new line.

Constraints
1≤N≤105
0≤Bi≤1000
1≤Q≤105

Sample Input
6
1 1 1 2 2 0
6
1
2
1
0
3
4

Sample Output

3
2
3
1
NOT PRESENT
NOT PRESENT

Solution

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
        int n, i, j, q;
        
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        int a[] = new int[n];
        for(i=0; i<n; i++)
        {
            a[i] = s.nextInt();
        }
        q = s.nextInt();
        int count[] = new int[q];
        int b[] = new int[q];
        for(i=0; i<q; i++)
        {
            b[i] = s.nextInt();
        }
        for(i=0; i<q; i++)
        {
            count[i]=0;
            for(j=0; j<n; j++)
            {
                if(a[j] == b[i])
                    count[i]++;
            }
            if(count[i]==0)
                System.out.println("NOT PRESENT");
            else
                System.out.println(count[i]);
        }
    }
}

Comments