📌  相关文章
📜  两边偶数或奇数计数相同的数组索引

📅  最后修改于: 2022-05-13 01:57:47.635000             🧑  作者: Mango

两边偶数或奇数计数相同的数组索引

给定一个包含 N 个整数的数组。我们需要找到一个索引,使得其左侧偶数的频率等于其右侧偶数的频率,或者其左侧奇数的频率等于其右侧奇数的频率。如果数组中不存在这样的索引,则打印 -1 否则打印所需的索引。注意-(如果存在多个索引,则返回第一个索引)

例子:

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 

方法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 
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
 $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.
?>


Javascript


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 
#include 
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> 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 v_left = new Vector<>();;
        Vector 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 v_left = new List();;
        List v_right = new List();
 
        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


Javascript


输出:
index = 2

时间复杂度:O(n*n)
辅助空间:O(1)

方法二:(有效的解决方案)
1- 创建两个对类型的向量,即 v_left 和 v_right
2- v_left[i] 存储其左侧奇偶数的频率
3- v_right[i] 存储其右侧奇偶数的频率
4- 现在检查 (v_left[i].first == v_right[i].first || v_left[i].second == v_right[i].second)
如果真返回我
5- 最后,如果不存在这样的索引,则返回 -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 
#include 
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> 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 v_left = new Vector<>();;
        Vector 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 v_left = new List();;
        List v_right = new List();
 
        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

Javascript


输出:
index = 2

时间复杂度: O(n)
辅助空间: O(n)
进一步优化:我们可以优化程序中使用的空间。我们可以制作整数向量,而不是制作成对的向量。我们可以使用奇数元素的数量等于总元素减去偶数元素的总数这一事实。类似地,偶数元素的数量等于总元素减去奇数元素的总数。