
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Java program to find whether given number is even or odd
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Python Program for Check if the count of divisors is even or odd
- C++ Program to Check Whether Number is Even or Odd
- How to find edit text values start from Number is Even or Odd?
- Check if count of divisors is even or odd in Python
- C Program to Check if count of divisors is even or odd?
- Java Program to check if count of divisors is even or odd
- Program to find longest even value path of a binary tree in Python
- Python program to find the sum of all even and odd digits of an integer list
- Java Program to Check Whether a Number is Even or Odd
- Find if sum of odd or even array elements are smaller in Java
- Program to find nearest point that has the same x or y coordinate using Python
- LCM of two prime number $x$ and $y( x>y)$ is $161$. Find value of $3y-x$.
- 8085 program to check whether the given number is even or odd

Advertisements