You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).Return the total number of good pairs. Example 1:Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1Output: 5Explanation:The 5 good pairs are (0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).Example 2:Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3Output: 2Explanation:The 2 good pairs are (3, 0) and (3, 1). Constraints:1 <= n, m <= 1051 <= nums1[i], nums2[j] <= 1061 <= k <= 103C++ 1class Solution {2public:3 long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {4 5 }6};
Question
You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).Return the total number of good pairs. Example 1:Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1Output: 5Explanation:The 5 good pairs are (0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).Example 2:Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3Output: 2Explanation:The 2 good pairs are (3, 0) and (3, 1). Constraints:1 <= n, m <= 1051 <= nums1[i], nums2[j] <= 1061 <= k <= 103C++ 1class Solution {2public:3 long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {4 5 }6};
Solution
class Solution {
public:
long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {
long long count = 0;
int n = nums1.size();
int m = nums2.size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (nums1[i] % (nums2[j] * k) == 0) {
count++;
}
}
}
return count;
}
};
Similar Questions
You are given two 0-indexed integer arrays nums1 and nums2 of sizes n and m, respectively.Consider calculating the following values:The number of indices i such that 0 <= i < n and nums1[i] occurs at least once in nums2.The number of indices i such that 0 <= i < m and nums2[i] occurs at least once in nums1.Return an integer array answer of size 2 containing the two values in the above order.
ou are given an array of integers of length n. Determine the number of pairs of adjacent elements whose sum is divisible by a given integer k. If such pairs exist, output the count of pairs; otherwise, indicate that no pairs were found.Example 1Input:3 // number of elements (n)10 2 30 // elements2 // kOutput: Number of pairs: 2Explanation: In the given array [10, 2, 30], the pairs with sums divisible by 2 are (10, 2) and (2, 30). Hence, the output is 2, indicating the number of such pairs found.Input format :The first line consists of an integer n, representing the size of the array.The second line consists of n space-separated integers, representing the elements of the array.The third line consists of an integer k, representing the divisor.Output format :If there exist pairs of adjacent elements in the array whose sum is divisible by k, output "Number of pairs: " followed by the count of such pairs.If no such pairs are found, output "No pairs found".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:3 ≤ n ≤ 251 ≤ Each element ≤ 1002 ≤ k ≤ 25Sample test cases :Input 1 :310 2 302Output 1 :Number of pairs: 2Input 2 :610 20 30 40 50 6010
You are given an integer array nums of size n and a positive integer k.Divide the array into one or more arrays of size 3 satisfying the following conditions:Each element of nums should be in exactly one array.The difference between any two elements in one array is less than or equal to k.Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
You are given 2 arrays A and B of same size N. A pair is said to be a valid if and only if i < j, A[i] == B[j] and A[j] == B[i]. You have to count total number of valid pairs.Input FormatFirst line of input contains T - number of test cases. First line of each test case contains N - size of the array. The second and third line of each test case contains N integers - elements of the array A and B.Output FormatFor each test case, print the count of number of valid pairs, separated by newline.Constraints30 points1 <= N <= 10370 points1 <= N <= 105General Constraints1 <= T <= 1000 <= A[i], B[i] <= 109ExampleInput231 2 33 2 151 3 7 9 93 1 9 7 7Output13ExplanationTest Case 1:There is only one valid pair - (0, 2). This is because A[0] == B[2] and A[2] == B[0].Test Case 2:There are 3 valid pairs - (0, 1), (2, 3), (2, 4). For example (0, 1) is valid because A[0] == B[1] and A[1] == B[0].
Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.