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.