查找具有相同的偶数或奇数计数的数组索引是一个在其两边具有相等数量的数字或奇数的数字,即,左侧的编号=右侧的编号。
在这里,我们需要一些与概念相关的定义,
数组-相同数据类型的元素的容器。
数组索引-元素的位置称为其索引。数组的索引始终从0开始。
偶数-可被2整除的数字。
奇数-一个不能被2整除的数字。
整数可以是偶数或奇数。
现在,让我们看一个使概念更清晰的示例。
Input: arr[] = {4, 3, 2, 1, 2}
Output : 2在索引2处,左数为奇数,右数为奇数。
我们有一个由n个整数组成的数组,该数组可以按以下方式找到数组元素的索引:它的左侧有偶数个偶数个元素,右侧有一个偶数个元素,否则我们必须找到奇数个频率如果没有这样的条件,则其左侧元素的数量等于其右侧元素的奇数的频率,因此,如果存在这种条件,我们必须由谁来写ok print -1,我们必须打印其索引
要计算两边均具有偶数或奇数的元素的索引,我们需要在给定元素的左侧和右侧找到元素的数量。
给定数组arr [],n个数组元素。
Step 1 : For i -> 0 to n, follow step 2 - 5: Step 2: initialise e_l, o_l, e_r, o_r to 0. Step 3: for j -> o to i Step 3.1 : count values for e_l and o_l. Step 4: for j -> i+1 to n Step 4.1 : count values for e_r and o_r. Step 5: if(e_l == e_r) or (o_l == e_r ) , print i.
#include <iostream>
using namespace std;
int main() {
   int arr[] = {4, 3, 2, 1, 2};
   int n = 5;
   cout<<"The array is : ";
   for(int i = 0; i < n; i++) {
      cout<<arr[i]<<" ";
   }
   cout<<"\nThe index of the element with the same count of even or odd numbers on both sides = ";
   for (int i = 0; i < n; i++) {
      int o_l = 0, e_l = 0;
      int o_r = 0, e_r = 0;
   for (int j = 0; j < i; j++) {
      if (arr[j] % 2 == 0)
         e_l++;
      else
         o_l++;
   }
   for (int k = n - 1; k > i; k--) {
      if (arr[k] % 2 == 0)
         e_r++;
      else
         o_r++;
   }
   if (e_r == e_l || o_r == o_l)
      cout<<i<<endl;
   }
   return 0;
}输出结果
The array is : 4 3 2 1 2 The index of the element with the same count of even or odd numbers on both sides = 2