2176. Count Equal and Divisible Pairs in an Array (leet code)

임병욱·2023년 11월 21일

algorithm

목록 보기
7/31

문제 설명

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.

제한 사항

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

입출력 예

내 풀이

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var countPairs = function(nums, k) {
    let answer = 0;

    for (let i = 0; i < nums.length - 1; i++) {
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[i] === nums[j] && (i * j) % k === 0) answer++
        }
    }
    
    return answer
};

회고

너무 쉬웠당

profile
안녕하세여

0개의 댓글