Given an array arr[] of size N (N > 3), the task is to find the position of the element that differs in parity (odd/even) with respect to all other array elements.
Note: It is guaranteed that there will always be a number that differs in parity from all other elements.
Examples:
Input: arr[] = {2, 4, 7, 8, 10}
Output: 2
Explanation: The only odd element in the array is 7 (= arr[2]). Therefore, required output is 2.
Input: arr[] = {2, 1, 1}
Output: 0
Naive Approach: The simplest approach to solve this problem is to store all even and odd numbers with their indices in a Multimap and print the index of the element present in the Map having size 1. Follow the steps below to solve the problem:
- Initialize two Multimap, for even and odd numbers.
- Traverse the array and store array elements in their respective Multimaps along with their indices.
- Now find the Multimap with size equal to 1. Print the index of the array element stored in that Multimap.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int OddOneOut( int arr[], int N)
{
multimap< int , int > e, o;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
e.insert({ arr[i], i });
}
else {
o.insert({ arr[i], i });
}
}
if (e.size() == 1) {
cout << e.begin()->second;
}
else {
cout << o.begin()->second;
}
}
int main()
{
int arr[] = { 2, 4, 7, 8, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
OddOneOut(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void OddOneOut( int [] arr, int N)
{
HashMap<Integer, Integer> e
= new LinkedHashMap<Integer, Integer>();
HashMap<Integer, Integer> o
= new LinkedHashMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 0 ) {
e.put(arr[i], i);
}
else {
o.put(arr[i], i);
;
}
}
if (e.size() == 1 ) {
Map.Entry<Integer, Integer> entry
= e.entrySet().iterator().next();
System.out.print(entry.getValue());
}
else {
Map.Entry<Integer, Integer> entry
= o.entrySet().iterator().next();
System.out.print(entry.getValue());
}
}
public static void main(String[] args)
{
int [] arr = { 2 , 4 , 7 , 8 , 10 };
int N = arr.length;
OddOneOut(arr, N);
}
}
|
Python3
def OddOneOut(arr, N) :
e, o = {}, {}
for i in range (N) :
if (arr[i] % 2 = = 0 ) :
e[arr[i]] = i
else :
o[arr[i]] = i
if ( len (e) = = 1 ) :
print ( list (e.values())[ 0 ] )
else :
print ( list (o.values())[ 0 ] )
arr = [ 2 , 4 , 7 , 8 , 10 ]
N = len (arr)
OddOneOut(arr, N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static void OddOneOut( int [] arr, int N)
{
Dictionary< int , int > e = new Dictionary< int , int >();
Dictionary< int , int > o = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
e[arr[i]] = i;
}
else {
o[arr[i]] = i;
}
}
if (e.Count == 1) {
Console.Write(e.First().Value);
}
else {
Console.Write(o.First().Value);
}
}
public static void Main( string [] args)
{
int [] arr = { 2, 4, 7, 8, 10 };
int N = arr.Length;
OddOneOut(arr, N);
}
}
|
Javascript
<script>
function OddOneOut(arr,N)
{
let e = new Map();
let o = new Map();
for (let i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
e.set(arr[i] , i);
}
else {
o.set(arr[i] , i);
}
}
if (e.size == 1) {
document.write(Array.from(e.values())[0]) ;
}
else {
document.write(Array.from(o.values())[0]) ;
}
}
let arr=[2, 4, 7, 8, 10];
let N = arr.length;
OddOneOut(arr, N);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to keep a count of even and odd array elements in two variables and check which count is equal to 1. Follow the steps below to solve the problem:
- Maintain four variables even, odd, to keep count of even and odd array elements in the array, and lastOdd, lastEven to store the indices of the last odd and even array elements encountered.
- After traversing the array, if odd is found to be 1, then print lastOdd.
- Otherwise, print lastEven.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int oddOneOut( int arr[], int N)
{
int odd = 0, even = 0;
int lastOdd = 0, lastEven = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
else {
odd++;
lastOdd = i;
}
}
if (odd == 1) {
cout << lastOdd << endl;
}
else {
cout << lastEven << endl;
}
}
int main()
{
int arr[] = { 2, 4, 7, 8, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
oddOneOut(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void oddOneOut( int arr[], int N)
{
int odd = 0 , even = 0 ;
int lastOdd = 0 , lastEven = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 0 ) {
even++;
lastEven = i;
}
else {
odd++;
lastOdd = i;
}
}
if (odd == 1 ) {
System.out.println(lastOdd);
}
else {
System.out.println(lastEven);
}
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 7 , 8 , 10 };
int N = arr.length;
oddOneOut(arr, N);
}
}
|
Python3
def oddOneOut(arr, N) :
odd = 0
even = 0
lastOdd = 0
lastEven = 0
for i in range (N):
if (arr[i] % 2 = = 0 ) :
even + = 1
lastEven = i
else :
odd + = 1
lastOdd = i
if (odd = = 1 ) :
print (lastOdd)
else :
print (lastEven)
arr = [ 2 , 4 , 7 , 8 , 10 ]
N = len (arr)
oddOneOut(arr, N)
|
C#
using System;
class GFG
{
static void oddOneOut( int [] arr, int N)
{
int odd = 0, even = 0;
int lastOdd = 0, lastEven = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
else
{
odd++;
lastOdd = i;
}
}
if (odd == 1)
{
Console.WriteLine(lastOdd);
}
else
{
Console.WriteLine(lastEven);
}
}
static void Main()
{
int [] arr = { 2, 4, 7, 8, 10 };
int N = arr.Length;
oddOneOut(arr, N);
}
}
|
Javascript
<script>
function oddOneOut(arr, N)
{
let odd = 0, even = 0;
let lastOdd = 0, lastEven = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
else
{
odd++;
lastOdd = i;
}
}
if (odd == 1)
{
document.write(lastOdd);
}
else
{
document.write(lastEven);
}
}
let arr = [ 2, 4, 7, 8, 10 ];
let N = arr.length;
oddOneOut(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)