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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to print the array // element which differs in parity // with the remaining array elements int OddOneOut( int arr[], int N)
{ // Multimaps to store even
// and odd numbers along
// with their indices
multimap< int , int > e, o;
// Traverse the array
for ( int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
e.insert({ arr[i], i });
}
// Otherwise
else {
o.insert({ arr[i], i });
}
}
// If only one even element
// is present in the array
if (e.size() == 1) {
cout << e.begin()->second;
}
// If only one odd element
// is present in the array
else {
cout << o.begin()->second;
}
} // Driver Code int main()
{ // Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = sizeof (arr) / sizeof (arr[0]);
OddOneOut(arr, N);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG {
// Function to print the array
// element which differs in parity
// with the remaining array elements
static void OddOneOut( int [] arr, int N)
{
// Multimaps to store even
// and odd numbers along
// with their indices
HashMap<Integer, Integer> e
= new LinkedHashMap<Integer, Integer>();
HashMap<Integer, Integer> o
= new LinkedHashMap<Integer, Integer>();
// Traverse the array
for ( int i = 0 ; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0 ) {
e.put(arr[i], i);
}
// Otherwise
else {
o.put(arr[i], i);
;
}
}
// If only one even element
// is present in the array
if (e.size() == 1 ) {
Map.Entry<Integer, Integer> entry
= e.entrySet().iterator().next();
System.out.print(entry.getValue());
}
// If only one odd element
// is present in the array
else {
Map.Entry<Integer, Integer> entry
= o.entrySet().iterator().next();
System.out.print(entry.getValue());
}
}
// Driver Code
public static void main(String[] args)
{
// Given array
int [] arr = { 2 , 4 , 7 , 8 , 10 };
// Size of the array
int N = arr.length;
OddOneOut(arr, N);
}
} // This code is contributed by phasing17. |
# Python3 program for the above approach # Function to print the array # element which differs in parity # with the remaining array elements def OddOneOut(arr, N) :
# Multimaps to store even
# and odd numbers along
# with their indices
e, o = {}, {}
# Traverse the array
for i in range (N) :
# If array element is even
if (arr[i] % 2 = = 0 ) :
e[arr[i]] = i
# Otherwise
else :
o[arr[i]] = i
# If only one even element
# is present in the array
if ( len (e) = = 1 ) :
print ( list (e.values())[ 0 ] )
# If only one odd element
# is present in the array
else :
print ( list (o.values())[ 0 ] )
# Given array arr = [ 2 , 4 , 7 , 8 , 10 ]
# Size of the array N = len (arr)
OddOneOut(arr, N) # This code is contributed by divyesh072019. |
// C# program for the above approach using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
// Function to print the array
// element which differs in parity
// with the remaining array elements
static void OddOneOut( int [] arr, int N)
{
// Multimaps to store even
// and odd numbers along
// with their indices
Dictionary< int , int > e = new Dictionary< int , int >();
Dictionary< int , int > o = new Dictionary< int , int >();
// Traverse the array
for ( int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
e[arr[i]] = i;
}
// Otherwise
else {
o[arr[i]] = i;
}
}
// If only one even element
// is present in the array
if (e.Count == 1) {
Console.Write(e.First().Value);
}
// If only one odd element
// is present in the array
else {
Console.Write(o.First().Value);
}
}
// Driver Code
public static void Main( string [] args)
{
// Given array
int [] arr = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.Length;
OddOneOut(arr, N);
}
} // This code is contributed by chitranayal. |
<script> // Javascript program for the above approach // Function to print the array // element which differs in parity // with the remaining array elements function OddOneOut(arr,N)
{ // Multimaps to store even
// and odd numbers along
// with their indices
let e = new Map();
let o = new Map();
// Traverse the array
for (let i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
e.set(arr[i] , i);
}
// Otherwise
else {
o.set(arr[i] , i);
}
}
// If only one even element
// is present in the array
if (e.size == 1) {
document.write(Array.from(e.values())[0]) ;
}
// If only one odd element
// is present in the array
else {
document.write(Array.from(o.values())[0]) ;
}
} // Driver Code // Given array let arr=[2, 4, 7, 8, 10]; // Size of the array let N = arr.length; OddOneOut(arr, N); // This code is contributed by avanitrachhadiya2155 </script> |
2
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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to print the element // which differs in parity int oddOneOut( int arr[], int N)
{ // Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for ( int i = 0; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1) {
cout << lastOdd << endl;
}
// If only one even element
// is present in the array
else {
cout << lastEven << endl;
}
} // Driver Code int main()
{ // Given array
int arr[] = { 2, 4, 7, 8, 10 };
// Size of the array
int N = sizeof (arr) / sizeof (arr[0]);
oddOneOut(arr, N);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG
{ // Function to print the element // which differs in parity static void oddOneOut( int arr[], int N)
{ // Stores the count of odd and
// even array elements encountered
int odd = 0 , even = 0 ;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0 , lastEven = 0 ;
// Traverse the array
for ( int i = 0 ; i < N; i++) {
// If array element is even
if (arr[i] % 2 == 0 ) {
even++;
lastEven = i;
}
// Otherwise
else {
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1 ) {
System.out.println(lastOdd);
}
// If only one even element
// is present in the array
else {
System.out.println(lastEven);
}
} // Driver Code public static void main(String args[])
{ // Given array
int arr[] = { 2 , 4 , 7 , 8 , 10 };
// Size of the array
int N = arr.length;
oddOneOut(arr, N);
} } // This code is contributed by jana_sayantan. |
# Python program for the above approach # Function to print the element # which differs in parity def oddOneOut(arr, N) :
# Stores the count of odd and
# even array elements encountered
odd = 0
even = 0
# Stores the indices of the last
# odd and even array elements encountered
lastOdd = 0
lastEven = 0
# Traverse the array
for i in range (N):
# If array element is even
if (arr[i] % 2 = = 0 ) :
even + = 1
lastEven = i
# Otherwise
else :
odd + = 1
lastOdd = i
# If only one odd element
# is present in the array
if (odd = = 1 ) :
print (lastOdd)
# If only one even element
# is present in the array
else :
print (lastEven)
# Driver Code # Given array arr = [ 2 , 4 , 7 , 8 , 10 ]
# Size of the array N = len (arr)
oddOneOut(arr, N) # This code is contributed by susmitakundugoaldanga. |
// C# program for the above approach using System;
class GFG
{ // Function to print the element
// which differs in parity
static void oddOneOut( int [] arr, int N)
{
// Stores the count of odd and
// even array elements encountered
int odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
int lastOdd = 0, lastEven = 0;
// Traverse the array
for ( int i = 0; i < N; i++)
{
// If array element is even
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
// Otherwise
else
{
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1)
{
Console.WriteLine(lastOdd);
}
// If only one even element
// is present in the array
else
{
Console.WriteLine(lastEven);
}
}
// Driver code
static void Main()
{
// Given array
int [] arr = { 2, 4, 7, 8, 10 };
// Size of the array
int N = arr.Length;
oddOneOut(arr, N);
}
} // This code is contributed by divyeshrabadiya07. |
<script> // Javascript program for the above approach // Function to print the element // which differs in parity function oddOneOut(arr, N)
{ // Stores the count of odd and
// even array elements encountered
let odd = 0, even = 0;
// Stores the indices of the last
// odd and even array elements encountered
let lastOdd = 0, lastEven = 0;
// Traverse the array
for (let i = 0; i < N; i++)
{
// If array element is even
if (arr[i] % 2 == 0)
{
even++;
lastEven = i;
}
// Otherwise
else
{
odd++;
lastOdd = i;
}
}
// If only one odd element
// is present in the array
if (odd == 1)
{
document.write(lastOdd);
}
// If only one even element
// is present in the array
else
{
document.write(lastEven);
}
} // Driver code // Given array let arr = [ 2, 4, 7, 8, 10 ]; // Size of the array let N = arr.length; oddOneOut(arr, N); // This code is contributed by suresh07 </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)