📌  相关文章
📜  计算可以从给定数组中获得的具有单个不同元素的子数组

📅  最后修改于: 2021-04-29 14:27:31             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算由可以从给定数组获得的单个不同元素组成的子数组的数量。

例子:

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

  • 初始化映射以存储数组元素的频率。
  • 遍历数组并更新Map中每个元素的频率。
  • 遍历地图,检查当前元素的频率是否大于1。
  • 如果确定为真,则按当前元素频率– 1递增子数组的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count subarrays of
// single distinct element into
// which given array can be split
void divisionalArrays(int arr[3], int N)
{
    // Stores the count
    int sum = N;
 
    // Stores frequency of
    // array elements
    unordered_map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        mp[arr[i]]++;
    }
 
    // Traverse the map
    for (auto x : mp) {
        if (x.second > 1) {
 
            // Increase count of
            // subarrays by (frequency-1)
            sum += x.second - 1;
        }
    }
    cout << sum << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 3 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
    divisionalArrays(arr, N);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to count subarrays of
  // single distinct element into
  // which given array can be split
  static void divisionalArrays(int arr[], int N)
  {
 
    // Stores the count
    int sum = N;
 
    // Stores frequency of
    // array elements
    HashMap mp
      = new HashMap();
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
      if (mp.containsKey(arr[i]))
      {
        mp.put(arr[i], mp.get(arr[i]) + 1);
      }
      else
      {
        mp.put(arr[i], 1);
      }
    }
 
    // Traverse the map
    for (Map.Entry x : mp.entrySet())
    {
 
      if ((int)x.getValue() > 1)
      {
 
        // Increase count of
        // subarrays by (frequency-1)
        sum += (int)x.getValue() - 1;
      }
    }
 
    System.out.println(sum);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 1, 1, 3 };
    int N = arr.length;
    divisionalArrays(arr, N);
  }
}
 
// This code is contributed by Dharanendra L V


Python3
# python 3 program for the above approach
from collections import defaultdict
 
# Function to count subarrays of
# single distinct element into
# which given array can be split
def divisionalArrays(arr, N):
 
    # Stores the count
    sum = N
 
    # Stores frequency of
    # array elements
    mp = defaultdict(int)
 
    # Traverse the array
    for i in range(N):
        mp[arr[i]] += 1
 
    # Traverse the map
    for x in mp:
        if (mp[x] > 1):
 
            # Increase count of
            # subarrays by (frequency-1)
            sum += mp[x] - 1
    print(sum)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 1, 3]
    N = len(arr)
    divisionalArrays(arr, N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count subarrays of
// single distinct element into
// which given array can be split
static void divisionalArrays(int []arr, int N)
{
     
    // Stores the count
    int sum = N;
     
    // Stores frequency of
    // array elements
    Dictionary mp = new Dictionary();
     
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
        if (mp.ContainsKey(arr[i]))
        {
            mp[arr[i]] =  mp[arr[i]] + 1;
        }
        else
        {
            mp.Add(arr[i], 1);
        }
    }
     
    // Traverse the map
    foreach(KeyValuePair x in mp)
    {
        if ((int)x.Value > 1)
        {
             
            // Increase count of
            // subarrays by (frequency-1)
            sum += (int)x.Value - 1;
        }
    }
     
    Console.WriteLine(sum);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 3 };
    int N = arr.Length;
     
    divisionalArrays(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


输出:
4

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