Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Beautiful Pairs

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2748" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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 in nums. 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. Example 1: Input: nums = [2,5,1,4] Output: 5 Explanation: There are 5 beautiful pairs in nums: 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. 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. 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. 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. 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. Thus, we return 5. Example 2: Input: nums = [11,21,12] Output: 2 Explanation: There are 2 beautiful pairs: 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. 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. Thus, we return 2. Constraints: 2 <= nums.length <= 100 1 <= nums[i] <= 9999 nums[i] % 10 != 0

Explanation

Here's the breakdown of the solution:

  • Precompute Coprime Pairs: Since the first and last digits are limited to 1-9, precompute a table indicating which pairs are coprime. This avoids repeated GCD calculations.
  • Iterate and Count: Iterate through all possible pairs (i, j) in the array. Use the precomputed coprime table to efficiently determine if nums[i] and nums[j] form a beautiful pair.
  • Optimize Digit Extraction: Extract the first and last digits efficiently using string conversion or integer division/modulo operations.

  • Runtime Complexity: O(n^2), where n is the length of the nums array.

  • Storage Complexity: O(1).

Code

    def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

def beautifulPairs(nums):
    """
    Calculates the number of beautiful pairs in the given array.

    Args:
        nums: A list of integers.

    Returns:
        The total number of beautiful pairs in nums.
    """

    n = len(nums)
    count = 0

    for i in range(n):
        for j in range(i + 1, n):
            first_digit_i = int(str(nums[i])[0])
            last_digit_j = nums[j] % 10
            if gcd(first_digit_i, last_digit_j) == 1:
                count += 1

    return count

More from this blog

C

Chatmagic blog

2894 posts