Count pairs with special numbers
Last Updated :
14 Sep, 2023
Given an array arr[] of size N. You need to find the total number of good pairs such that the if you chose two numbers arr[i] and arr[j] then the following condition holds arr[i] + SpecialNumber(arr[j]) = = arr[j] + SpecialNumber(arr[i]). Here Special Number indicates the number after removing the digits at the even position of the given number like if there is a number 1234 then the SpecialNumber(1234) will be 13 which comes after removing 2 and 4 from even positions.
Examples:
Input:- N = 3, arr[] = [4, 5, 6]
Output:- 3
Explanation: There is no number whose length is two so numbers remain the same after converting them to special numbers and 4 + SpecialNumber(6) == 6 + SpecialNumber(4), similarly with (4, 5) and with (5, 6).
Input: N = 4, arr[] = [12, 13, 12, 20]
Output: 1
Explanation: only one pair occurs which is (12, 12).
Approach: This can be solved with the following idea:
- As we have to find such pairs which hold the condition arr[i] + SpecialNumber(arr[j]) == arr[j] + SpecialNumber(arr[i]).
- Let’s make this simple by changing the equation to arr[i] – SpecialNumber(arr[i]) == arr[j] – SpecialNumber(arr[j]).
- Now we just have to find out the numbers whose difference with their special number is equal.
- After finding the count of such a number we can simply apply the formula to find the total pairs which is, let’s say that there are n numbers with the same difference of their value and their special number. Then the total pairs we can form by use of them are (n-1)*(n)/2.
- We will find the total pairs in linear time by using this formula.
Below are the steps involved in the implementation of the code:
- For finding the difference between the number and its special number first of all we need to find the special number for every number.
- After this, we will find the difference between each number with its special number and store it in a unordered_map.
- After this, we will just traverse the map and count the pairs which we can form for a particular value.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
int SpecialNumber( int n)
{
string num = to_string(n);
int ans = 0;
for ( int i = 0; i < num.length(); i++) {
if (i % 2 == 0) {
ans = ans * 10 + (num[i] - '0' );
}
}
return ans;
}
int CountPairs( int n, vector< int > arr)
{
int ans = 0;
unordered_map< int , int > mm;
for ( int i = 0; i < n; i++) {
int temp = SpecialNumber(arr[i]);
mm[arr[i] - temp]++;
}
for ( auto x : mm) {
int temp = x.second;
ans += (temp * (temp - 1)) / 2;
}
return ans;
}
int main()
{
int N = 3;
vector< int > arr = { 4, 5, 6 };
cout << CountPairs(N, arr);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
class GFG {
static int specialNumber( int n) {
String num = Integer.toString(n);
int ans = 0 ;
for ( int i = 0 ; i < num.length(); i++) {
if (i % 2 == 0 ) {
ans = ans * 10 + (num.charAt(i) - '0' );
}
}
return ans;
}
static int countPairs( int n, Vector<Integer> arr) {
int ans = 0 ;
Map<Integer, Integer> mm = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
int temp = specialNumber(arr.get(i));
mm.put(arr.get(i) - temp, mm.getOrDefault(arr.get(i) - temp, 0 ) + 1 );
}
for (Map.Entry<Integer, Integer> entry : mm.entrySet()) {
int temp = entry.getValue();
ans += (temp * (temp - 1 )) / 2 ;
}
return ans;
}
public static void main(String[] args) {
int N = 3 ;
Vector<Integer> arr = new Vector<>( 3 );
arr.add( 4 );
arr.add( 5 );
arr.add( 6 );
System.out.println(countPairs(N, arr));
}
}
|
Python
def special_number(n):
num = str (n)
ans = 0
for i in range ( len (num)):
if i % 2 = = 0 :
ans = ans * 10 + int (num[i])
return ans
def count_pairs(n, arr):
ans = 0
mm = {}
for i in range (n):
temp = special_number(arr[i])
mm[arr[i] - temp] = mm.get(arr[i] - temp, 0 ) + 1
for value in mm.values():
temp = value
ans + = (temp * (temp - 1 )) / / 2
return ans
if __name__ = = "__main__" :
N = 3
arr = [ 4 , 5 , 6 ]
print (count_pairs(N, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int SpecialNumber( int n)
{
string num = n.ToString();
int ans = 0;
for ( int i = 0; i < num.Length; i++)
{
if (i % 2 == 0)
{
ans = ans * 10 + (num[i] - '0' );
}
}
return ans;
}
static int CountPairs( int n, List< int > arr)
{
int ans = 0;
Dictionary< int , int > mm = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
int temp = SpecialNumber(arr[i]);
mm[arr[i] - temp] = mm.GetValueOrDefault(arr[i] - temp, 0) + 1;
}
foreach ( var entry in mm)
{
int temp = entry.Value;
ans += (temp * (temp - 1)) / 2;
}
return ans;
}
public static void Main( string [] args)
{
int N = 3;
List< int > arr = new List< int > { 4, 5, 6 };
Console.WriteLine(CountPairs(N, arr));
}
}
|
Javascript
function SpecialNumber(n) {
let num = n.toString();
let ans = 0;
for (let i = 0; i < num.length; i++) {
if (i % 2 === 0) {
ans = ans * 10 + parseInt(num[i]);
}
}
return ans;
}
function CountPairs(n, arr) {
let ans = 0;
let mm = new Map();
for (let i = 0; i < n; i++) {
let temp = SpecialNumber(arr[i]);
if (mm.has(arr[i] - temp)) {
mm.set(arr[i] - temp, mm.get(arr[i] - temp) + 1);
} else {
mm.set(arr[i] - temp, 1);
}
}
for (let [key, value] of mm) {
/ Taking frequency of a
let temp = value;
ans += (temp * (temp - 1)) / 2;
}
return ans;
}
let N = 3;
let arr = [4, 5, 6];
console.log(CountPairs(N, arr));
|
Time Complexity: O(N*X)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...