### The minimum position in vector of the number x The program reads from the keyboard, on the first line, the natural number n. On the next line reads the string of n natural numbers, sorted in ascending order. From the third line we read a natural number m, and from the fourth we read m natural numbers, representing possible values for x.
Displays, for each of the m numbers read, the minimum position in the initial string.

Input :
10
1 2 3 4 4 4 5 6 6 7
4
2 4 5 6
Output:
2 4 7 8

Change my code please.

 ``12345678910111213141516171819202122232425`` `````` #include using namespace std; int main() { int n, m ,i, j, v, w, ok = 0; cin >> n; for (i = 1; i <=n; i++) { cin >> v[i]; } cin >> m; for (j = 1; j <= m; j++) { cin >> w[j]; for (i = 1; i <= n; i++) { if ( v[i] == w[j]) { cout << i << " "; } } } return 0; } `````` ``1234567891011121314151617181920`` ``````#include #include using namespace std; int main() { vector v; int n; cin >> n; for (int i = 0, x; i < n && cin >> x; ++i) v.push_back(x); int m; cin >> m; for (int i = 0, j = 0, x; i < m && cin >> x; ++i) { while (j < n && x > v[j]) ++j; cout << j + 1 << ' '; } cout << '\n'; }``````

Last edited on And if you need to find the maximum position of x?
Input:
10
1 2 3 4 4 4 5 6 6 7
4
2 6 5 4

Output :
2 9 7 6 Note that dutch's solution does optimize on assumption that both input sequences have ascending order.

More generically, you must find x from v and report the position, if found.

For reporting the last position you should find from v in reverse order; start search from last, rather than first element.
Topic archived. No new replies allowed.