给定一个由N 个整数组成的数组arr[] ,任务是找到数组中无序对(i, j)的总数,使得arr[i]等于arr[j]并且对于所有子数组i < j给定数组的。
例子:
Input: arr[] = {1, 2, 1, 1}
Output: 6
Explanation: All subarrays of the given array of size greater than 1 are:
- {1, 2}: There are no such pairs satisfying the given criteria. Hence, the count of pairs for the current subarray is 0.
- {2, 1}: There are no such pairs satisfying the given criteria. Hence, the count of pairs for the current subarray is 0.
- {1, 1}: The pairs satisfying the given criteria are (0, 1). Hence, the count of pairs for the current subarray is 1.
- {1, 2, 1}: The pairs satisfying the given criteria are (0, 2). Hence, the count of pairs for the current subarray is 1.
- {2, 1, 1}: The pairs satisfying the given criteria are (1, 1). Hence, the count of pairs for the current subarray is 1.
- {1, 2, 1, 1}: The pairs satisfying the given criteria are (0, 2), (0, 3), and (2, 3). Hence, the count of pairs for the current subarray is 3.
Therefore, the total count from all the subarrays = 1 + 1 + 1 + 3 = 6.
Input: arr[] = {1, 2, 1, 3}
Output: 2
朴素方法:解决给定问题的最简单方法是生成大小大于 1 的所有可能子数组,并为给定数组的所有可能子数组找到满足给定条件的对的计数和。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:上述方法也可以通过将每个不同元素对应的所有位置存储在一个 Map 中,然后对每个元素遍历该元素对应的位置并计算每对出现的子数组的数量来优化上述方法。请按照以下步骤解决问题:
- 将映射M初始化为将键作为一对,将值作为向量。
- 初始化另一个变量,比如ans为0 ,它存储满足给定标准的对的总数。
- 遍历使用变量i在给定数组ARR []和i的值附加到键M [ARR [I]]。
- 遍历地图M并执行以下步骤:
- 初始化一个向量,比如V作为与当前键对应的值。
- 初始化一个变量,比如sum为0 。
- 遍历每个元素V[j]的给定向量V将(sum*(N – V[j]))的值添加到变量ans并将值(V[j] + 1) 添加到变量sum 。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count all pairs (i, j)
// such that arr[i] equals arr[j] in
// all possible subarrays of the array
void countPairs(vector arr)
{
// Stores the size of the array
int N = arr.size();
int ans = 0;
// Stores the positions of all
// the distinct elements
map > M;
// Append index corresponding
// to arr[i] in the map
for (int i = 0;
i < arr.size(); i++) {
M[arr[i]].push_back(i);
}
// Traverse the map M
for (auto it : M) {
vector v = it.second;
int sum = 0;
// Traverse the array
for (int j = 0;
j < v.size(); j++) {
// Update the value of
// ans
ans += sum * (N - v[j]);
// Update the value of
// the sum
sum += v[j] + 1;
}
}
// Print the value of ans
cout << ans;
}
// Driver Code
int main()
{
vector arr = { 1, 2, 1, 1 };
countPairs(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
import java.util.List;
import com.google.common.collect.*;
class GFG {
// Function to count all pairs (i, j)
// such that arr[i] equals arr[j] in
// all possible subarrays of the array
static void countPairs(int[] arr)
{
// Stores the size of the array
int N = arr.length;
int ans = 0;
// Stores the positions of all
// the distinct elements
ListMultimap M = ArrayListMultimap.create();
// Append index corresponding
// to arr[i] in the map
for (int i = 0;
i < arr.length; i++) {
M.put(arr[i], i);
}
// Traverse the map M
for (var it: M.keySet()) {
List v = M.get(it);
int sum = 0;
// Traverse the array
for (int j : v) {
// Update the value of
// ans
ans += sum * (N - j);
// Update the value of
// the sum
sum += j + 1;
}
}
// Print the value of ans
System.out.println(ans);
}
// Driver Code
public static void main (String[] args) {
int[] arr = { 1, 2, 1, 1 };
countPairs(arr);
}
}
// This Code is contributed by ShubhamSingh10
Python3
# Python3 program for the above approach
# Function to count all pairs (i, j)
# such that arr[i] equals arr[j] in
# all possible subarrays of the array
def countPairs(arr):
# Stores the size of the array
N = len(arr)
ans = 0
# Stores the positions of all
# the distinct elements
M = {}
# Append index corresponding
# to arr[i] in the map
for i in range(len(arr)):
if arr[i] in M:
M[arr[i]].append(i)
else:
M[arr[i]] = [i]
# Traverse the map M
for key, value in M.items():
v = value
sum1 = 0
# Traverse the array
for j in range(len(v)):
# Update the value of
# ans
ans += (sum1 * (N - v[j]))
# Update the value of
# the sum
sum1 += v[j] + 1
# Print the value of ans
print(ans)
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 1, 1 ]
countPairs(arr)
# This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
6
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。