Program to find value of find(x, y) is even or odd in Python


Suppose we have an array nums. We also have another pair (x, y), we need to find whether the value find(x,y) is Odd or Even. The find() is as follows

  • find(x, y) = 1 if x > y
  • find(x, y) = nums[x]^find(x+1, y) otherwise

So, if the input is like nums = [3,2,7] (x, y) = 1, 2, then the output will be even, because −

  • find(1, 2) = nums[1]^find(2,3)
  • find(2, 2) = nums[2]^find(3,2)
  • find(3, 2) = 1,
  • so find(2, 2) = 7, and find(1, 2) = 2^7 = 128, this is even

To solve this, we will follow these steps −

  • even := True
  • if x > y or nums[x] is odd , then
    • even := False
  • if x < size of nums - 1 and x < y and nums[x+1] is same as 0, then
    • even := False
  • if even True, then
    • return 'Even'
  • otherwise,
    • return 'Odd'

Example

Let us see the following implementation to get better understanding −

def solve(nums, x, y):
   even = True
   if x > y or (nums[x] % 2 == 1):
      even = False
   if x < len(nums) - 1 and x < y and nums[x+1] == 0:
      even = False
   if even:
      return 'Even'
   else:
      return 'Odd'

nums = [3,2,7]
(x, y) = 1,2
print(solve(nums, x, y))

Input

[3,2,7], 1, 2

Output

Even

Updated on: 25-Oct-2021

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements