Skip to content

Commit 9d268cd

Browse files
committed
New Problem Solution - "1814. Count Nice Pairs in an Array"
1 parent 1ab937e commit 9d268cd

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
1414
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
1515
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [C++](./algorithms/cpp/truncateSentence/TruncateSentence.cpp)|Easy|
16+
|1814|[Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | [C++](./algorithms/cpp/countNicePairsInAnArray/CountNicePairsInAnArray.cpp)|Medium|
1617
|1813|[Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/) | [C++](./algorithms/cpp/sentenceSimilarity/SentenceSimilarity.III.cpp)|Medium|
1718
|1812|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/) | [C++](./algorithms/cpp/determineColorOfAChessboardSquare/DetermineColorOfAChessboardSquare.cpp)|Easy|
1819
|1808|[Maximize Number of Nice Divisors](https://leetcode.com/problems/maximize-number-of-nice-divisors/) | [C++](./algorithms/cpp/maximizeNumberOfNiceDivisors/MaximizeNumberOfNiceDivisors.cpp)|Hard|
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Source : https://leetcode.com/problems/count-nice-pairs-in-an-array/
2+
// Author : Hao Chen
3+
// Date : 2021-04-06
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given an array nums that consists of non-negative integers. Let us define rev(x) as the
8+
* reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of
9+
* indices (i, j) is nice if it satisfies all of the following conditions:
10+
*
11+
* 0 <= i < j < nums.length
12+
* nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
13+
*
14+
* Return the number of nice pairs of indices. Since that number can be too large, return it modulo
15+
* 10^9 + 7.
16+
*
17+
* Example 1:
18+
*
19+
* Input: nums = [42,11,1,97]
20+
* Output: 2
21+
* Explanation: The two pairs are:
22+
* - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.
23+
* - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
24+
*
25+
* Example 2:
26+
*
27+
* Input: nums = [13,10,35,24,76]
28+
* Output: 4
29+
*
30+
* Constraints:
31+
*
32+
* 1 <= nums.length <= 10^5
33+
* 0 <= nums[i] <= 10^9
34+
******************************************************************************************************/
35+
36+
class Solution {
37+
private:
38+
int rev(int n) {
39+
int x = 0;
40+
while(n > 0) {
41+
x = x*10 + (n % 10);
42+
n /= 10;
43+
}
44+
return x;
45+
}
46+
47+
public:
48+
int countNicePairs(vector<int>& nums) {
49+
return countNicePairs02(nums);
50+
return countNicePairs01(nums);
51+
}
52+
int countNicePairs01(vector<int>& nums) {
53+
// suppose n' = rev(n)
54+
// define: a + b' == b + a'
55+
// then: a - a' == b - b'
56+
57+
unordered_map<int, int> stat;
58+
for(auto& n : nums) {
59+
stat[n-rev(n)]++;
60+
61+
}
62+
63+
//if there are n elements has same value,
64+
// then there are n*(n-1)/2 unique pairs.
65+
int result = 0;
66+
for(auto& [n, cnt] : stat) {
67+
result = (result + cnt * (cnt -1l) / 2) % 1000000007;
68+
}
69+
return result;
70+
}
71+
72+
int countNicePairs02(vector<int>& nums) {
73+
// suppose n' = rev(n)
74+
// define: a + b' == b + a'
75+
// then: a - a' == b - b'
76+
int result = 0;
77+
unordered_map<int, int> stat;
78+
for(auto& n : nums) {
79+
int delta = n-rev(n);
80+
stat[delta]++;
81+
result = (result + (stat[delta] - 1l)) % 1000000007 ;
82+
}
83+
84+
return result ;
85+
}
86+
};

0 commit comments

Comments
 (0)