Array Index with same count of even or odd numbers on both sides

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.

CPP

// 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.
?>


Output:

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

// 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;
}
Output:

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.





Check out this Author's contributed articles.

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 2 vote(s)