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

C++Server Side ProgrammingProgramming

Here we will see one problem, suppose one array is given. There are n elements. We have to find one index, where frequency of even numbers of its left and the frequency of even numbers of its right side are same, or the frequency of odd numbers of its left is same as the frequency of odd numbers of its right. If no such result is there, return -1.

Suppose the array is like {4, 3, 2, 1, 2, 4}. The output is 2. The element at index 2 is 2, there is only one odd number at left of it, and also there is only one odd number at the right of it.

To solve this problem, we will create two vectors of pairs to store left and right information. the vector for left will store frequency of odd and even numbers of its left side, and vector for right will do the same for the right side. If the even count for left and right or the odd count for left and right are same, then return the index.

Algorithm

getIndex(arr, n) −

Begin
   define odd and even, and initialize as 0
   define left_vector, right_vector for odd even pairs
   add (odd, even) into left_vector
   for i in range 0 to n-1, do
      if arr[i] is even, then increase even, otherwise increase odd
         add (odd, even) into left_vector
   done
   odd := 0 and even := 0
   add (odd, even) into right_vector
   for i in range n-1 down to 1, do
      if arr[i] is even, then increase even, otherwise increase odd
         add (odd, even) into right_vector
   done
   reverse the right_vector
   for each element at index i in left_vector, do
      if left_vector[i].first = right_vector[i].first, or left_vector[i].odd= right_vector[i].odd, then return i
   done
   return -1
End

Example

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int getIndex(int n, int arr[]) {
   int odd = 0, even = 0;
   vector<pair<int, int >> left_vector, right_vector;
   left_vector.push_back(make_pair(odd, even));
   for (int i = 0; i < n - 1; i++) { //count and store odd and even frequency for left side
      if (arr[i] % 2 == 0)
         even++;
      else
         odd++;
      left_vector.push_back(make_pair(odd, even));
   }
   odd = 0, even = 0;
   right_vector.push_back(make_pair(odd, even)); //count and store odd and even frequency for right side
   for (int i = n - 1; i > 0; i--) {
      if (arr[i] % 2 == 0)
         even++;
      else
         odd++;
      right_vector.push_back(make_pair(odd, even));
   }
   reverse(right_vector.begin(), right_vector.end());
   for (int i = 0; i < left_vector.size(); i++) {
      if (left_vector[i].first == right_vector[i].first ||
         left_vector[i].second == right_vector[i].second)
      return i;
   }
   return -1;
}
int main() {
   int arr[] = {4, 3, 2, 1, 2};
   int n = sizeof(arr) / sizeof(arr[0]);
   int index = getIndex(n, arr);
   if(index == -1) {
      cout << "-1";
   } else {
      cout << "index : " << index;
   }
}

Output

index : 2
raja
Published on 20-Aug-2019 07:43:06
Advertisements