Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Smallest and Second-Smallest Distinct Digits

Basic Acceptance 91.98% Points 10.00

You are given a non-negative integer n. Your task is to return its smallest and second-smallest distinct digits.

The digits must be distinct, meaning repeated occurrences of the same digit count only once. Return the two smallest different digits in ascending order.

Examples
Example 1

Input: n = 3852
Output: smallest = 2, second = 3
Explanation: The two smallest distinct digits are 2 and 3.

Example 2

Input: n = 91019
Output: smallest = 0, second = 1
Explanation: The distinct digits are 0, 1, and 9, so the two smallest are 0 and 1.

Example 3

Input: n = 4321
Output: smallest = 1, second = 2
Explanation: The two smallest distinct digits are 1 and 2.

Constraints
  • 0 = n = 10¹8
  • The number contains at least two distinct digits.
Companies
Samsung ServiceNow ZScaler
Topics
Data Structures Basics Searching Algorithms
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions