Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
GCDs of given index ranges in an Array
Next article icon

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

Last Updated : 19 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an 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 the rightmost bit have even values and the sum of even numbers is always even. Now the rightmost bit can have a value of either 1 or 0 as we know even + odd = odd so when the rightmost 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 

Implementation:

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; } 
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.  
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. 
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. 
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 ?> 
JavaScript
<script>      // Javascript 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)             document.write("odd") ;                 // if arr[r] = 0 print even         else             document.write("even") ;     }          let arr = [1, 1, 0, 1];     let n = arr.length;      checkEVENodd (arr, n, 1, 3);  </script> 

Output
odd

Time Complexity : O(1)
Space Complexity : O(1)


This article is contributed by Ayush Jha.  


Next Article
GCDs of given index ranges in an Array

A

Ayush Jha
Improve
Article Tags :
  • Misc
  • Bit Magic
  • DSA
  • Arrays
  • array-range-queries
Practice Tags :
  • Arrays
  • Bit Magic
  • Misc

Similar Reads

    PreComputation Technique on Arrays
    Precomputation refers to the process of pre-calculating and storing the results of certain computations or data structures(array in this case) in advance, in order to speed up the execution time of a program. This can be useful in situations where the same calculations are needed multiple times, as
    15 min read
    Queries for the product of first N factorials
    Given Q[] queries where each query consists of an integer N, the task is to find the product of first N factorials for each of the query. Since the result could be large, compute it modulo 109 + 7.Examples: Input: Q[] = {4, 5} Output: 288 34560 Query 1: 1! * 2! * 3! * 4! = 1 * 2 * 6 * 24 = 288 Query
    7 min read
    Range sum queries without updates
    Given an array arr of integers of size n. We need to compute the sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times.Examples: Input : arr[] = {1, 2, 3, 4, 5} i = 1, j = 3 i = 2, j = 4Output : 9 12 Input : arr[] = {1, 2, 3, 4, 5} i
    6 min read
    Range Queries for Frequencies of array elements
    Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
    13 min read
    Count Primes in Ranges
    Given a 2d array queries[][] of size n, where each query queries[i] contain 2 elements [l, r], your task is to find the count of number of primes in inclusive range [l, r]Examples: Input: queries[][] = [ [1, 10], [5, 10], [11, 20] ]Output: 4 2 4Explanation: For query 1, number of primes in range [1,
    12 min read
    Check in binary array the number represented by a subarray is odd or even
    Given an 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}
    4 min read
    GCDs of given index ranges in an Array
    Given an array arr[] of size N and Q queries of type {qs, qe} where qs and qe denote the starting and ending index of the query, the task is to find the GCD of all the numbers in the range. Examples: Input: arr[] = {2, 3, 60, 90, 50};Index Ranges: {1, 3}, {2, 4}, {0, 2}Output: GCDs of given ranges a
    14 min read
    Mean of range in array
    Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[
    12 min read
    Difference Array | Range update query in O(1)
    You are given an integer array arr[] and a list of queries. Each query is represented as a list of integers where:[1, l, r, x]: Adds x to all elements from arr[l] to arr[r] (inclusive).[2]: Prints the current state of the array.You need to perform the queries in order.Examples : Input: arr[] = [10,
    10 min read
    Range sum query using Sparse Table
    We have an array arr[]. We need to find the sum of all the elements in the range L and R where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Examples: Input : 3 7 2 5 8 9 query(0, 5) query(3, 5) query(2, 4) Output : 34 22 15Note : array is 0 based indexed and q
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences