每个数组元素左右存在的不同元素的计数差异
给定一个由N个整数组成的数组arr[] ,每个数组元素的任务是在给定数组arr[]中找到它左右不同元素的计数之间的绝对差。
例子:
Input: arr[] = {7, 7, 3, 2, 3}
Output: 2 2 0 1 2
Explanation:
Count of distinct elements that occur to the left of every index is given by [0, 0, 1, 1, 2] and the number of distinct elements that occur to the right of every index is [2, 2, 1, 0, 0]. Taking absolute difference of both gives the above output.
Input: arr[] = {4, 3, 2, 3}
Output: 2 0 1 2
朴素的方法:给定的问题可以使用 Set Data Structure 来解决,想法是遍历范围[0, i – 1]以找到每个元素左侧的不同元素的计数,并类似地遍历数组range [i + 1, N – 1]找到每个元素右侧的不同元素。请按照以下步骤解决给定的问题:
- 初始化一个数组res[] ,它存储每个数组元素的不同元素的绝对差值。
- 遍历给定的数组,对于索引处的每个元素,我执行以下操作:
- 迭代范围[0, i – 1]并插入集合中的所有元素,例如S1 。
- 迭代范围[i + 1, N – 1]并插入集合中的所有元素,例如S2 。
- 将res[i]的值更新为集合S1和S2大小的绝对差。
- 完成上述步骤后,打印数组res[]作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
void findDifferenceArray(int arr[], int N)
{
// Stores distinct array element
// in the left and right
set S1;
set S2;
// Traverse the array
for (int i = 0; i < N; i++) {
// Insert all element to the left
// in the set S1
for (int j = 0; j < i; j++) {
S1.insert(arr[j]);
}
// Insert all element to the right
// in the set S2
for (int j = i + 1; j < N; j++) {
S2.insert(arr[j]);
}
// Print the difference
cout << abs((int)S1.size()
- (int)S2.size())
<< ' ';
S1.clear();
S2.clear();
}
}
// Driver Code
int main()
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
findDifferenceArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.HashSet;
class GFG{
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
public static void findDifferenceArray(int arr[], int N)
{
// Stores distinct array element
// in the left and right
HashSet S1 = new HashSet();
HashSet S2 = new HashSet();
// Traverse the array
for (int i = 0; i < N; i++) {
// Insert all element to the left
// in the set S1
for (int j = 0; j < i; j++) {
S1.add(arr[j]);
}
// Insert all element to the right
// in the set S2
for (int j = i + 1; j < N; j++) {
S2.add(arr[j]);
}
// Print the difference
System.out.print(Math.abs(S1.size() - S2.size()) + " ");
S1.clear();
S2.clear();
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = arr.length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by gfgking.
Python3
# Python3 program for the above approach
# Function to find the difference of
# count of distinct elements to the
# left and right for each array elements
def findDifferenceArray(arr, N) :
# Stores distinct array element
# in the left and right
S1 = set();
S2 = set();
# Traverse the array
for i in range(N) :
# Insert all element to the left
# in the set S1
for j in range(i) :
S1.add(arr[j]);
# Insert all element to the right
# in the set S2
for j in range(i + 1, N) :
S2.add(arr[j]);
# Print the difference
print(abs(len(S1) - len(S2)),end=' ');
S1.clear();
S2.clear();
# Driver Code
if __name__ == "__main__" :
arr = [ 7, 7, 3, 2, 3 ];
N = len(arr);
findDifferenceArray(arr, N);
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
public static void findDifferenceArray(int[] arr, int N)
{
// Stores distinct array element
// in the left and right
HashSet S1 = new HashSet();
HashSet S2 = new HashSet();
// Traverse the array
for (int i = 0; i < N; i++) {
// Insert all element to the left
// in the set S1
for (int j = 0; j < i; j++) {
S1.Add(arr[j]);
}
// Insert all element to the right
// in the set S2
for (int j = i + 1; j < N; j++) {
S2.Add(arr[j]);
}
// Print the difference
Console.Write(Math.Abs(S1.Count - S2.Count) + " ");
S1.Clear();
S2.Clear();
}
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 7, 7, 3, 2, 3 };
int N = arr.Length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
void findDifferenceArray(int arr[], int N)
{
// Stores the frequency of array
// element to the left and the right
unordered_map leftMap, rightMap;
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
rightMap[arr[i]]++;
}
// Stores the resultant differences
vector res;
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.size();
// Decrement the frequency
if (rightMap[arr[i]] > 1) {
rightMap[arr[i]]--;
}
else {
rightMap.erase(arr[i]);
}
// Find the count in the left
int countRight = rightMap.size();
// Insert the resultant difference
res.push_back(abs(countRight - countLeft));
// Increment the frequency
leftMap[arr[i]]++;
}
// Print the result
for (auto& it : res) {
cout << it << ' ';
}
}
// Driver Code
int main()
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
findDifferenceArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
static void findDifferenceArray(int arr[], int N)
{
// Stores the frequency of array
// element to the left and the right
HashMap leftMap = new HashMap<>();
HashMap rightMap
= new HashMap<>();
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
if (rightMap.containsKey(arr[i]))
rightMap.put(arr[i],
rightMap.get(arr[i]) + 1);
else
rightMap.put(arr[i], 1);
}
// Stores the resultant differences
Vector res = new Vector<>();
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.size();
// Decrement the frequency
if (rightMap.get(arr[i]) > 1) {
rightMap.put(arr[i],
rightMap.get(arr[i]) - 1);
}
else {
rightMap.remove(arr[i]);
}
// Find the count in the left
int countRight = rightMap.size();
// Insert the resultant difference
res.add(Math.abs(countRight - countLeft));
// Increment the frequency
if (leftMap.containsKey(arr[i]))
leftMap.put(arr[i],
leftMap.get(arr[i]) + 1);
else
leftMap.put(arr[i], 1);
}
// Print the result
for (int it : res) {
System.out.print(it + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = arr.length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by Rajput-Ji
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
static void findDifferenceArray(int []arr, int N) {
// Stores the frequency of array
// element to the left and the right
Dictionary leftMap = new Dictionary();
Dictionary rightMap = new Dictionary();
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
if (rightMap.ContainsKey(arr[i]))
rightMap[arr[i]] = rightMap[arr[i]] + 1;
else
rightMap.Add(arr[i], 1);
}
// Stores the resultant differences
List res = new List();
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.Count;
// Decrement the frequency
if (rightMap[arr[i]] > 1) {
rightMap[arr[i]] = rightMap[arr[i]] - 1;
} else {
rightMap.Remove(arr[i]);
}
// Find the count in the left
int countRight = rightMap.Count;
// Insert the resultant difference
res.Add(Math.Abs(countRight - countLeft));
// Increment the frequency
if (leftMap.ContainsKey(arr[i]))
leftMap[arr[i]]= leftMap[arr[i]] + 1;
else
leftMap.Add(arr[i], 1);
}
// Print the result
foreach (int it in res) {
Console.Write(it + " ");
}
}
// Driver Code
public static void Main(String[] args) {
int []arr = { 7, 7, 3, 2, 3 };
int N = arr.Length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by Rajput-Ji
3 1 1 1 3
时间复杂度: O((N 2 )*log N)
辅助空间: O(N)
高效方法:上述方法也可以通过存储每个数组元素的左右不同元素的频率来优化,然后找到每个数组元素的结果差异。请按照以下步骤解决给定的问题:
- 初始化两个 unordered_map leftMap和rightMap分别存储每个索引左右不同的元素。
- 遍历给定的数组并将所有数组元素插入到rightMap中。
- 使用变量i遍历给定的数组并执行以下步骤:
- 计算当前元素左侧的不同元素(比如countLeft )作为地图leftMap的大小。
- 将地图rightMap中当前元素的频率减少1 。
- 计算当前元素右侧的不同元素(比如countRight )作为地图rightMap的大小。
- 将地图leftMap中当前元素的频率增加1 。
- 插入地图leftMap和rightMap大小的绝对差值。
- 完成上述步骤后,打印数组res[]作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
void findDifferenceArray(int arr[], int N)
{
// Stores the frequency of array
// element to the left and the right
unordered_map leftMap, rightMap;
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
rightMap[arr[i]]++;
}
// Stores the resultant differences
vector res;
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.size();
// Decrement the frequency
if (rightMap[arr[i]] > 1) {
rightMap[arr[i]]--;
}
else {
rightMap.erase(arr[i]);
}
// Find the count in the left
int countRight = rightMap.size();
// Insert the resultant difference
res.push_back(abs(countRight - countLeft));
// Increment the frequency
leftMap[arr[i]]++;
}
// Print the result
for (auto& it : res) {
cout << it << ' ';
}
}
// Driver Code
int main()
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
findDifferenceArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
static void findDifferenceArray(int arr[], int N)
{
// Stores the frequency of array
// element to the left and the right
HashMap leftMap = new HashMap<>();
HashMap rightMap
= new HashMap<>();
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
if (rightMap.containsKey(arr[i]))
rightMap.put(arr[i],
rightMap.get(arr[i]) + 1);
else
rightMap.put(arr[i], 1);
}
// Stores the resultant differences
Vector res = new Vector<>();
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.size();
// Decrement the frequency
if (rightMap.get(arr[i]) > 1) {
rightMap.put(arr[i],
rightMap.get(arr[i]) - 1);
}
else {
rightMap.remove(arr[i]);
}
// Find the count in the left
int countRight = rightMap.size();
// Insert the resultant difference
res.add(Math.abs(countRight - countLeft));
// Increment the frequency
if (leftMap.containsKey(arr[i]))
leftMap.put(arr[i],
leftMap.get(arr[i]) + 1);
else
leftMap.put(arr[i], 1);
}
// Print the result
for (int it : res) {
System.out.print(it + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 7, 7, 3, 2, 3 };
int N = arr.length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by Rajput-Ji
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find the difference of
// count of distinct elements to the
// left and right for each array elements
static void findDifferenceArray(int []arr, int N) {
// Stores the frequency of array
// element to the left and the right
Dictionary leftMap = new Dictionary();
Dictionary rightMap = new Dictionary();
// Stores the frequency of array
// element in the map rightMap
for (int i = 0; i < N; i++) {
if (rightMap.ContainsKey(arr[i]))
rightMap[arr[i]] = rightMap[arr[i]] + 1;
else
rightMap.Add(arr[i], 1);
}
// Stores the resultant differences
List res = new List();
// Traverse the array
for (int i = 0; i < N; i++) {
// Find the count in the left
int countLeft = leftMap.Count;
// Decrement the frequency
if (rightMap[arr[i]] > 1) {
rightMap[arr[i]] = rightMap[arr[i]] - 1;
} else {
rightMap.Remove(arr[i]);
}
// Find the count in the left
int countRight = rightMap.Count;
// Insert the resultant difference
res.Add(Math.Abs(countRight - countLeft));
// Increment the frequency
if (leftMap.ContainsKey(arr[i]))
leftMap[arr[i]]= leftMap[arr[i]] + 1;
else
leftMap.Add(arr[i], 1);
}
// Print the result
foreach (int it in res) {
Console.Write(it + " ");
}
}
// Driver Code
public static void Main(String[] args) {
int []arr = { 7, 7, 3, 2, 3 };
int N = arr.Length;
findDifferenceArray(arr, N);
}
}
// This code is contributed by Rajput-Ji
3 1 1 1 3
时间复杂度: O(N)
辅助空间: O(N)