Suma de todos los elementos de frecuencia par en Matrix

Dada una array NxM de enteros que contienen elementos duplicados. La tarea es encontrar la suma de todos los elementos pares en la array dada. Esa es la suma de todos esos elementos cuya frecuencia es par en la array.
Ejemplos
 

Input : mat[] = {{1, 1, 2},
                {2, 3, 3},
                {4, 5, 3}}
Output : 18
The even occurring elements are 1, 2 and their number
of occurrences are 2, 2 respectively. Therefore,
sum = 1+1+2+2 = 6.

Input : mat[] = {{10, 20},
                 {40, 40}}
Output : 80

Enfoque :
 

  • Atraviese la array y use un mapa para almacenar la frecuencia de los elementos de la array de modo que la clave del mapa sea el elemento de la array y el valor sea su frecuencia en la array.
  • Luego, recorra el mapa para encontrar la frecuencia de los elementos y verifique si es par, luego agregue este elemento por su frecuencia para sumar.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to find sum of all even
// frequency elements in a Matrix
 
#include <bits/stdc++.h>
using namespace std;
 
#define N 3 // Rows
#define M 3 // Columns
 
// Function to find sum of all even
// frequency elements in a Matrix
int sumOddOccurring(int arr[N][M])
{
 
    // Store frequency of elements
    // in matrix
    unordered_map<int, int> mp;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            mp[arr[i][j]]++;
        }
    }
 
    // Sum even frequency elements
    int sum = 0;
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        if (itr->second % 2 == 0) {
            int x = itr->second;
            sum += (itr->first) * (itr->second);
        }
    }
 
    return sum;
}
 
// Driver Code
int main()
{
 
    int mat[N][M] = { { 1, 2, 3 },
                      { 1, 3, 2 },
                      { 1, 5, 6 } };
 
    cout << sumOddOccurring(mat) << endl;
 
    return 0;
}

Java

// Java program to find sum of all even
// frequency elements in a Matrix
import java.util.*;
 
class GFG
{
 
static final int N = 3; // Rows
static final int M = 3; // Columns
 
// Function to find sum of all even
// frequency elements in a Matrix
static int sumOddOccurring(int arr[][])
{
 
    // Store frequency of elements
    // in matrix
    Map<Integer,
        Integer> mp = new HashMap<Integer,
                                  Integer>();
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if(mp.get(arr[i][j]) == null)
                mp.put(arr[i][j], 1);
            else
                mp.put(arr[i][j],
                      (mp.get(arr[i][j]) + 1));
        }
    }
 
    // Sum even frequency elements
    int sum = 0;
    Set< Map.Entry<Integer,
                   Integer> > st = mp.entrySet();
 
    for (Map.Entry< Integer, Integer> me:st)
    {
        if (me.getValue() % 2 == 0)
        {
            int x = me.getValue();
            sum += (me.getKey()) * (me.getValue());
        }
    }
    return sum;
}
 
// Driver Code
public static void main(String args[])
{
    int mat[][] = {{ 1, 2, 3 },
                   { 1, 3, 2 },
                   { 1, 5, 6 }};
 
    System.out.print(sumOddOccurring(mat));
}
}
 
// This code is contributed by Arnab Kundu

Python3

# Python3 program to find sum of all even
# frequency elements in a Matrix
import sys
 
N = 3 # Rows
M = 3 # Columns
 
# Function to find sum of all even
# frequency elements in a Matrix
def sumOddOccuring(arr):
 
    # Store frequencies of elements
    # in matrix
    mp = dict()
    for i in range(N):
        for j in range(M):
            if arr[i][j] in mp:
                mp[arr[i][j]] += 1
            else:
                mp[arr[i][j]] = 1
 
    # Sum of even frequency elements
    s = 0
    for i in mp:
        if mp[i] % 2 == 0:
            x = mp[i]
            s += i * mp[i]
 
    return s
 
# Driver code
if __name__ == "__main__":
    mat = [[1, 2, 3],
           [1, 3, 2],
           [1, 5, 6]]
 
    print(sumOddOccuring(mat))
 
# This code is contributed by
# sanjeev2552

C#

// C# program to find sum of all even
// frequency elements in a Matrix
using System;
using System.Collections.Generic;
 
class Sol
{
 
static readonly int N = 3; // Rows
static readonly int M = 3; // Columns
 
// Function to find sum of all even
// frequency elements in a Matrix
static int sumOddOccurring(int [,]arr)
{
 
    // Store frequency of elements
    // in matrix
    Dictionary<int, int> mp = new Dictionary<int,int>();
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if(!mp.ContainsKey(arr[i, j]))
                mp.Add(arr[i, j], 1);
            else{
                var val = mp[arr[i, j]];
                mp.Remove(arr[i, j]);
                mp.Add(arr[i, j], val + 1);
            }
        }
    }
 
    // Sum even frequency elements
    int sum = 0;
    foreach(KeyValuePair<int, int> entry in mp)
    {
        if(entry.Value % 2 == 0){
            sum += entry.Key * entry.Value;
        }
    }
 
    return sum;
}
 
// Driver Code
public static void Main(String []args)
{
 
    int [,]mat = { { 1, 2, 3 },
                    { 1, 3, 2 },
                    { 1, 5, 6 } };
 
    Console.Write( sumOddOccurring(mat) );
 
}
}
 
/* This code contributed by PrinciRaj1992 */

Javascript

<script>
 
// JavaScript program to find sum of all even
// frequency elements in a Matrix
 
var N = 3; // Rows
var M = 3; // Columns
 
// Function to find sum of all even
// frequency elements in a Matrix
function sumOddOccurring(arr)
{
 
    // Store frequency of elements
    // in matrix
    var mp = new Map();
    for (var i = 0; i < N; i++)
    {
        for (var j = 0; j < M; j++)
        {
            if(!mp.has(arr[i][j]))
                mp.set(arr[i][j], 1);
            else{
                var val = mp.get(arr[i][j]);
                mp.delete(arr[i][j]);
                mp.set(arr[i][j], val + 1);
            }
        }
    }
 
    // Sum even frequency elements
    var sum = 0;
    mp.forEach((value, key) => {
        if(value % 2 == 0){
            sum += key * value;
        }
    });
 
    return sum;
}
 
// Driver Code
var mat = [[1, 2, 3 ],
                [1, 3, 2 ],
                [1, 5, 6 ]];
document.write( sumOddOccurring(mat) );
 
 
 
</script>
Producción: 

10

 

Tiempo Complejidad : O(N x M) 
Espacio Auxiliar : O(N x M)
 

Publicación traducida automáticamente

Artículo escrito por barykrg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *