-4

I need to check if A[0] ^ A[1] ^ A[2] ... ^ A[N] is even or odd.

Is this code right??

#include <bits/stdc++.h>
#include <cmath>
using namespace std;

int main(){
    long long n;
    cin >> n;
    int a[n];
    long int mp;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        mp = pow(a[i], a[i+1]);
    }
    if (mp % 2 == 0){
        cout << "YES";
    }
    else cout<<"NO";
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    No because 1. `a[i+1]` is used when it is not initialized and it may be out-of-range. 2. Using `pow` for integer calculation is not a good idea because it is for floating-point number and it may produce some errors. 3. `pow` may overflow, You should use modulo in your calculation. 4. Variable-Length Array like `int a[n];` is not in the standard C++. You should use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – MikeCAT Jun 30 '21 at 15:23
  • 2
    The question is simply about `a[0]` raised to some power. When will `a[0]` raised to some power be even, and when will it be odd? If that power is greater than 0, the answer does not depend on what that power is. – Pete Becker Jun 30 '21 at 15:27
  • Also, shouldn't it be "mp = pow( mp, a[i] )"? Also, also, since exponentiation is right-associative, I think you have to first collect the terms, then process them in reverse order. – Mark Lavin Jun 30 '21 at 15:28
  • 2
    Check with He-Man. Apparently he has the POWER! – user4581301 Jun 30 '21 at 15:43
  • 2
    are you sure you correctly understood the tasks? Because if it is what you wrote in the question then you are on the wrong track. You dont need to actually raise any number to the power of another if you just do the maths first (what Pete already said) – 463035818_is_not_an_ai Jun 30 '21 at 15:54

1 Answers1

3

Do the maths first.

Consider that

odd * odd * odd * odd .... * odd == odd

You can multiply any odd factors and the result is always odd. Whether a number is odd or even is equivalent to: It has a prime factor 2. Some integer raised to some other integer cannot remove a prime factor, it can also not add a prime factor when it wasn't present before. You start with some x and then

 x * x * x * x * x .... * x = y

has the same prime factors as x, just with different powers. The only exceptions is to get an odd number from an even one when you raise a number to power 0, because x^0 = 1.

Ergo, you are on the wrong track. Instead of brute force raising numbers to some power you merely need to consider ...

  • is A[0] odd or even
  • is any of the other elements 0 (remember that (a^b)^c) is just a^(b*c))

Thats it.

I will not write the code for you to not spoil the exercise. But I should tell you whats wrong with your code: pow is not made to be used with integers. There are many Q&As here about pow returning a "wrong" result, which is most often just due to wrong expectations. Here is one of them: Why does pow(n,2) return 24 when n=5, with my compiler and OS?. Moreover, you are accessing the array out of bounds in the last iteration of the loop. Hence all your code has undefined behavior. Output could be "maybe" or something else entirely. Also, your code merely calculates a[i] ^ a[i+1] and after the loop you only consider the very last result. Thats not what the task you describe asks for. Last but not least, Why aren't variable-length arrays part of the C++ standard?. Use std::vector for dynamically sized arrays.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185