Sort by

recency

|

145 Discussions

|

  • + 0 comments

    Thanks for sharing valuable post Sabexch

  • + 0 comments

    This code defines a recursive function find(x, y) to determine whether the value of pow(A[x], find(x+1, y)) is odd or even. World 777

  • + 0 comments

    This is bullshit. Problem statement is vague and does not walk through any example. I wish I could give this negative star.

  • + 1 comment

    Python oneliner:

    def solve(arr, queries):
        return ['Odd' if x>y or arr[x-1]%2 or x<y and arr[x]==0 else 'Even' for x,y in queries]
    

    Some solutions published below omit the first condition (x>y) but it works only because there are no test cases with x>y and arr[x-1] even (you can make your own test case to see that it would fail in such event.

    Explanation: x>y is odd, odd^anything is odd, anything^0 is odd, everything else is even.

    • + 1 comment

      This would be a wonderful solution, and indeed, it works with the test cases. However, it gives the wrong prediction, when you have 0^0^0 (which is 0, and not 1).

      Try the following input: 5 // 0 0 0 0 0 // 5 // 1 1 // 1 2 // 1 3 // 1 4 // 1 5 Your output will be: Even, Odd, Odd, Odd, Odd. But it should be: Even, Odd, Even, Odd, Even.

      • + 0 comments

        The problem statement says that no consecutive two numbers in the arrays will ever be 0. So this case is ruled out.

  • + 1 comment

    Some things to notice: Since we only need to take note whether find(x,y) is odd or even, and since find(x,y) function is just repeated exponentiation with base arr[x]: 1. the result will be even if arr[x-1] (-1 because indices start from 1) is even and the next array element arr[x] is not zero (otherwise we end up getting 1). 2. the result will be odd if arr[x-1] is odd or if arr[x-1] is even and arr[x] is odd (as I explained in 1 above). Then the code is simple to be honest: In C++:

    vector<string> solve(vector<int> arr, vector<vector<int>> queries) {
        vector<string> result;
        
        for (auto k : queries){
            int x = k[0], y = k[1];
            if (arr[x-1]%2 != 0) result.push_back("Odd");
            else if (x < y && arr[x] == 0) result.push_back("Odd");
            else result.push_back("Even");
        }
        return result;
    }
    
    • + 1 comment

      It does not work with x>y. For example the following test cases returns Even although it should return Odd as find() returns 1:

      5
      0 0 0 0 0
      2
      5 1
      4 1
      
      • + 0 comments

        @adrien_cordonni1 The problem statement says x is less than or equal to y.