I need to process an array and find the index of an element whose value is less than a certain one (If there are several such elements, then find out the first occurrence).

I did something like this:

```
# include & lt; iostream & gt;
using namespace std;
int main ()
{
int N; cin & gt; & gt; N;
int A [N];
int total [N];
for (int i = 0; i & lt; N; i ++)
{
cin & gt; & gt; A [i];
total [i] = -1;
}
for (int i = 0; i & lt; N; i ++)
{
for (int j = i; j & lt; N; j ++)
{
if (A [i] & gt; A [j])
{
total [i] = j;
break;
}
}
}
for (int i = 0; i & lt; N; ++ i)
{
cout & lt; & lt; total [i] & lt; & lt; "";
}
return 0;
}
```

I have two questions:

The task itself: http: // informatics. mccme.ru/moodle/mod/statements/view.php?chapterid=112736#1

The program crashes on two tests and precisely because of the speed of execution.

## Answer 1, authority 100%

It’s not clear why you need O (N ^ 2)? and in general what you do – you don’t have any **definite **value. If it is `M `

, then enough

```
int i = 0;
for (; i & lt; N; ++ i) if (A [i] & lt; M) break;
```

Everyone. If `i `

is less than `N `

– this is the desired index, if it is `N `

– there is no such element.

With `find_if `

:

```
int * res = find_if (A, A + N, [] (int a) {return a & lt; M ;});
```

`res `

indicates the element to search for. If `res `

points outside of `A `

– to `A [N] `

– there is no such element.

But – again! You have clearly formulated the problem incorrectly! And I answer strictly your question – *find the index of an element whose value is less than a certain *.

P.S. Yeah, I looked at the original problem. In fact, instead of asking how it is more convenient to hammer in a nail, you asked how it is more convenient to hold the microscope when hammering in a nail … your method, and above the **other **method.