Find index of the element differing in parity with all other array elements | C ++ program for the above approach ; Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE 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 ; }