Check if the number is even or odd whose digits and base (radix) is given

Given an array arr[] of size N which represents the digits of a number and an integer r which is the base (radix) of the given number i.e. n = arr[n – 1] * r0 + arr[n – 2] * r1 + … + a[0] * rN – 1. The task is to find whether the given number is odd or even.

Examples:

Input: arr[] = {1, 0}, r = 2
Output: Even
(10)2 = (2)10



Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, r = 10
Output: Odd

Naive Approach: The simplest approach is to calculate the number n by multiplying the digits with the corresponding power of base. But as the number of digits can be of the order of 105, this approach won’t work for a large n.

Efficient Approach: There are two cases possible.

  1. If r is even then the final answer depends on the last digit i.e. arr[n – 1].
  2. If r is odd then we have to count the number of odd digits. If the number of odd digits is even, then the sum is even. Else the sum is odd.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function that returns true if the number
// represented by arr[] is even in base r
bool isEven(int arr[], int n, int r)
{
  
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0) {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
  
    // If base is odd, then the
    // number of odd digits are checked
    else {
  
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i) {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
  
    // Number is odd
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int r = 2;
  
    if (isEven(arr, n, r))
        cout << "Even";
    else
        cout << "Odd";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
// Function that returns true if the number
// represented by arr[] is even in base r
static boolean isEven(int arr[], int n, int r)
{
  
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0)
    {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
  
    // If base is odd, then the
    // number of odd digits are checked
    else {
  
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i) {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
  
    // Number is odd
    return false;
}
  
// Driver code
public static void main (String[] args) 
{
      
    int arr[] = { 1, 0 };
    int n = arr.length;
    int r = 2;
  
    if (isEven(arr, n, r))
  
        System.out.println ("Even");
    else
      
        System.out.println("Odd");
}
}
  
// This code is contributed by jit_t.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function that returns true if the number
# represented by arr[] is even in base r
def isEven(arr, n, r):
      
    # If the base is even, then
    # the last digit is checked
    if (r % 2 == 0):
        if (arr[n - 1] % 2 == 0):
            return True
  
    # If base is odd, then the
    # number of odd digits are checked
    else:
        # To store the count of odd digits
        oddCount = 0
        for i in range(n):
            if (arr[i] % 2 != 0):
                oddCount += 1
        if (oddCount % 2 == 0):
            return True
  
    # Number is odd
    return False
  
# Driver code
if __name__ == '__main__':
    arr = [1, 0]
    n = len(arr)
    r = 2
  
    if (isEven(arr, n, r)):
        print("Even")
    else:
        print("Odd")
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function that returns true if the number
// represented by arr[] is even in base r
static bool isEven(int []arr, int n, int r)
{
  
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0)
    {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
  
    // If base is odd, then the
    // number of odd digits are checked
    else 
    {
  
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i)
        {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
  
    // Number is odd
    return false;
}
  
// Driver code
public static void Main () 
{
      
    int []arr = { 1, 0 };
    int n = arr.Length;
    int r = 2;
  
    if (isEven(arr, n, r))
  
        Console.WriteLine ("Even");
    else
      
        Console.WriteLine("Odd");
}
}
  
// This code is contributed by anuj_67...

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP implementation of the approach 
  
  
// Function that returns true if the number 
// represented by arr[] is even in base r 
function isEven($arr, $n, $r
  
    // If the base is even, then 
    // the last digit is checked 
    if ($r % 2 == 0) 
    
        if ($arr[$n - 1] % 2 == 0) 
            return true; 
    
  
    // If base is odd, then the 
    // number of odd digits are checked 
    else
    
  
        // To store the count of odd digits 
        $oddCount = 0; 
        for ($i = 0; $i < $n; ++$i
        
            if ($arr[$i] % 2 != 0) 
                $oddCount++; 
        
        if ($oddCount % 2 == 0) 
            return true; 
    
  
    // Number is odd 
    return false; 
  
    // Driver code 
    $arr = array( 1, 0 ); 
    $n = Count($arr); 
    $r = 2; 
  
    if (isEven($arr, $n, $r)) 
        echo "Even"
    else
        echo "Odd"
  
// This code is contributed by andrew1234
?>

chevron_right


Output:

Even


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.