Leetcode Problem 2748. Number of Beautiful Pairs

2748. Number of Beautiful Pairs

Leetcode Solutions

Iterative Pairwise Comparison

  1. Initialize a counter count to 0.
  2. Loop through the array with two nested loops, with the outer loop index i ranging from 0 to len(nums) - 1 and the inner loop index j ranging from i + 1 to len(nums).
  3. For each i, find the first digit by dividing nums[i] by 10 until it is less than 10.
  4. For each j, find the last digit by taking nums[j] % 10.
  5. Check if the first digit and last digit are coprime by calculating the gcd and checking if it is equal to 1.
  6. If they are coprime, increment count by 1.
  7. After the loops, return the value of count.
UML Thumbnail

Precompute Coprime Digits and Use Counting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...