An evil number is a non-negative number that has an even number of 1s in its binary expansion. (Binary Expansion – is representation of a number in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one)).
Odious Numbers: Numbers that are not Evil are called Odious Numbers.
Given a number, the task is to check if it is Evil Number or Odious Numbers.
Examples :
Input : 3
Output : Evil Number
Explanation: Binary expansion of 3 is 11,
the number of 1s in this is 2 i.e even.
Input : 16
Output : Odious Number(not an evil number)
Explanation: Binary expansion of 16 = 10000,
having number of 1s =1 i.e odd.
Input : 23
Output : Evil Number
Explanation: Binary expansion of 23 is 10111,
the number of 1s in this is 4 i.e even.
C++
#include <iostream>
using namespace std;
#include <math.h>
int count_one( int n)
{
int c_one = 0;
while (n != 0) {
int rem = n % 10;
if (rem == 1)
c_one = c_one + 1;
n = n / 10;
}
return c_one;
}
int checkEvil( int n)
{
int i = 0, bin = 0, n_one = 0;
while (n != 0) {
int r = n % 2;
bin = bin + r * ( int )( pow (10, i));
n = n / 2;
}
n_one = count_one(bin);
if (n_one % 2 == 0)
return 1;
else
return 0;
}
int main( void )
{
int i, check, num;
num = 32;
check = checkEvil(num);
if (check == 1)
cout << num << " is Evil Number\n" ;
else
cout << num << " is Odious Number\n" ;
return 0;
}
|
Java
class GFG {
static int count_one( int n)
{
int c_one = 0 ;
while (n != 0 ) {
int rem = n % 10 ;
if (rem == 1 )
c_one = c_one + 1 ;
n = n / 10 ;
}
return c_one;
}
static int checkEvil( int n)
{
int i = 0 , bin = 0 , n_one = 0 ;
while (n != 0 ) {
int r = n % 2 ;
bin = bin + r * ( int )(Math.pow( 10 , i));
n = n / 2 ;
}
n_one = count_one(bin);
if (n_one % 2 == 0 )
return 1 ;
else
return 0 ;
}
public static void main(String[] args)
{
int i, check, num;
num = 32 ;
check = checkEvil(num);
if (check == 1 )
System.out.println(num + " is Evil Number" );
else
System.out.println(num + " is Odious Number" );
}
}
|
Python3
def count_one(n):
c_one = 0
while n ! = 0 :
rem = n % 10
if rem = = 1 :
c_one = c_one + 1
n = n / / 10
return c_one
def checkEvil(n):
i = 0
binary = 0
while n ! = 0 :
r = n % 2
binary = binary + r * ( int ( 10 * * i))
n = n / / 2
n_one = count_one(binary)
if n_one % 2 = = 0 :
return True
return False
num = 32
check = checkEvil(num)
if check:
print (num, "is Evil Number" )
else :
print (num, "is Odious Number" )
|
C#
using System;
class GFG {
static int count_one( int n)
{
int c_one = 0;
while (n != 0) {
int rem = n % 10;
if (rem == 1)
c_one = c_one + 1;
n = n / 10;
}
return c_one;
}
static int checkEvil( int n)
{
int i = 0, bin = 0, n_one = 0;
while (n != 0) {
int r = n % 2;
bin = bin + r * ( int )(Math.Pow(10, i));
n = n / 2;
}
n_one = count_one(bin);
if (n_one % 2 == 0)
return 1;
else
return 0;
}
public static void Main(String[] args)
{
int check, num;
num = 32;
check = checkEvil(num);
if (check == 1)
Console.WriteLine(num + " is Evil Number" );
else
Console.WriteLine(num + " is Odious Number" );
}
}
|
PHP
<?php
function count_one( $n )
{
$c_one = 0;
while ( $n != 0)
{
$rem = $n % 10;
if ( $rem == 1)
$c_one = $c_one + 1;
$n = $n / 10;
}
return $c_one ;
}
function checkEvil( $n )
{
$i = 0; $bin = 0; $n_one = 0;
while ( $n != 0)
{
$r = $n % 2;
$bin = $bin + $r * (pow(10, $i ));
$n = $n / 2;
}
$n_one = count_one( $bin );
if ( $n_one % 2 == 0)
return 1;
else
return 0;
}
$i ; $check ; $num ;
$num = 32;
$check = checkEvil( $num );
if ( $check == 1)
echo $num , " is Evil Number\n" ;
else
echo $num , " is Odious Number\n" ;
?>
|
Javascript
<script>
function count_one(n)
{
let c_one = 0;
while (n != 0)
{
let rem = n % 10;
if (rem == 1)
c_one = c_one + 1;
n = parseInt(n / 10, 10);
}
return c_one;
}
function checkEvil(n)
{
let i = 0, bin = 0, n_one = 0;
while (n != 0)
{
let r = n % 2;
bin = bin + r * (Math.pow(10, i));
n = parseInt(n / 2, 10);
}
n_one = count_one(bin);
if (n_one % 2 == 0)
return 1;
else
return 0;
}
let check, num;
num = 32;
check = checkEvil(num);
if (check == 1)
document.write(num + " is Evil Number" );
else
document.write(num + " is Odious Number" );
</script>
|
Output
32 is Odious Number
Time Complexity: O(log2n)
Auxiliary Space: O(1), As constant extra space is used.
With the use of __builtin_parity(x): This function is used to check the parity of a number. This function returns true(1) if the number has odd parity else it returns false(0) for even parity.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 5;
if (__builtin_parity(num))
cout << num << " is Odious Number\n" ;
else
cout << num << " is Evil Number\n" ;
return 0;
}
|
Java
import java.lang.*;
public class Main {
public static void main(String[] args) {
int num = 5 ;
if (Integer.bitCount(num) % 2 == 1 )
System.out.println(num + " is Odious Number" );
else
System.out.println(num + " is Evil Number" );
}
}
|
Python3
num = 5
if bin (num).count( '1' ) % 2 = = 1 :
print (num, "is Odious Number" )
else :
print (num, "is Evil Number" )
|
C#
using System;
class Program {
static void Main( string [] args)
{
int num = 5;
if (Parity(num))
Console.WriteLine(num + " is Odious Number" );
else
Console.WriteLine(num + " is Evil Number" );
}
static bool Parity( int num)
{
int count = 0;
while (num > 0) {
count++;
num &= num - 1;
}
return (count % 2 == 1);
}
}
|
Javascript
function isOdiousNumber(num) {
return (num.toString(2).split( '1' ).length - 1) % 2 !== 0;
}
let num = 5;
if (isOdiousNumber(num)) {
console.log(num + " is Odious Number" );
} else {
console.log(num + " is Evil Number" );
}
|
Time Complexity: O(log2n)
Auxiliary Space: O(1)
This article is contributed by Nikita Tiwari. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!