📌  相关文章
📜  给定三个数字的出现次数相等的子数组

📅  最后修改于: 2021-05-17 18:06:54             🧑  作者: Mango

给定一个数组arr []和三个整数X,Y,Z ,任务是从该数组中找到X,YZ出现次数相等的子数组的数量。

例子:

方法:请按照以下步骤解决问题:

  1. 初始化三个变量,例如fNum_count = 0,sNum_count = 0和tNum_count = 0以及mini
  2. 初始化Map,int>,int>。
  3. {0,0,0}的递增频率一次。
  4. 遍历数组,如果找到任何给定的数字,则增加其对应的计数,并从所有数组中减去三个的最小值。
  5. 遍历后,增加这三个变量的值集的频率。
  6. 现在初始化一个变量,例如final_ans。
  7. 遍历地图并将每个频率的v *(v-1)/ 2加到final_ans
  8. 打印final_ans作为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
typedef long long ll;
 
// Function to count subarrays
void countSubarrays(int arr[], int N,
                    int X, int Y, int Z)
{
 
    map, int>, int> m;
    m[{ { 0, 0 }, 0 }]++;
 
    int fNum_count = 0, sNum_count = 0,
        tNum_count = 0;
    int mini;
 
    // Traverse the array
    for (int i = 0; i < N; ++i) {
 
        // Check is arr[i] is equal to X
        if (arr[i] == X) {
 
            // Increment fNum_count
            fNum_count++;
 
            mini = min(min(fNum_count,
                           sNum_count),
                       tNum_count);
            fNum_count -= mini;
            sNum_count -= mini;
            tNum_count -= mini;
        }
 
        // Check is arr[i] is equal to Y
        else if (arr[i] == Y) {
 
            // Increment the count of sNum_count
            sNum_count++;
 
            mini = min(min(fNum_count,
                           sNum_count),
                       tNum_count);
            fNum_count -= mini;
            sNum_count -= mini;
            tNum_count -= mini;
        }
 
        // Check is arr[i] is equal to Z
        else if (arr[i] == Z) {
 
            // Increment the count of
            // tNum_count
            tNum_count++;
 
            mini = min(min(fNum_count,
                           sNum_count),
                       tNum_count);
            fNum_count -= mini;
            sNum_count -= mini;
            tNum_count -= mini;
        }
        m[{ { fNum_count, sNum_count },
            tNum_count }]++;
    }
 
    ll final_ans = 0;
    map, int>,
        int>::iterator it;
 
    // Iterate over the map
    for (it = m.begin(); it != m.end();
         ++it) {
        ll val = it->second;
        final_ans += (val * (val - 1)) / 2;
    }
 
    // Print the  answer
    cout << final_ans;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 3, 6, 7, 8, 3, 6, 7 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of X, Y & Z
    int X = 3, Y = 6, Z = 7;
 
    // Function Call
    countSubarrays(arr, N, X, Y, Z);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Function to count subarrays
def countSubarrays(arr, N, X, Y, Z):
    m = {}
    m[(  ( 0, 0 ), 0 )] = 1
    fNum_count, sNum_count = 0, 0
    tNum_count = 0
    mini = 0
 
    # Traverse the array
    for i in range(N):
 
        # Check is arr[i] is equal to X
        if (arr[i] == X):
 
            # Increment fNum_count
            fNum_count += 1
 
            mini = min(min(fNum_count,sNum_count),tNum_count)
            fNum_count -= mini
            sNum_count -= mini
            tNum_count -= mini
 
        # Check is arr[i] is equal to Y
        elif (arr[i] == Y):
 
            # Increment the count of sNum_count
            sNum_count+=1
 
            mini = min(min(fNum_count,sNum_count), tNum_count)
 
            fNum_count -= mini
            sNum_count -= mini
            tNum_count -= mini
 
        # Check is arr[i] is equal to Z
        elif (arr[i] == Z):
 
            # Increment the count of
            # tNum_count
            tNum_count +=1
 
            mini = min(min(fNum_count, sNum_count), tNum_count)
 
            fNum_count -= mini
            sNum_count -= mini
            tNum_count -= mini
 
        m[(( fNum_count, sNum_count ),tNum_count )] = m.get((( fNum_count, sNum_count ),tNum_count ),0)+1
 
    final_ans = 0
     
    # Iterate over the map
    for it in m:
        val = m[it]
        final_ans += (val * (val - 1)) // 2
 
    # Prthe  answer
    print (final_ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [3, 6, 7, 8, 3, 6, 7]
 
    # Size of the array
    N = len(arr)
 
    # Given value of X, Y & Z
    X, Y, Z = 3, 6, 7
 
    # Function Call
    countSubarrays(arr, N, X, Y, Z)
 
    # This code is contributed by mohit kumar 29.


输出:
8

时间复杂度: O(N * LogN)
辅助空间: O(N)