📌  相关文章
📜  求每个元素左右相等元素的计数差

📅  最后修改于: 2021-10-27 17:02:54             🧑  作者: Mango

给定一个大小为N的数组arr[] 。任务是为每个元素找到X – Y ,其中Xj的计数,使得arr[i] = arr[j]并且j > iYj的计数,满足arr[i] = arr[j]j < i
例子:

方法:一种有效的方法是使用地图。一个映射用于存储数组中每个元素的计数,另一个映射用于计算每个元素剩下的相同元素的数量。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the count of equal
// elements to the right - count of equal
// elements to the left for each of the element
void right_left(int a[], int n)
{
 
    // Maps to store the frequency and same
    // elements to the left of an element
    unordered_map total, left;
 
    // Count the frequency of each element
    for (int i = 0; i < n; i++)
        total[a[i]]++;
 
    for (int i = 0; i < n; i++) {
 
        // Print the answer for each element
        cout << (total[a[i]] - 1 - (2 * left[a[i]])) << " ";
 
        // Increment it's left frequency
        left[a[i]]++;
    }
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 2, 1 };
    int n = sizeof(a) / sizeof(a[0]);
 
    right_left(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to find the count of equal
// elements to the right - count of equal
// elements to the left for each of the element
static void right_left(int a[], int n)
{
 
    // Maps to store the frequency and same
    // elements to the left of an element
    Map total = new HashMap<>();
    Map left = new HashMap<>();
 
    // Count the frequency of each element
    for (int i = 0; i < n; i++)
        total.put(a[i],
        total.get(a[i]) == null ? 1 :
        total.get(a[i]) + 1);
 
    for (int i = 0; i < n; i++)
    {
 
        // Print the answer for each element
        System.out.print((total.get(a[i]) - 1 -
                         (2 * (left.containsKey(a[i]) == true ?
                               left.get(a[i]) : 0))) + " ");
 
        // Increment it's left frequency
        left.put(a[i],
        left.get(a[i]) == null ? 1 :
        left.get(a[i]) + 1);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 3, 2, 1 };
    int n = a.length;
 
    right_left(a, n);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
 
# Function to find the count of equal
# elements to the right - count of equal
# elements to the left for each of the element
def right_left(a, n) :
 
    # Maps to store the frequency and same
    # elements to the left of an element
    total = dict.fromkeys(a, 0);
    left = dict.fromkeys(a, 0);
 
    # Count the frequency of each element
    for i in range(n) :
        if a[i] not in total :
            total[a[i]] = 1
        total[a[i]] += 1;
 
    for i in range(n) :
 
        # Print the answer for each element
        print(total[a[i]] - 1 - (2 * left[a[i]]),
                                      end = " ");
 
        # Increment it's left frequency
        left[a[i]] += 1;
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 1, 2, 3, 2, 1 ];
    n = len(a);
 
    right_left(a, n);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
     
class GFG
{
 
// Function to find the count of equal
// elements to the right - count of equal
// elements to the left for each of the element
static void right_left(int []a, int n)
{
 
    // Maps to store the frequency and same
    // elements to the left of an element
    Dictionary total = new Dictionary();
    Dictionary left = new Dictionary();
 
    // Count the frequency of each element
    for (int i = 0; i < n; i++)
    {
        if(total.ContainsKey(a[i]))
        {
            total[a[i]] = total[a[i]] + 1;
        }
        else{
            total.Add(a[i], 1);
        }
    }
 
    for (int i = 0; i < n; i++)
    {
 
        // Print the answer for each element
        Console.Write((total[a[i]] - 1 -
                      (2 * (left.ContainsKey(a[i]) == true ?
                                   left[a[i]] : 0))) + " ");
 
        // Increment it's left frequency
        if(left.ContainsKey(a[i]))
        {
            left[a[i]] = left[a[i]] + 1;
        }
        else
        {
            left.Add(a[i], 1);
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 1, 2, 3, 2, 1 };
    int n = a.Length;
 
    right_left(a, n);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
1 1 0 -1 -1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程