Cherry Biscuit

Easy
0/40
Average time to solve is 10m
profile
Contributed by
4 upvotes
Asked in company
Flipkart limited

Problem statement

Ninja loves cherry biscuits. He brings a packet of biscuits from a shop which contains ‘N’ biscuits. The number of cherries present in each biscuit is given in an array ‘A’.

Ninja likes a biscuit only if the number of cherries in that biscuit is strictly greater than ‘K’.

Can you tell the number of biscuits Ninja likes?

Example :
N = 3 
K = 5
A = [ 1, 6, 7 ]

Biscuit 1 : Since there is only 1 cherry in this biscuit which is not greater than ‘K’, Ninja doesn’t like this biscuit.

Biscuit 2 : Since there are 6 cherries in this biscuit which is greater than ‘K’, Ninja likes this biscuit.

Biscuit 3 : Since there are 7 cherries in this biscuit which is greater than ‘K’, Ninja likes this biscuit.

Hence, Ninja likes 2 biscuits from this packet.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.

The first line of each test case contains integer ‘N’.

The second line of each test case contains integer ‘K’.

The second line contains ‘N’ space separated integers denoting the elements of array ‘A’.
Output format :
For each test case, print one integer denoting the number of biscuits Ninja likes.

Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
0 <= K <= 10^9
0 <= A[i] <= 10^9

Time Limit: 1 sec
Sample Input 1 :
2
2
1
2 1
4
4
1 5 3 7
Sample Output 1 :
1
2
Explanation Of Sample Input 1 :
For test case 1 we have, 

Biscuit 1 : Since there are 2 cherries in this biscuit which is greater than ‘K = 1’, Ninja likes this biscuit.

Biscuit 2 : Since there is 1 cherry in this biscuit which is not greater than ‘K = 1’, Ninja doesn’t like this biscuit.

Hence, Ninja likes 1 biscuit from this packet.

For test case 2 we have,

Biscuits with cherries 5 and 7 respectively have values greater than ‘K = 4’, hence Ninja likes these biscuits.

Hence, we output 2.    
Sample Input 2 :
3
3
6
1 5 4
2
6
5 7    
3
0
1 2 3
Sample Output 2 :
0
1
3
Hint

Simulate the Problem Statement.

Approaches (1)
Optimal Approach

 

Approach : 

 

  • We traverse the array and check each value :
    • If it is greater than ‘K’, we increment our answer.
    • Else, we continue.


 

Algorithm : 

 

  • Initialise a variable ‘ans’ as 0.
  • Run a for loop over the length of array ‘A’ from ‘i’ = 1 to ‘i’ = ’N’.
    • If ‘A[i]’ > ‘K’ , we increment the ‘ans’ by 1.
    • Else, we continue.
  • Return the ‘ans’ as final result.
Time Complexity

 

O(N) , where ‘N’ is the length of the array ‘A’.
 

 

Since we are traversing the array once, the overall time complexity is O(N).

Space Complexity

 

O(1)
 

 

Constant extra space is required. Hence, the overall Space Complexity is O(1).

Code Solution
(100% EXP penalty)
Cherry Biscuit
Full screen
Console