Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
- even := False
- return 'Even'
- 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 Input
[3,2,7], 1, 2
Output
Even
Advertisements
