给定一个大小为N的数组arr[] ,任务是计算可以从给定数组中获得的由单个不同元素组成的子数组的数量。
例子:
Input: N = 4, arr[] = { 2, 2, 2, 2 }
Output: 7
Explanation: All such subarrays {{2}, {2}, {2}, {2}, {2, 2}, {2, 2, 2}, {2, 2, 2, 2}}. Therefore, total count of such subarrays are 7.
Input: N = 3, arr[] = { 1, 1, 3 }
Output: 4
处理方法:按照以下步骤解决问题:
- 初始化一个 Map 来存储数组元素的频率。
- 遍历数组并更新Map中每个元素的频率。
- 遍历 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
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。