Count the Number of Ones Problem Statement

Given an integer N, calculate the total number of times the digit '1' appears in each number from 0 to N inclusive.

Input:

T (number of test cases)
Each test case contains an integer N

Output:

For each test case, return the total count of digit '1' from 0 to N.

Example:

Input:
2
5
13
Output:
1
6
Explanation:

For N = 5, the numbers are 0, 1, 2, 3, 4, 5 and there is 1 occurrence of '1'.
For N = 13, the numbers are 0 to 13, and there are 6 occurrences of '1'.

Constraints:

  • 1 ≤ T ≤ 11
  • 1 ≤ N ≤ 5000
  • Time Limit: 1 sec
Note:
You do not need to print anything; implement the provided function to return the answers.
AnswerBot
1y

The task is to count the number of occurrences of the digit '1' in each number from 0 to N.

  • Iterate through each number from 0 to N

  • Convert each number to a string

  • Count the number of occurrences of the ...read more

Help your peers!
Select
Add answer anonymously...
Novartis Full Stack Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits