D
D
dmitrii20042021-07-23 09:33:26
C++ / C#
dmitrii2004, 2021-07-23 09:33:26

Find the number of numbers less than a given number?

The input is N integers, as well as a set of M requests, each of which is an integer. Your task is to find, for each query, the number of numbers from the original set that are less than the number specified in the query. Using the built-in binary search functions is prohibited.

Specifications Input

The first line contains the number N — the number of elements in the array. 1≤N≤250000.
The second line contains N space-separated integers Ai. −109≤Ai≤109.
The third line contains the number M — the number of requests. 1≤M≤250000.
The fourth line contains M space-separated integers Qi. −109≤Qi≤109.

Output

Print a single line with M integers — the numbers of numbers in the original array that are less than the corresponding query.

Examples
Input
Pin
5
1 5 3 2 1
2
4 3
Pin
4 3
Constraints
Execution time: 3 seconds

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
  unsigned int n;
  cin >> n;
  int* A = new int[n];
  for (int i = 0; i < n; i++)
  {
    cin >> A[i];
  }
  sort(A, A + n);
  int m;
  cin >> m;
  int x = m;
  int* B = new int[m];
  for (int i = 0; i < m; i++)
  {
    cin >> B[i];
  }
  int f = 250000;
  int* V = new int[f];
  for (int i = 0; i <m; i++)
  {
    int L = -1;
    int R = n;
    while (R - L > 1)
    {
      m = (R + L) / 2;
      if (A[m] < B[i])
      {
        L = m;
      }
      else
      {
        R = m;
      }
    }
    V[i] = R;
  }
  for (int i = 0; i < x; i++)
  {
    cout << V[i] << " ";
  }
}

Help me please ! The site says that the program gives the wrong answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-07-23
@dmitrii2004

array A must be sorted for binary search.
if(A[m<i])is there any error here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question