Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:28:19+00:00 2026-05-23T15:28:19+00:00

This problem is really confusing me; we’re given two integers A , B ,

  • 0

This problem is really confusing me; we’re given two integers A, B, we want to count occurrences of digits in the range [A, B]. I though that if we could count the number of digit occurrences in the range [0, A] and [0, B], then the rest is trivial. So how can I count digit occurrences in a range [0, x]?
This isn’t homework, this is actually a problem from SPOJ.
The naive approach won’t work, as A and B can be as large as 10 ^ 9. Here are some examples:

Input:

1 10

Output:

1 2 1 1 1 1 1 1 1 1

Input:

44 497

Output:

85 185 185 185 190 96 96 96 95 93

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:28:19+00:00Added an answer on May 23, 2026 at 3:28 pm

    I would try the brute force approach first to get something working. Look at each number, iterate through each character in the string representation of that number, etc.

    However, there is an easier way.

    • In the interval [0,9], 3 appears 1 time
    • In the interval [0,99], 3 appears 10 times in the first digit and 10 times in the second digit
    • In the interval [0,999], 3 appears 100 times in the first digit, 100 times in the second digit and 100 times in the third digit.

    You can generalize this and with a bit of effort come up with a formula for how many of a certain digit (0 being a possible special case) will appear in a certain range. You don’t need to actually go through each number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really suprised I haven't run into this problem before, but it seems that
Ok I have this problem that I've never had before, it's really bugging me.
This is a really weird problem that I have been having. When I download
This is a really strange problem, that appears to be somewhat intermittent (although it
I have a really confusing problem. I have a PHP script that polls another
Pardon me for giving you a confusing title on this problem. I'm really confused
I would really appreciate if you could shed light on this problem. I have
I've been fighting this problem for many hours now and could really use some
I know I've had this problem before so I'm really frustrated. I've got the
Sorry for the vague title, as I really can't explain this problem succinctly. Basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.