Knowee
Questions
Features
Study Tools

Given an array, you have to find the frequency of a number x.Input FormatThe first line of input contains N - size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the frequency of X in the given array.Output FormatFor each query, print the frequency of X, separated by a new line.Constraints20 points1 <= N <= 1051 <= Q <= 102-108 <= ar[i] <= 10830 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 10850 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 108ExampleInput9-6 10 -1 20 -1 15 5 -1 155-11081520Output31021

Question

Given an array, you have to find the frequency of a number x.Input FormatThe first line of input contains N - size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the frequency of X in the given array.Output FormatFor each query, print the frequency of X, separated by a new line.Constraints20 points1 <= N <= 1051 <= Q <= 102-108 <= ar[i] <= 10830 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 10850 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 108ExampleInput9-6 10 -1 20 -1 15 5 -1 155-11081520Output31021

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is about finding the frequency of a specific number in an array. Here's a step-by-step guide on how to solve it:

  1. Read the size of the array (N) from the input.
  2. Read the elements of the array from the input.
  3. Read the number of queries (Q) from the input.
  4. For each query, do the following:
    • Read the number (X) for which you have to find the frequency.
    • Count the number of times X appears in the array.
    • Print the count.

In Python, you can use the count() function of a list to count the number of times an element appears in the list. Here's a simple implementation:

N = int(input())
array = list(map(int, input().split()))
Q = int(input())
for _ in range(Q):
    X = int(input())
    print(array.count(X))

This code reads the size of the array and the elements of the array. Then it reads the number of queries. For each query, it reads the number to find, counts the number of times it appears in the array, and prints the count.

In the given example, the array is [-6, 10, -1, 20, -1, 15,

This problem has been solved

Similar Questions

You are given an array of integers. Sort them by frequency. See examples for more clarifications.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the elements of the array sorted by frequency. In case 2 elements have the same frequency, print the smaller element first.Constraints1 <= T <= 1001 <= N <= 10000-1000 <= A[i] <= 1000ExampleInput264 -2 10 12 -8 48176 -272 -272 -45 269 -327 -945 176Output-8 -2 10 12 4 4-945 -327 -45 269 -272 -272 176 176

Given an array, you have to find the ceil of a number x. The ceil of a number x is nothing but the smallest number in the array greater than or equal to x.Input FormatThe first line of input contains the N-size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the ceil of X in the given array.Output FormatFor each query, print the ceil of X, separated by a new line. If ceil is not found, print the value of "INT_MAXConstraints30 points1 <= N <=1051 <= Q <=102-108 <= ar[i] <=10870 points1 <= N <=1051 <= Q <=105-108 <= ar[i] <=108

Given an array, you have to find the floor of a number x. The floor of a number x is nothing but the largest number in the array less than or equal to x.Input FormatThe first line of input contains the N - the size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the floor of X in the given array.Output FormatFor each query, print the floor of X, separated by a new line. If the floor is not found, print the value of "INT_MIN".Constraints30 points1 <= N <= 1051 <= Q <= 102-108 <= ar[i] <= 10870 points1 <= N <= 1051 <= Q <= 105-108 <= ar[i] <= 108ExampleInput6-6 10 -1 20 15 55-1108-10-4Output-1105-2147483648-6

Given an array of size 3X+1, where every element occurs three times, except one element, which occurs only once. Find the element that occurs only once.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array (of the form 3X + 1) and second line contains the elements of the array.Output FormatFor each test case, print the number which occurs only once, separated by new line.Constraints30 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 1001 <= N <= 106ExampleInput2105 7 8 7 7 9 5 9 5 9710 20 20 30 20 30 30Output810

You are given an array of integers. For each element in the array, find the number of smaller elements on the right side and print the total count.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the sum of count of smaller elements on right side of each element in the array, separated by new line.Constraints30 points1 <= N <= 10370 points1 <= N <= 105General Constraints1 <= T <= 100-104 <= A[i] <= 104ExampleInput254 10 54 11 8615 35 25 10 15 12Output410ExplanationTest Case 1Smaller Elements on right side of 4 : 0Smaller Elements on right side of 10 : 1Smaller Elements on right side of 54 : 2Smaller Elements on right side of 11 : 1Smaller Elements on right side of 8 : 0Total Count = 0 + 1 + 2 + 1 + 0 = 4Test Case 2Smaller Elements on right side of 15 : 2Smaller Elements on right side of 35 : 4Smaller Elements on right side of 25 : 3Smaller Elements on right side of 10 : 0Smaller Elements on right side of 15 : 1Smaller Elements on right side of 12 : 0Total Count = 2 + 4 + 3 + 0 + 1 + 0 = 10

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.