# Check in binary array the number represented by a subarray is odd or even

Given a array such that all its terms is either 0 or 1.You need to tell the number represented by a subarray a[l..r] is odd or even

**Examples :**

Input : arr = {1, 1, 0, 1} l = 1, r = 3 Output : odd number represented by arr[l...r] is 101 which 5 in decimal form which is odd Input : arr = {1, 1, 1, 1} l = 0, r = 3 Output : odd

The important point to note here is all the odd numbers in binary form have 1 as their rightmost bit and all even numbers have 0 as their rightmost bit.

The reason is simple all other bits other than rightmost bit have even values and sum of even numbers is always even .Now the rightmost bit can have value either 1 or 0 as we know even + odd = odd so when right most bit is 1 the number is odd and when it is 0 the number is even.

So to solve this problem we have to just check if a[r] is 0 or 1 and accordingly print odd or even

## C++

`// C++ program to find if a subarray ` `// is even or odd. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// prints if subarray is even or odd ` `void` `checkEVENodd (` `int` `arr[], ` `int` `n, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// if arr[r] = 1 print odd ` ` ` `if` `(arr[r] == 1) ` ` ` `cout << ` `"odd"` `<< endl; ` ` ` ` ` `// if arr[r] = 0 print even ` ` ` `else` ` ` `cout << ` `"even"` `<< endl; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = {1, 1, 0, 1}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `checkEVENodd (arr, n, 1, 3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find if a subarray ` `// is even or odd. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// prints if subarray is even or odd ` ` ` `static` `void` `checkEVENodd (` `int` `arr[], ` `int` `n, ` `int` `l, ` `int` `r) ` ` ` `{ ` ` ` `// if arr[r] = 1 print odd ` ` ` `if` `(arr[r] == ` `1` `) ` ` ` `System.out.println( ` `"odd"` `) ; ` ` ` ` ` `// if arr[r] = 0 print even ` ` ` `else` ` ` `System.out.println ( ` `"even"` `) ; ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = {` `1` `, ` `1` `, ` `0` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` `checkEVENodd (arr, n, ` `1` `, ` `3` `); ` ` ` ` ` ` ` `} ` `} ` ` ` `// This article is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find if a ` `# subarray is even or odd. ` ` ` `# Prints if subarray is even or odd ` `def` `checkEVENodd (arr, n, l, r): ` ` ` ` ` `# if arr[r] = 1 print odd ` ` ` `if` `(arr[r] ` `=` `=` `1` `): ` ` ` `print` `(` `"odd"` `) ` ` ` ` ` `# if arr[r] = 0 print even ` ` ` `else` `: ` ` ` `print` `(` `"even"` `) ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `1` `, ` `0` `, ` `1` `] ` `n ` `=` `len` `(arr) ` `checkEVENodd (arr, n, ` `1` `, ` `3` `) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if a subarray ` `// is even or odd. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// prints if subarray is even or odd ` ` ` `static` `void` `checkEVENodd (` `int` `[]arr, ` ` ` `int` `n, ` `int` `l, ` `int` `r) ` ` ` `{ ` ` ` ` ` `// if arr[r] = 1 print odd ` ` ` `if` `(arr[r] == 1) ` ` ` `Console.WriteLine( ` `"odd"` `) ; ` ` ` ` ` `// if arr[r] = 0 print even ` ` ` `else` ` ` `Console.WriteLine( ` `"even"` `) ; ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `[]arr = {1, 1, 0, 1}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `checkEVENodd (arr, n, 1, 3); ` ` ` `} ` `} ` ` ` `// This article is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if a subarray ` `// is even or odd. ` ` ` `// prints if subarray is even or odd ` `function` `checkEVENodd (` `$arr` `, ` `$n` `, ` `$l` `, ` `$r` `) ` `{ ` ` ` `// if arr[r] = 1 print odd ` ` ` `if` `(` `$arr` `[` `$r` `] == 1) ` ` ` `echo` `"odd"` `, ` `"\n"` `; ` ` ` ` ` `// if arr[r] = 0 print even ` ` ` `else` ` ` `echo` `"even"` `, ` `"\n"` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(1, 1, 0, 1); ` `$n` `= sizeof(` `$arr` `); ` `checkEVENodd (` `$arr` `, ` `$n` `, 1, 3); ` ` ` `// This code is Contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

odd

This article is contributed by **Ayush Jha**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check whether a large number represented as array is divisible by Y
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Highest power of 2 that divides a number represented in binary
- Find the missing element in an array of integers represented in binary format
- Add 1 to number represented as array | Recursive Approach
- Sum of two numbers where one number is represented as array of digits
- Minimum length subarray of 1s in a Binary Array
- Check if a subarray exists with sum greater than the given Array
- Check if subarray with given product exists in an array
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Check whether an Array is Subarray of another Array
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Number of triplets in array having subarray xor equal
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check if all elements of binary array can be made 1
- How to check if a given array represents a Binary Heap?
- Check if an array is sorted and rotated using Binary Search
- Check if binary representation of a number is palindrome
- Check whether the binary equivalent of a number ends with "001" or not
- Check if actual binary representation of a number is palindrome