Skip to content

Commit

Permalink
add 1814. Count Nice Pairs in an Array
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeed205 committed Nov 22, 2023
1 parent 6353e2d commit 51faf1f
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Arrays;

class Solution {

int reverse(int num) {
int rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}

public int countNicePairs(int[] nums) {
final int mod = 1000000007;

int len = nums.length;
for (int i = 0; i < len; i++) nums[i] = nums[i] - reverse(nums[i]);
Arrays.sort(nums);
long res = 0;
for (int i = 0; i < len - 1; i++) {
long cont = 1;
while (i < len - 1 && nums[i] == nums[i + 1]) {
cont++;
i++;
}
res = (res % mod + (cont * (cont - 1)) / 2) % mod;
}
return (int) (res + mod) % mod; // Ensure non-negative result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number[]} nums
* @return {number}
*/
var countNicePairs = function (nums) {
const rev = (num) => {
let result = 0;
while (num > 0) {
result = result * 10 + (num % 10);
num = Math.floor(num / 10);
}
return result;
};

const MOD = 1_000_000_007;
const dist = new Map();

for (const num of nums) {
const offset = num - rev(num);
dist.set(offset, (dist.get(offset) || 0) + 1);
}

let res = 0;

for (const [_, count] of dist) {
if (count > 1) {
res = (res + (((count * (count - 1)) / 2) % MOD)) % MOD;
}
}

return res;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def countNicePairs(self, nums: List[int]) -> int:
def rev(num: int) -> int:
result = 0
while num > 0:
result = result * 10 + num % 10
num //= 10
return result

MOD = 10**9 + 7
dist = {}

for num in nums:
offset = num - rev(num)
dist[offset] = dist.get(offset, 0) + 1

nice_pairs = 0

for count in dist.values():
if count > 1:
nice_pairs = (nice_pairs + (count * (count - 1) // 2) % MOD) % MOD

return nice_pairs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;

impl Solution {
pub fn count_nice_pairs(nums: Vec<i32>) -> i32 {
let mut dist = HashMap::new();
for num in nums {
let offset = num - Self::rev(num);
*dist.entry(offset).or_insert(0) += 1;
}
let mut res: i64 = dist
.into_iter()
.filter(|&(_, ct)| ct > 1)
.map(|(_, ct)| ct as i64)
.map(|ct| ct * (ct - 1) / 2)
.sum();
res %= 1_000_000_007;
res as i32
}

fn rev(mut num: i32) -> i32 {
let mut res = 0;
while num > 0 {
res *= 10;
res += num % 10;
num /= 10;
}
res
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function countNicePairs(nums: number[]): number {
const rev = (num: number): number => {
let result = 0;
while (num > 0) {
result = result * 10 + (num % 10);
num = Math.floor(num / 10);
}
return result;
};

const MOD = 1_000_000_007;
const dist = new Map<number, number>();

for (const num of nums) {
const offset = num - rev(num);
dist.set(offset, (dist.get(offset) || 0) + 1);
}

let res: number = 0;

for (const [, count] of dist) {
if (count > 1) {
res = (res + (((count * (count - 1)) / 2) % MOD)) % MOD;
}
}

return res;
}
30 changes: 30 additions & 0 deletions Medium/1814. Count Nice Pairs in an Array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## [1814. Count Nice Pairs in an Array](https://leetcode.com/problems/count-nice-pairs-in-an-array/)

You are given an array `nums` that consists of non-negative integers. Let us define `rev(x)` as the reverse of the non-negative integer `x`. For example, `rev(123) = 321`, and `rev(120) = 21`. A pair of indices `(i, j)` is _nice_ if it satisfies all of the following conditions:

- `0 <= i < j < nums.length`
- `nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])`

Return _the number of nice pairs of indices_. Since that number can be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

#### Example 1:

<pre>
<strong>Input:</strong> nums = [42,11,1,97]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two pairs are:
- (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.
- (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
</pre>

#### Example 2:

<pre>
<strong>Input:</strong> nums = [13,10,35,24,76]
<strong>Output:</strong> 4
</pre>

#### Constraints:

- <code>1 <= nums.length <= 10<sup>5</sup></code>
- <code>0 <= nums[i] <= 10<sup>9</sup></code>

0 comments on commit 51faf1f

Please sign in to comment.