878. Nth Magical Number LeetCode Solution | Easy Approach

Minimum Cost to Merge Stones
Share:

Nth Magical Number A positive integer is magical if it is divisible by either a or b.

Given the three integers na, and b, return the nth magical number. Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: n = 1, a = 2, b = 3
Output: 2

Example 2:

Input: n = 4, a = 2, b = 3
Output: 6

Constraints:

  • 1 <= n <= 109
  • 2 <= a, b <= 4 * 104

Nth Magical Number Solutions

Time: O(n)
Space: O(n)

C++

 Will be updated Soon

Java

 
 Will be updated Soon

Python

  Will be updated Soon

Watch Tutorial

Checkout more Solutions here

Leave a Comment

Your email address will not be published. Required fields are marked *

x