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.
- If r is even then the final answer depends on the last digit i.e. arr[n – 1].
- 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++
#include <iostream>
using namespace std;
bool isEven( int arr[], int n, int r)
{
if (r % 2 == 0) {
if (arr[n - 1] % 2 == 0)
return true ;
}
else {
int oddCount = 0;
for ( int i = 0; i < n; ++i) {
if (arr[i] % 2 != 0)
oddCount++;
}
if (oddCount % 2 == 0)
return true ;
}
return false ;
}
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;
}
|
Java
import java.io.*;
class GFG
{
static boolean isEven( int arr[], int n, int r)
{
if (r % 2 == 0 )
{
if (arr[n - 1 ] % 2 == 0 )
return true ;
}
else {
int oddCount = 0 ;
for ( int i = 0 ; i < n; ++i) {
if (arr[i] % 2 != 0 )
oddCount++;
}
if (oddCount % 2 == 0 )
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def isEven(arr, n, r):
if (r % 2 = = 0 ):
if (arr[n - 1 ] % 2 = = 0 ):
return True
else :
oddCount = 0
for i in range (n):
if (arr[i] % 2 ! = 0 ):
oddCount + = 1
if (oddCount % 2 = = 0 ):
return True
return False
if __name__ = = '__main__' :
arr = [ 1 , 0 ]
n = len (arr)
r = 2
if (isEven(arr, n, r)):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class GFG
{
static bool isEven( int []arr, int n, int r)
{
if (r % 2 == 0)
{
if (arr[n - 1] % 2 == 0)
return true ;
}
else
{
int oddCount = 0;
for ( int i = 0; i < n; ++i)
{
if (arr[i] % 2 != 0)
oddCount++;
}
if (oddCount % 2 == 0)
return true ;
}
return false ;
}
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" );
}
}
|
PHP
<?php
function isEven( $arr , $n , $r )
{
if ( $r % 2 == 0)
{
if ( $arr [ $n - 1] % 2 == 0)
return true;
}
else
{
$oddCount = 0;
for ( $i = 0; $i < $n ; ++ $i )
{
if ( $arr [ $i ] % 2 != 0)
$oddCount ++;
}
if ( $oddCount % 2 == 0)
return true;
}
return false;
}
$arr = array ( 1, 0 );
$n = Count ( $arr );
$r = 2;
if (isEven( $arr , $n , $r ))
echo "Even" ;
else
echo "Odd" ;
?>
|
Javascript
<script>
function isEven(arr, n, r)
{
if (r % 2 == 0)
{
if (arr[n - 1] % 2 == 0)
return true ;
}
else
{
let oddCount = 0;
for (let i = 0; i < n; ++i)
{
if (arr[i] % 2 != 0)
oddCount++;
}
if (oddCount % 2 == 0)
return true ;
}
return false ;
}
let arr = [ 1, 0 ];
let n = arr.length;
let r = 2;
if (isEven(arr, n, r))
document.write( "Even" );
else
document.write( "Odd" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)