Count pairs in an array having sum of elements with their respective sum of digits equal
Given an array arr[] consisting of N positive integers, the task is to count the number of pairs in the array, say (a, b) such that sum of a with its sum of digits is equal to sum of b with its sum of digits.
Examples:
Input: arr[] = {1, 1, 2, 2}
Output: 2
Explanation:
Following are the pairs that satisfy the given criteria:
- (1, 1): The difference between 1+ sumOfDigits(1) and 1+sumOfDigits(1) is 0, thus they are equal.
- (2, 2): The difference between 2+sumOfDigits(2) and 2+sumOfDigits(2) is 0 , thus they are equal.
Therefore, the total number of pairs are 2.
Input: arr[] = {105, 96, 20, 2, 87, 96}
Output: 3
Following are the pairs that satisfy the given criteria:
- (105, 96): The difference between 105+ sumOfDigits(105) and 96+sumOfDigits(96) is 0, thus they are equal.
- (105, 96): The difference between 105+ sumOfDigits(105) and 96+sumOfDigits(96) is 0, thus they are equal.
- (96, 96): The difference between 96+ sumOfDigits(96) and 96+sumOfDigits(96) is 0, thus they are equal.
Input: arr[] = {1, 2, 3, 4}
Output: 0
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs of the given array and count those pairs that satisfy the given criteria. After checking for all the pairs print the total count of pairs obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by storing the sum of elements with its sum of digits in a HashMap and then count the total number of pairs formed accordingly. Follow the steps given below to solve the problem:
- Initialize an unordered_map, M that stores the frequency of the sum of elements with its sum of digits for each array element.
- Traverse the given array and increment the frequency of (arr[i] + sumOfDigits(arr[i])) in the map M.
- Initialize a variable, say count as 0 that stores the total count of resultant pairs.
- Traverse the given map M and if the frequency of any element, say F is greater than or equal to 2, then increment the value of count by (F*(F – 1))/2.
- After completing the above steps, print the value of count as the resultant count of pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDigits( int N)
{
int sum = 0;
while (N) {
sum += (N % 10);
N = N / 10;
}
return sum;
}
int CountPair( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
int val = arr[i] + sumOfDigits(arr[i]);
mp[val]++;
}
int count = 0;
for ( auto x : mp) {
int val = x.first;
int times = x.second;
count += ((times * (times - 1)) / 2);
}
return count;
}
int main()
{
int arr[] = { 105, 96, 20, 2, 87, 96 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << CountPair(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int sumOfDigits( int N)
{
int sum = 0 ;
while (N > 0 ) {
sum += (N % 10 );
N = N / 10 ;
}
return sum;
}
static int CountPair( int arr[], int n)
{
HashMap<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
int val = arr[i] + sumOfDigits(arr[i]);
if (mp.containsKey(val)) {
mp.put(val, mp.get(val) + 1 );
}
else {
mp.put(val, 1 );
}
}
int count = 0 ;
for (Map.Entry<Integer, Integer> x :
mp.entrySet()) {
int val = x.getKey();
int times = x.getValue();
count += ((times * (times - 1 )) / 2 );
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 105 , 96 , 20 , 2 , 87 , 96 };
int N = 6 ;
System.out.println(CountPair(arr, N));
}
}
|
Python3
def sumOfDigits(N):
sum = 0
while (N):
sum + = (N % 10 )
N = N / / 10
return sum
def CountPair(arr, n):
mp = {}
for i in range (n):
val = arr[i] + sumOfDigits(arr[i])
if val in mp:
mp[val] + = 1
else :
mp[val] = 1
count = 0
for key,value in mp.items():
val = key
times = value
count + = ((times * (times - 1 )) / / 2 )
return count
if __name__ = = '__main__' :
arr = [ 105 , 96 , 20 , 2 , 87 , 96 ]
N = len (arr)
print (CountPair(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int sumOfDigits( int N)
{
int sum = 0;
while (N>0) {
sum += (N % 10);
N = N / 10;
}
return sum;
}
static int CountPair( int []arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
int val = arr[i] + sumOfDigits(arr[i]);
if (mp.ContainsKey(val))
mp[val]++;
else
mp.Add(val,1);
}
int count = 0;
foreach (KeyValuePair< int , int > entry in mp) {
int val = entry.Key;
int times = entry.Value;
count += ((times * (times - 1)) / 2);
}
return count;
}
public static void Main()
{
int []arr = { 105, 96, 20, 2, 87, 96 };
int N = arr.Length;
Console.Write(CountPair(arr, N));
}
}
|
Javascript
<script>
function sumOfDigits(N) {
let sum = 0;
while (N != 0) {
sum = sum + (N % 10);
N = Math.floor(N / 10);
}
return sum;
}
function CountPair(arr, n) {
let mp = new Map();
for (let i = 0; i < n; i++) {
let val = arr[i] + sumOfDigits(arr[i]);
if (mp.has(val)) {
mp.set(val, mp.get(val) + 1);
}
else {
mp.set(val, 1);
}
}
let count = 0;
for (let [key, value] of mp) {
count = count + (value * (value - 1)) / 2;
}
return count;
}
let arr = [105, 96, 20, 2, 87, 96];
let N = arr.length;
document.write(CountPair(arr, N))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...