2831. Number of Beautiful Pairs
My accepted Python solution to LeetCode problem 2831, Number of Beautiful Pairs, running in 219ms.
- Difficulty: Easy
- Python
- Runtime 219ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 219ms, memory 17.5MB, accepted 2025-12-31.
class Solution:
def countBeautifulPairs(self, nums: List[int]) -> int:
from math import gcd
count = 0
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
first_digit = int(str(nums[i])[0])
last_digit = nums[j] % 10
if gcd(first_digit, last_digit) == 1:
count += 1
return count