How to check there are negative elements in a specific range in an array with C++

1 Answer

0 votes
#include <iostream>     
#include <algorithm>    
 
int main () {
    std::array<int, 9> arr = {3, 4, 8, -1, 9, 2, -7, 6, 0};
 
    for (auto it = arr.begin() + 2; it != arr.end() - 3; it++) {
        std::cout << *it << ' ';
    }   
     
    if (std::any_of(arr.begin() + 2, arr.end() - 3, [](int n){return n < 0;}))
        std::cout << "\nyes";
    else
        std::cout << "\nno";
 
    return 0;
}
 
 
 
  
/*
run:
        
8 -1 9 2 
yes
        
*/

 



answered Dec 3, 2021 by avibootz
edited Dec 3, 2021 by avibootz
...