2748. Number of Beautiful Pairs Easy
1/**
2 * [2748] Number of Beautiful Pairs
3 *
4 * You are given a 0-indexed integer array nums. A pair of indices i, j where 0 <= i < j < nums.length is called beautiful if the first digit of nums[i] and the last digit of nums[j] are coprime.
5 * Return the total number of beautiful pairs in nums.
6 * Two integers x and y are coprime if there is no integer greater than 1 that divides both of them. In other words, x and y are coprime if gcd(x, y) == 1, where gcd(x, y) is the greatest common divisor of x and y.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [2,5,1,4]
11 * Output: 5
12 * Explanation: There are 5 beautiful pairs in nums:
13 * When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1.
14 * When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1.
15 * When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1.
16 * When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1.
17 * When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1.
18 * Thus, we return 5.
19 *
20 * <strong class="example">Example 2:
21 *
22 * Input: nums = [11,21,12]
23 * Output: 2
24 * Explanation: There are 2 beautiful pairs:
25 * When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1.
26 * When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1.
27 * Thus, we return 2.
28 *
29 *
30 * Constraints:
31 *
32 * 2 <= nums.length <= 100
33 * 1 <= nums[i] <= 9999
34 * nums[i] % 10 != 0
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/number-of-beautiful-pairs/
40// discuss: https://leetcode.com/problems/number-of-beautiful-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn count_beautiful_pairs(nums: Vec<i32>) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2748() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.