Given an array of N integers. We need to find an index such that Frequency of Even numbers on its left side is equal to the frequency of even numbers on its right sides Or frequency of odd numbers on its left side is equal to the frequency of Odd numbers on its right sides. If No such index exist in an array print -1 Else print required index. Note-(If more than one index exist then return index that comes first)
Examples:
Input : arr[] = {4, 3, 2, 1, 2, 4}
Output : index = 2
Explanation: At index 2, there is one
odd number on its left and one odd on
its right.
Input :arr[] = { 1, 2, 4, 5, 8, 3, 12}
Output : index = 3
Method 1 : (Simple Approach)
Run two loops. For every element, count evens and odds on its left and right sides.
C++
// CPP program to find an index which has // same number of even elements on left and // right, Or same number of odd elements on // left and right. #include <iostream> using namespace std; // Function to find index int findIndex(int arr[], int n) { for (int i = 0; i < n; i++) { int odd_left = 0, even_left = 0; int odd_right = 0, even_right = 0; // To count Even and Odd numbers of left side for (int j = 0; j < i; j++) { if (arr[j] % 2 == 0) even_left++; else odd_left++; } // To count Even and Odd numbers of right side for (int k = n - 1; k > i; k--) { if (arr[k] % 2 == 0) even_right++; else odd_right++; } // To check Even Or Odd of Both sides are equal or not if (even_right == even_left || odd_right == odd_left) return i; } return -1; } // Driver's Function int main() { int arr[] = {4, 3, 2, 1, 2}; int n = sizeof(arr) / sizeof(arr[0]); int index = findIndex(arr, n); ((index == -1) ? cout << "-1" : cout << "index = " << index); return 0; } |
Java
// Java program to find // an index which has // same number of even // elements on left and // right, Or same number // of odd elements on // left and right. class GFG{ // Function to find index static int findIndex(int arr[], int n) { for (int i = 0; i < n; i++) { int odd_left = 0, even_left = 0; int odd_right = 0, even_right = 0; // To count Even and Odd // numbers of left side for (int j = 0; j < i; j++) { if (arr[j] % 2 == 0) even_left++; else odd_left++; } // To count Even and Odd // numbers of right side for (int k = n - 1; k > i; k--) { if (arr[k] % 2 == 0) even_right++; else odd_right++; } // To check Even Or Odd of Both // sides are equal or not if (even_right == even_left || odd_right == odd_left) return i; } return -1; } // Driver's Function public static void main(String[] args) { int arr[] = {4, 3, 2, 1, 2}; int n = arr.length; int index = findIndex(arr, n); if (index == -1) System.out.println("-1"); else{ System.out.print("index = "); System.out.print(index); } } } // This code is contributed by // Smitha Dinesh Semwal |
Python3
'''Python program to find an index which has same number of even elements on left and right, Or same number of odd elements on left and right.''' # Function to find index def findIndex(arr,n): for i in range(n): odd_left = 0 even_left = 0 odd_right = 0 even_right = 0 # To count Even and Odd # numbers of left side for j in range(i): if (arr[j] % 2 == 0): even_left=even_left+1 else: odd_left=odd_left+1 # To count Even and Odd # numbers of right side for k in range(n - 1, i, -1): if (arr[k] % 2 == 0): even_right=even_right+1 else: odd_right=odd_right+1 # To check Even Or Odd of Both # sides are equal or not if (even_right == even_left and odd_right == odd_left): return i return -1 # Driver's Function arr = [4, 3, 2, 1, 2] n = len(arr) index = findIndex(arr, n) if (index == -1): print("-1") else: print("index = ", index) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find an index which has // same number of even elements on left // and right, Or same number of odd // elements on left and right. using System; class GFG{ // Function to find index static int findIndex(int []arr, int n) { for (int i = 0; i < n; i++) { int odd_left = 0, even_left = 0; int odd_right = 0, even_right = 0; // To count Even and Odd // numbers of left side for (int j = 0; j < i; j++) { if (arr[j] % 2 == 0) even_left++; else odd_left++; } // To count Even and Odd // numbers of right side for (int k = n - 1; k > i; k--) { if (arr[k] % 2 == 0) even_right++; else odd_right++; } // To check Even Or Odd of Both // sides are equal or not if (even_right == even_left || odd_right == odd_left) return i; } return -1; } // Driver's Function public static void Main() { int []arr = {4, 3, 2, 1, 2}; int n = arr.Length; int index = findIndex(arr, n); if (index == -1) Console.Write("-1"); else{ Console.Write("index = "); Console.Write(index); } } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find an index which has // same number of even elements on left and // right, Or same number of odd elements on // left and right. // Function to find index function findIndex($arr, $n) { for ($i = 0; $i < $n; $i++) { $odd_left = 0; $even_left = 0; $odd_right = 0; $even_right = 0; // To count Even and Odd // numbers of left side for ($j = 0; $j < $i; $j++) { if ($arr[$j] % 2 == 0) $even_left++; else $odd_left++; } // To count Even and Odd // numbers of right side for ($k = $n - 1; $k > $i; $k--) { if ($arr[$k] % 2 == 0) $even_right++; else $odd_right++; } // To check Even Or Odd of // Both sides are equal or not if ($even_right == $even_left || $odd_right == $odd_left) return $i; } return -1; } // Drivers Code { $arr = array(4, 3, 2, 1, 2); $n = sizeof($arr) / sizeof($arr[0]); $index = findIndex($arr, $n); if($index == -1) echo "-1"; else echo ("index = $index" ); return 0; } // This code is contributed by nitin mittal. ?> |
index = 2
Time Complexity : O(n*n)
Auxiliary Space : O(1)
Method 2:(Efficient solution)
1- Create two vectors of pair types i.e v_left and v_right
2- v_left[i] stores the frequency of odd and even numbers of its left sides
3- v_right[i] stores the frequency of odd and even numbers of its right sides
4- Now check (v_left[i].first == v_right[i].first || v_left[i].second == v_right[i].second)
if True return i
5- At last if no such index exist return -1
C++
// CPP program to find an index which has // same number of even elements on left and // right, Or same number of odd elements on // left and right. #include <bits/stdc++.h> #include <iostream> using namespace std; // Function to Find index int Find_Index(int n, int arr[]) { int odd = 0, even = 0; // Create two vectors of pair type vector<pair<int, int>> v_left, v_right; v_left.push_back(make_pair(odd, even)); for (int i = 0; i < n - 1; i++) { if (arr[i] % 2 == 0) even++; else odd++; v_left.push_back(make_pair(odd, even)); } odd = 0, even = 0; v_right.push_back(make_pair(odd, even)); for (int i = n - 1; i > 0; i--) { if (arr[i] % 2 == 0) even++; else odd++; v_right.push_back(make_pair(odd, even)); } reverse(v_right.begin(), v_right.end()); for (int i = 0; i < v_left.size(); i++) { // To check even or odd of Both sides are // equal or not if (v_left[i].first == v_right[i].first || v_left[i].second == v_right[i].second) return i; } return -1; } // Driver's Function int main() { int arr[] = {4, 3, 2, 1, 2}; int n = sizeof(arr) / sizeof(arr[0]); int index = Find_Index(n, arr); ((index == -1) ? cout << "-1" : cout << "index = " << index); return 0; } |
Java
// Java program to find an index which has // same number of even elements on left and // right, Or same number of odd elements on // left and right. import java.util.*; class GFG { public static class pair { int first, second; pair(int f, int s) { first = f; second = s; } }; // Function to Find index static int Find_Index(int n, int arr[]) { int odd = 0, even = 0; // Create two vectors of pair type Vector<pair> v_left = new Vector<>();; Vector<pair> v_right = new Vector<>(); v_left.add(new pair(odd, even)); for (int i = 0; i < n - 1; i++) { if (arr[i] % 2 == 0) { even++; } else { odd++; } v_left.add(new pair(odd, even)); } odd = 0; even = 0; v_right.add(new pair(odd, even)); for (int i = n - 1; i > 0; i--) { if (arr[i] % 2 == 0) { even++; } else { odd++; } v_right.add(new pair(odd, even)); } Collections.reverse(v_right); for (int i = 0; i < v_left.size(); i++) { // To check even or odd of Both sides are // equal or not if (v_left.get(i).first == v_right.get(i).first || v_left.get(i).second == v_right.get(i).second) { return i; } } return -1; } // Driver code public static void main(String[] args) { int arr[] = {4, 3, 2, 1, 2}; int n = arr.length; int index = Find_Index(n, arr); if (index == -1) { System.out.println("-1"); } else { System.out.println("index = " + index); } } } /* This code contributed by PrinciRaj1992 */ |
C#
// C# program to find an index which has // same number of even elements on left and // right, Or same number of odd elements on // left and right. using System; using System.Collections.Generic; class GFG { public class pair { public int first, second; public pair(int f, int s) { first = f; second = s; } }; // Function to Find index static int Find_Index(int n, int []arr) { int odd = 0, even = 0; // Create two vectors of pair type List<pair> v_left = new List<pair>();; List<pair> v_right = new List<pair>(); v_left.Add(new pair(odd, even)); for (int i = 0; i < n - 1; i++) { if (arr[i] % 2 == 0) { even++; } else { odd++; } v_left.Add(new pair(odd, even)); } odd = 0; even = 0; v_right.Add(new pair(odd, even)); for (int i = n - 1; i > 0; i--) { if (arr[i] % 2 == 0) { even++; } else { odd++; } v_right.Add(new pair(odd, even)); } v_right.Reverse(); for (int i = 0; i < v_left.Count; i++) { // To check even or odd of Both sides are // equal or not if (v_left[i].first == v_right[i].first || v_left[i].second == v_right[i].second) { return i; } } return -1; } // Driver code public static void Main(String[] args) { int []arr = {4, 3, 2, 1, 2}; int n = arr.Length; int index = Find_Index(n, arr); if (index == -1) { Console.WriteLine("-1"); } else { Console.WriteLine("index = " + index); } } } // This code is contributed by 29AjayKumar |
index = 2
Time Complexity : O(n)
Auxiliary Space : O(n)
Further optimization : We can optimize the space used in the program. Instead of making vectors of pairs, we can make vectors of integers. We can use the fact that number of odd elements is equal total elements minus total number of even elements. Similarly number of even elements is equal total elements minus total number of odd elements.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of pairs having each element equal to index of the other from an Array
- Count of index pairs with equal elements in an array
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Count of index pairs in array whose range product is a positive integer
- Count of contiguous subarrays possible for every index by including the element at that index
- Queries for number of distinct elements from a given index till last index in an array
- Maximum area rectangle by picking four sides from array
- Count of Missing Numbers in a sorted array
- Count numbers in a range that are divisible by all array elements
- Count the number of digits of palindrome numbers in an array
- Minimum count of numbers required from given array to represent S
- C program to count Positive and Negative numbers in an Array
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count of numbers in Array ending with digits of number N
- Array Range Queries to count Powerful numbers with updates
- Maximum count of equal numbers in an array after performing given operations
- Array range queries to count the number of Fibonacci numbers with updates
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Even numbers at even index and odd numbers at odd index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

