返回介绍

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

发布于 2024-06-17 01:03:09 字数 4063 浏览 0 评论 0 收藏 0

2176. Count Equal and Divisible Pairs in an Array

中文文档

Description

Given a 0-indexed integer array nums of length n and an integer k, return _the number of pairs_ (i, j) _where_ 0 <= i < j < n, _such that_ nums[i] == nums[j] _and_ (i \* j) _is divisible by_ k.

 

Example 1:

Input: nums = [3,1,2,2,2,1,3], k = 2
Output: 4
Explanation:
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.

Example 2:

Input: nums = [1,2,3,4], k = 1
Output: 0
Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i], k <= 100

Solutions

Solution 1

class Solution:
  def countPairs(self, nums: List[int], k: int) -> int:
    n = len(nums)
    return sum(
      nums[i] == nums[j] and (i * j) % k == 0
      for i in range(n)
      for j in range(i + 1, n)
    )
class Solution {
  public int countPairs(int[] nums, int k) {
    int n = nums.length;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (nums[i] == nums[j] && (i * j) % k == 0) {
          ++ans;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countPairs(vector<int>& nums, int k) {
    int n = nums.size();
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
      }
    }
    return ans;
  }
};
func countPairs(nums []int, k int) int {
  n := len(nums)
  ans := 0
  for i, v := range nums {
    for j := i + 1; j < n; j++ {
      if v == nums[j] && (i*j)%k == 0 {
        ans++
      }
    }
  }
  return ans
}
function countPairs(nums: number[], k: number): number {
  const n = nums.length;
  let ans = 0;
  for (let i = 0; i < n - 1; i++) {
    for (let j = i + 1; j < n; j++) {
      if (nums[i] === nums[j] && (i * j) % k === 0) {
        ans++;
      }
    }
  }
  return ans;
}
impl Solution {
  pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
    let k = k as usize;
    let n = nums.len();
    let mut ans = 0;
    for i in 0..n - 1 {
      for j in i + 1..n {
        if nums[i] == nums[j] && (i * j) % k == 0 {
          ans += 1;
        }
      }
    }
    ans
  }
}
int countPairs(int* nums, int numsSize, int k) {
  int ans = 0;
  for (int i = 0; i < numsSize - 1; i++) {
    for (int j = i + 1; j < numsSize; j++) {
      if (nums[i] == nums[j] && i * j % k == 0) {
        ans++;
      }
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文