Find index of the element differing in parity with all other array elements

• Last Updated : 22 Nov, 2021

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). 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:

1. Initialize two Multimap, for even and odd numbers.
2. Traverse the array and store array elements in their respective Multimaps along with their indices.
3. 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++

 // C++ program for the above approach#include using namespace std; // Function to print the array// element which differs in parity// with the remaining array elementsint OddOneOut(int arr[], int N){     // Multimaps to store even    // and odd numbers along    // with their indices    multimap 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 Codeint main(){    // Given array    int arr[] = { 2, 4, 7, 8, 10 };     // Size of the array    int N = sizeof(arr) / sizeof(arr);     OddOneOut(arr, N);     return 0;}

Python3

 # Python3 program for the above approach # Function to print the array# element which differs in parity# with the remaining array elementsdef 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()) )     # If only one odd element    # is present in the array    else :        print(list(o.values()) ) # Given arrayarr = [ 2, 4, 7, 8, 10 ] # Size of the arrayN = len(arr) OddOneOut(arr, N) # This code is contributed by divyesh072019.

C#

 // C# program for the above approachusing 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 e = new Dictionary();    Dictionary o = new Dictionary();     // 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.

Javascript


Output:
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:

1. 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.
2. After traversing the array, if odd is found to be 1, then print lastOdd.
3. Otherwise, print lastEven.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to print the element// which differs in parityint 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 Codeint main(){    // Given array    int arr[] = { 2, 4, 7, 8, 10 };     // Size of the array    int N = sizeof(arr) / sizeof(arr);     oddOneOut(arr, N);     return 0;}

Java

 // Java program for the above approachimport java.util.*;class GFG{  // Function to print the element// which differs in paritystatic 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 Codepublic 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.

Python3

 # Python program for the above approach # Function to print the element# which differs in paritydef 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 arrayarr = [ 2, 4, 7, 8, 10 ] # Size of the arrayN = len(arr) oddOneOut(arr, N) # This code is contributed by susmitakundugoaldanga.

C#

 // C# program for the above approachusing 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.

Javascript


Output:
2

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up