Many problems can be formulated where one needs to verify if the elements of a vector satisfy various properties, but all can ultimately be reduced to one of the following:
A solution could be to count the elements that satisfy the rule. Finally:
Another, more efficient solution allows us to stop traversing when we are certain whether the vector satisfies the desired property or not. We'll use a boolean variable (with values true or false, 1 or 0, ...):
bool OK = true;
for(int i = 0 ; i < n ; i ++)
if(X[i] - does not satisfy the rule)
{
OK = false;
break;
}
bool OK = false;
for(int i = 0 ; i < n ; i ++)
if(X[i] - satisfies the rule)
{
OK = true;
break;
}