[Solved] C++ function that returns 0,1 or -1


Check the number n times whether if it’s even or odd. Create two counters for even and odd and increment counters accordingly. In the end, return -1 for even/odd, 0 for even and 1 for odd.

Note – In C, C++ the non-zero value is true while zero is taken as false.

int check(int n){
    int even=0, odd=0, num;

    for(int i=0;i<n;i++){
        cin>>num;
        if(num % 2 == 0)
            even++;
        else
            odd++;
        }

    if(even && odd){ 
        return -1;
    }else if(even){
        return 0;
    }else
        return 1;
}

solved C++ function that returns 0,1 or -1