📌  相关文章
📜  计算两个给定元素出现次数相等的子数组

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

计算两个给定元素出现次数相等的子数组

给定一个数组和两个整数 x 和 y,找出其中 x 的出现次数等于 y 的出现次数的子数组的数量。

例子:

Input : arr[] = {1, 2, 1},
        x = 1, y = 2 
Output : 2
The possible sub-arrays have same equal number 
of occurrences of x and y are:
1) {1, 2}, x and y have same occurrence(1).
2) {2, 1}, x and y have same occurrence(1).

Input : arr[] = {1, 2, 1},
        x = 4, y = 6
Output : 6
The possible sub-arrays have same equal number of 
occurrences of x and y are:
1) {1}, x and y have same occurrence(0).
2) {2}, x and y have same occurrence(0).
3) {1}, x and y have same occurrence(0).
1) {1, 2}, x and y have same occurrence(0).
2) {2, 1}, x and y have same occurrence(0).
3) {1, 2, 1}, x and y have same occurrence(0).

Input : arr[] = {1, 2, 1},
        x = 1, y = 1
Output : 6
The possible sub-arrays have same equal number 
of occurrences of x and y are:
1) {1}, x and y have same occurrence(1).
2) {2}, x and y have same occurrence(0).
3) {1}, x and y have same occurrence(1).
1) {1, 2}, x and y have same occurrence(1).
2) {2, 1}, x and y have same occurrence(1).
3) {1, 2, 1}, x and y have same occurrences (2).

我们可以简单地生成所有可能的子数组,并检查每个子数组在该特定子数组中 x 的出现次数是否等于 y 的出现次数。

C++
/* C++ program to count number of sub-arrays in which
   number of occurrence of x is equal to that of y
    using brute force */
#include 
using namespace std;
 
int sameOccurrence(int arr[], int n, int x, int y)
{
    int result = 0;
 
    // Check for each subarray for the required condition
    for (int i = 0; i <= n - 1; i++) {
        int ctX = 0,  ctY = 0;
        for (int j = i; j <= n - 1; j++) {
            if (arr[j] == x)
                ctX += 1;
            else if (arr[j] == y)
                ctY += 1;
            if (ctX == ctY)
                result += 1;           
        }
    }
 
    return (result);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2, y = 3;
    cout << sameOccurrence(arr, n, x, y);
    return (0);
}


Java
/* Java program to count number of sub-arrays in which
number of occurrence of x is equal to that of y
    using brute force */
import java.util.*;
 
class solution
{
 
static int sameOccurrence(int arr[], int n, int x, int y)
{
    int result = 0;
 
    // Check for each subarray for the required condition
    for (int i = 0; i <= n - 1; i++) {
        int ctX = 0, ctY = 0;
        for (int j = i; j <= n - 1; j++) {
            if (arr[j] == x)
                ctX += 1;
            else if (arr[j] == y)
                ctY += 1;
            if (ctX == ctY)
                result += 1;        
        }
    }
 
    return (result);
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = arr.length;
    int x = 2, y = 3;
    System.out.println(sameOccurrence(arr, n, x, y));
 
}
}
 
// This code is contributed by
// Sahil_shelangia


Python3
# Python 3 program to count number of
# sub-arrays in which number of occurrence
# of x is equal to that of y using brute force
def sameOccurrence(arr, n, x, y):
    result = 0
 
    # Check for each subarray for
    # the required condition
    for i in range(n):
        ctX = 0
        ctY = 0
        for j in range(i, n, 1):
            if (arr[j] == x):
                ctX += 1;
            elif (arr[j] == y):
                ctY += 1
            if (ctX == ctY):
                result += 1
 
    return (result)
 
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 2, 3, 4, 1]
    n = len(arr)
    x = 2
    y = 3
    print(sameOccurrence(arr, n, x, y))
 
# This code is contributed by
# Surendra_Gangwar


C#
/* C# program to count number of sub-arrays in which
number of occurrence of x is equal to that of y
using brute force */
using System;
 
class GFG
{
 
static int sameOccurrence(int[] arr, int n,
                            int x, int y)
{
    int result = 0;
 
    // Check for each subarray for
    // the required condition
    for (int i = 0; i <= n - 1; i++)
    {
        int ctX = 0, ctY = 0;
        for (int j = i; j <= n - 1; j++)
        {
            if (arr[j] == x)
                ctX += 1;
            else if (arr[j] == y)
                ctY += 1;
            if (ctX == ctY)
                result += 1;        
        }
    }
    return (result);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 1, 2, 2, 3, 4, 1 };
    int n = arr.Length;
    int x = 2, y = 3;
    Console.Write(sameOccurrence(arr, n, x, y));
 
}
}
 
// This code is contributed by Ita_c.


PHP


Javascript


C++
/* C++ program to count number of sub-arrays in which
  number of occurrence of x is equal to that of y using
  efficient approach in terms of time */
#include 
using namespace std;
 
int sameOccurrence(int arr[], int n, int x, int y)
{
    int countX[n], countY[n];
 
    map m; // To store counts of same diffs
 
    // Count occurrences of x and y
    for (int i = 0; i < n; i++) {
        if (arr[i] == x) {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        } else {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y) {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        } else {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
   
         // Increment count of current
         m[countX[i] - countY[i]]++;
    }
 
    // Traverse map and commute result.
    int result = m[0];
    for (auto it = m.begin(); it != m.end(); it++)
        result = result + ((it->second) * ((it->second) - 1)) / 2;
     
    return (result);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2, y = 3;
    cout << sameOccurrence(arr, n, x, y);
    return (0);
}


Java
/* Java program to count number of sub-arrays in which
number of occurrence of x is equal to that of y using
efficient approach in terms of time */
import java.util.*;
 
class GFG
{
 
static int sameOccurrence(int arr[], int n, int x, int y)
{
    int []countX = new int[n];
    int []countY = new int[n];
 
    Map m = new HashMap<>();
     
    // To store counts of same diff
    // Count occurrences of x and y
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == x)
        {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        }
        else
        {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y)
        {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        }
        else
        {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
     
        // Increment count of current
        if(m.containsKey(countX[i] - countY[i]))
        {
            m.put(countX[i] - countY[i], m.get(countX[i] - countY[i])+1);
        }
        else
        {
            m.put(countX[i] - countY[i], 1);
        }
    }
 
    // Traverse map and commute result.
    int result = m.get(0);
    for (Map.Entry it : m.entrySet())
        result = result + ((it.getValue()) * ((it.getValue()) - 1)) / 2;
     
    return (result);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = arr.length;
    int x = 2, y = 3;
    System.out.println(sameOccurrence(arr, n, x, y));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to count number of
# sub-arrays in which number of occurrence
# of x is equal to that of y using efficient
# approach in terms of time */
def sameOccurrence( arr, n, x, y):
 
    countX = [0 for i in range(n)]
    countY = [0 for i in range(n)]
 
    # To store counts of same diffs
    m = dict()
 
    # Count occurrences of x and y
    for i in range(n):
        if (arr[i] == x):
            if (i != 0):
                countX[i] = countX[i - 1] + 1
            else:
                countX[i] = 1
        else:
            if (i != 0):
                countX[i] = countX[i - 1]
            else:
                countX[i] = 0
 
        if (arr[i] == y):
            if (i != 0):
                countY[i] = countY[i - 1] + 1
            else:
                countY[i] = 1
        else:
            if (i != 0):
                countY[i] = countY[i - 1]
            else:
                countY[i] = 0
         
        # Increment count of current
        m[countX[i] - countY[i]] = m.get(countX[i] -
                                         countY[i], 0) + 1
     
    # Traverse map and commute result.
    result = m[0]
    for j in m:
        result += (m[j] * (m[j] - 1)) // 2
     
    return result
 
# Driver code
arr = [1, 2, 2, 3, 4, 1]
n = len(arr)
x, y = 2, 3
print(sameOccurrence(arr, n, x, y))
  
# This code is contributed
# by mohit kumar


C#
/* C# program to count number of sub-arrays in which
number of occurrence of x is equal to that of y using
efficient approach in terms of time */
using System;
using System.Collections.Generic;
 
class GFG
{
 
static int sameOccurrence(int []arr, int n, int x, int y)
{
    int []countX = new int[n];
    int []countY = new int[n];
 
    Dictionary m = new Dictionary();
     
    // To store counts of same diff
    // Count occurrences of x and y
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == x)
        {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        }
        else
        {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y)
        {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        }
        else
        {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
     
        // Increment count of current
        if(m.ContainsKey(countX[i] - countY[i]))
        {
            var v = m[countX[i] - countY[i]]+1;
            m.Remove(countX[i] - countY[i]);
            m.Add(countX[i] - countY[i], v);
        }
        else
        {
            m.Add(countX[i] - countY[i], 1);
        }
    }
 
    // Traverse map and commute result.
    int result = m[0];
    foreach(KeyValuePair it in m)
        result = result + ((it.Value) * ((it.Value) - 1)) / 2;
     
    return (result);
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3, 4, 1 };
    int n = arr.Length;
    int x = 2, y = 3;
    Console.WriteLine(sameOccurrence(arr, n, x, y));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:

7

时间复杂度 – O(N^2)
辅助空间 - O(1)

有效方法(O(N) 时间复杂度):

在这个解决方案中,辅助空间是 O(N),时间复杂度也是 O(N)。我们创建了两个数组,即 countX[] 和 countY[],它们分别表示 x 和 y 在数组中出现的次数。然后,我们评估另一个数组,比如存储 (countX[i]-countY[i]) 的 diff,i 是数组的索引。现在,将数组 diff 的每个元素的计数存储在映射中,例如 m。将结果初始化为 m[0],因为 diff 数组中出现的 0 为我们提供了满足所需条件的子数组计数。现在,遍历映射并使用握手公式更新结果,因为 diff 数组中的两个相同值表示子数组包含相同数量的 x 和 y。

Explanation:
arr[] = {1, 2, 2, 3, 4, 1};
x = 2, y = 3;

Two arrays countX[] and countY[] are be evaluated as-
countX[] = {0, 1, 2, 2, 2, 2};
countY[] = {0, 0, 0, 1, 1, 1};

Hence, diff[] = {0, 1, 2, 1, 1, 1}; 
(diff[i] = countX[i]-countY[i], i be the index of array)

Now, create a map and store the count of each element of diff in it,
so, finally, we get-
m[0] = 1, m[1] = 4, m[2] = 1;

Initialize result as m[0]
i.e result = m[0] = 1

Further, using handshake formula, updating the 
result as follows-
result  = result + (1*(1-1))/2 = 1 + 0 = 1
result  = result + (4*(4-1))/2 = 1 + 6 = 7
result  = result + (1*(1-1))/2 = 7 + 0 = 7

so, the final result will be 7, required subarrays having 
same number of occurrences of x and y.

C++

/* C++ program to count number of sub-arrays in which
  number of occurrence of x is equal to that of y using
  efficient approach in terms of time */
#include 
using namespace std;
 
int sameOccurrence(int arr[], int n, int x, int y)
{
    int countX[n], countY[n];
 
    map m; // To store counts of same diffs
 
    // Count occurrences of x and y
    for (int i = 0; i < n; i++) {
        if (arr[i] == x) {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        } else {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y) {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        } else {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
   
         // Increment count of current
         m[countX[i] - countY[i]]++;
    }
 
    // Traverse map and commute result.
    int result = m[0];
    for (auto it = m.begin(); it != m.end(); it++)
        result = result + ((it->second) * ((it->second) - 1)) / 2;
     
    return (result);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2, y = 3;
    cout << sameOccurrence(arr, n, x, y);
    return (0);
}

Java

/* Java program to count number of sub-arrays in which
number of occurrence of x is equal to that of y using
efficient approach in terms of time */
import java.util.*;
 
class GFG
{
 
static int sameOccurrence(int arr[], int n, int x, int y)
{
    int []countX = new int[n];
    int []countY = new int[n];
 
    Map m = new HashMap<>();
     
    // To store counts of same diff
    // Count occurrences of x and y
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == x)
        {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        }
        else
        {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y)
        {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        }
        else
        {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
     
        // Increment count of current
        if(m.containsKey(countX[i] - countY[i]))
        {
            m.put(countX[i] - countY[i], m.get(countX[i] - countY[i])+1);
        }
        else
        {
            m.put(countX[i] - countY[i], 1);
        }
    }
 
    // Traverse map and commute result.
    int result = m.get(0);
    for (Map.Entry it : m.entrySet())
        result = result + ((it.getValue()) * ((it.getValue()) - 1)) / 2;
     
    return (result);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3, 4, 1 };
    int n = arr.length;
    int x = 2, y = 3;
    System.out.println(sameOccurrence(arr, n, x, y));
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program to count number of
# sub-arrays in which number of occurrence
# of x is equal to that of y using efficient
# approach in terms of time */
def sameOccurrence( arr, n, x, y):
 
    countX = [0 for i in range(n)]
    countY = [0 for i in range(n)]
 
    # To store counts of same diffs
    m = dict()
 
    # Count occurrences of x and y
    for i in range(n):
        if (arr[i] == x):
            if (i != 0):
                countX[i] = countX[i - 1] + 1
            else:
                countX[i] = 1
        else:
            if (i != 0):
                countX[i] = countX[i - 1]
            else:
                countX[i] = 0
 
        if (arr[i] == y):
            if (i != 0):
                countY[i] = countY[i - 1] + 1
            else:
                countY[i] = 1
        else:
            if (i != 0):
                countY[i] = countY[i - 1]
            else:
                countY[i] = 0
         
        # Increment count of current
        m[countX[i] - countY[i]] = m.get(countX[i] -
                                         countY[i], 0) + 1
     
    # Traverse map and commute result.
    result = m[0]
    for j in m:
        result += (m[j] * (m[j] - 1)) // 2
     
    return result
 
# Driver code
arr = [1, 2, 2, 3, 4, 1]
n = len(arr)
x, y = 2, 3
print(sameOccurrence(arr, n, x, y))
  
# This code is contributed
# by mohit kumar

C#

/* C# program to count number of sub-arrays in which
number of occurrence of x is equal to that of y using
efficient approach in terms of time */
using System;
using System.Collections.Generic;
 
class GFG
{
 
static int sameOccurrence(int []arr, int n, int x, int y)
{
    int []countX = new int[n];
    int []countY = new int[n];
 
    Dictionary m = new Dictionary();
     
    // To store counts of same diff
    // Count occurrences of x and y
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == x)
        {
            if (i != 0)
                countX[i] = countX[i - 1] + 1;
            else
                countX[i] = 1;
        }
        else
        {
            if (i != 0)
                countX[i] = countX[i - 1];
            else
                countX[i] = 0;
        }
        if (arr[i] == y)
        {
            if (i != 0)
                countY[i] = countY[i - 1] + 1;
            else
                countY[i] = 1;
        }
        else
        {
            if (i != 0)
                countY[i] = countY[i - 1];
            else
                countY[i] = 0;
        }
     
        // Increment count of current
        if(m.ContainsKey(countX[i] - countY[i]))
        {
            var v = m[countX[i] - countY[i]]+1;
            m.Remove(countX[i] - countY[i]);
            m.Add(countX[i] - countY[i], v);
        }
        else
        {
            m.Add(countX[i] - countY[i], 1);
        }
    }
 
    // Traverse map and commute result.
    int result = m[0];
    foreach(KeyValuePair it in m)
        result = result + ((it.Value) * ((it.Value) - 1)) / 2;
     
    return (result);
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 3, 4, 1 };
    int n = arr.Length;
    int x = 2, y = 3;
    Console.WriteLine(sameOccurrence(arr, n, x, y));
}
}
 
// This code is contributed by Princi Singh

Javascript


输出:

7

时间复杂度 – O(N)
辅助空间 – O(N)