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.
Return the total number of beautiful pairs innums.
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.
Input: nums =[2,5,1,4]Output: 5Explanation: There are 5 beautiful pairs in nums:When i =0 and j =1: the first digit of nums[0]is2, and the last digit of nums[1]is5. We can confirm that 2 and 5 are coprime, since gcd(2,5)==1.When i =0 and j =2: the first digit of nums[0]is2, and the last digit of nums[2]is1. Indeed, gcd(2,1)==1.When i =1 and j =2: the first digit of nums[1]is5, and the last digit of nums[2]is1. Indeed, gcd(5,1)==1.When i =1 and j =3: the first digit of nums[1]is5, and the last digit of nums[3]is4. Indeed, gcd(5,4)==1.When i =2 and j =3: the first digit of nums[2]is1, and the last digit of nums[3]is4. Indeed, gcd(1,4)==1.Thus, we return5.
Input: nums =[11,21,12]Output: 2Explanation: There are 2 beautiful pairs:When i =0 and j =1: the first digit of nums[0]is1, and the last digit of nums[1]is1. Indeed, gcd(1,1)==1.When i =0 and j =2: the first digit of nums[0]is1, and the last digit of nums[2]is2. Indeed, gcd(1,2)==1.Thus, we return2.
#include<vector>usingnamespace std;
classSolution {
public:int beautifulPairs(vector<int>& nums) {
int n = nums.size(), ans =0;
auto first = [](int x) { while (x >=10) x /=10; return x; };
auto last = [](int x) { return x %10; };
auto gcd = [](int a, int b) { while (b) { int t = b; b = a % b; a = t; } return a; };
for (int i =0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (gcd(first(nums[i]), last(nums[j])) ==1) ++ans;
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcbeautifulPairs(nums []int) int {
n:= len(nums); ans:=0first:=func(xint) int { forx>=10 { x/=10 }; returnx }
last:=func(xint) int { returnx%10 }
gcd:=func(a, bint) int { forb!=0 { a, b = b, a%b }; returna }
fori:=0; i < n; i++ {
forj:=i+1; j < n; j++ {
ifgcd(first(nums[i]), last(nums[j])) ==1 { ans++ }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
publicintbeautifulPairs(int[] nums) {
int n = nums.length, ans = 0;
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (gcd(first(nums[i]), last(nums[j])) == 1) ans++;
return ans;
}
intfirst(int x) { while (x >= 10) x /= 10; return x; }
intlast(int x) { return x % 10; }
intgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funbeautifulPairs(nums: IntArray): Int {
funfirst(x: Int) = generateSequence(x) { it/10 }.first { it < 10 }
funlast(x: Int) = x % 10fungcd(a: Int, b: Int): Int = if (b ==0) a else gcd(b, a % b)
var ans = 0for (i in nums.indices)
for (j in i+1 until nums.size)
if (gcd(first(nums[i]), last(nums[j])) ==1) ans++return ans
}
}
1
2
3
4
5
6
7
8
from math import gcd
classSolution:
defbeautifulPairs(self, nums: List[int]) -> int:
deffirst(x):
while x >=10: x //=10return x
deflast(x): return x %10return sum(gcd(first(nums[i]), last(nums[j])) ==1for i in range(len(nums)) for j in range(i+1, len(nums)))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnbeautiful_pairs(nums: Vec<i32>) -> i32 {
fnfirst(x: i32) -> i32 { letmut x = x; while x >=10 { x /=10; } x }
fnlast(x: i32) -> i32 { x %10 }
fngcd(a: i32, b: i32) -> i32 { if b ==0 { a } else { gcd(b, a % b) } }
let n = nums.len(); letmut ans =0;
for i in0..n {
for j in i+1..n {
if gcd(first(nums[i]), last(nums[j])) ==1 { ans +=1; }
}
}
ans
}
}