Skip to content

Commit ea8bbb9

Browse files
committed
feat: add solutions to lc problem: No.2176
No.2176.Count Equal and Divisible Pairs in an Array
1 parent 2fe7396 commit ea8bbb9

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,54 @@ func countPairs(nums []int, k int) int {
120120
### **TypeScript**
121121

122122
```ts
123+
function countPairs(nums: number[], k: number): number {
124+
const n = nums.length;
125+
let ans = 0;
126+
for (let i = 0; i < n - 1; i++) {
127+
for (let j = i + 1; j < n; j++) {
128+
if (nums[i] === nums[j] && (i * j) % k === 0) {
129+
ans++;
130+
}
131+
}
132+
}
133+
return ans;
134+
}
135+
```
123136

137+
### **Rust**
138+
139+
```rust
140+
impl Solution {
141+
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
142+
let k = k as usize;
143+
let n = nums.len();
144+
let mut ans = 0;
145+
for i in 0..n - 1 {
146+
for j in i + 1..n {
147+
if nums[i] == nums[j] && i * j % k == 0 {
148+
ans += 1;
149+
}
150+
}
151+
}
152+
ans
153+
}
154+
}
155+
```
156+
157+
### **C**
158+
159+
```c
160+
int countPairs(int *nums, int numsSize, int k) {
161+
int ans = 0;
162+
for (int i = 0; i < numsSize - 1; i++) {
163+
for (int j = i + 1; j < numsSize; j++) {
164+
if (nums[i] == nums[j] && i * j % k == 0) {
165+
ans++;
166+
}
167+
}
168+
}
169+
return ans;
170+
}
124171
```
125172
126173
### **...**

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,54 @@ func countPairs(nums []int, k int) int {
110110
### **TypeScript**
111111

112112
```ts
113+
function countPairs(nums: number[], k: number): number {
114+
const n = nums.length;
115+
let ans = 0;
116+
for (let i = 0; i < n - 1; i++) {
117+
for (let j = i + 1; j < n; j++) {
118+
if (nums[i] === nums[j] && (i * j) % k === 0) {
119+
ans++;
120+
}
121+
}
122+
}
123+
return ans;
124+
}
125+
```
113126

127+
### **Rust**
128+
129+
```rust
130+
impl Solution {
131+
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
132+
let k = k as usize;
133+
let n = nums.len();
134+
let mut ans = 0;
135+
for i in 0..n - 1 {
136+
for j in i + 1..n {
137+
if nums[i] == nums[j] && i * j % k == 0 {
138+
ans += 1;
139+
}
140+
}
141+
}
142+
ans
143+
}
144+
}
145+
```
146+
147+
### **C**
148+
149+
```c
150+
int countPairs(int *nums, int numsSize, int k) {
151+
int ans = 0;
152+
for (int i = 0; i < numsSize - 1; i++) {
153+
for (int j = i + 1; j < numsSize; j++) {
154+
if (nums[i] == nums[j] && i * j % k == 0) {
155+
ans++;
156+
}
157+
}
158+
}
159+
return ans;
160+
}
114161
```
115162
116163
### **...**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int countPairs(int *nums, int numsSize, int k) {
2+
int ans = 0;
3+
for (int i = 0; i < numsSize - 1; i++) {
4+
for (int j = i + 1; j < numsSize; j++) {
5+
if (nums[i] == nums[j] && i * j % k == 0) {
6+
ans++;
7+
}
8+
}
9+
}
10+
return ans;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
3+
let k = k as usize;
4+
let n = nums.len();
5+
let mut ans = 0;
6+
for i in 0..n - 1 {
7+
for j in i + 1..n {
8+
if nums[i] == nums[j] && i * j % k == 0 {
9+
ans += 1;
10+
}
11+
}
12+
}
13+
ans
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function countPairs(nums: number[], k: number): number {
2+
const n = nums.length;
3+
let ans = 0;
4+
for (let i = 0; i < n - 1; i++) {
5+
for (let j = i + 1; j < n; j++) {
6+
if (nums[i] === nums[j] && (i * j) % k === 0) {
7+
ans++;
8+
}
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)