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

  • SEARCH
  • Home
  • 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 9123597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:22:45+00:00 2026-06-17T06:22:45+00:00

Given a number n, write a function that returns count of numbers from 1

  • 0

Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation

What can be the most optimal way of solving this problem.

the approach i am using in naive i.e nlogn (easy to guess the approach by seeing complexity 🙂 )

  • 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-06-17T06:22:46+00:00Added an answer on June 17, 2026 at 6:22 am

    The following algorithm computes the number of integers from 0 to (n-1) without “3” in their decimal representation quite efficiently. (I have modified the interval from 1 .. n to 0 .. n-1 only to simplify the following calculations slightly.)

    (I am not an expert in complexity calculations, but I think the complexity of this algorithm is O(log n), because it does a fixed number of steps for each digit of n.)

    The first observation is that the number of integers with at most d digits (i.e. the numbers in the interval 0 .. 10d-1) not having the digit 3 in their decimal representation is exactly 9d, because for each digit you have 9 possible choices 0,1,2,4,5,6,7,8,9.

    Now let me demonstrate the algorithm with a 5 digit number n = a4a3a2a1a0.

    We compute separately the number of integers with no “3” in their decimal representation for the intervals

    • I0: a4a3a2a1 0 <= i < a4a3a2a1a0
    • I1: a4a3a2 0 0 <= i < a4a3a2a1 0
    • I2: a4a3 0 0 0 <= i < a4a3a2 0 0
    • I3: a4 0 0 0 0 <= i < a4a3 0 0 0
    • I4: 0 0 0 0 0 <= i < a4 0 0 0 0

    The number of integers in the interval Ij that do not have a “3” in the decimal representation is

    • 0, if one of the higher valued digits aj+1, aj+2, … is equal to 3,
      otherwise:
    • aj * 9j, if 0 <= aj <= 3, (aj choices for the
      jth digit, 9 choices for all lower valued digits),
    • (aj – 1) * 9j, if aj > 3 (because 3 is not a valid choice for the jth digit) .

    So we have the following function:

    /*
     * Compute number of integers x with 0 <= x < n that do not
     * have a 3 in their decimal representation.
     */
    int f(int n)
    {
        int count = 0;
        int a;      // The current digit a_j
        int p = 1;  // The current value of 9^j
    
        while (n > 0) {
            a = n % 10;
            if (a == 3) {
                count = 0;
            }
            if (a <= 3) {
                count += a * p;
            } else {
                count += (a-1) * p;
            }
            n /= 10;
            p *= 9;
        }
    
        return count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an unsorted array of numbers, write a function that returns true if array
I'm attempting to write a function that recursively computes the resulting fibonacci number from
How do I write a function that returns FALSE if a given string is
So I wrote this function that is given possible numbers, and it has to
How to write multithread windows application that runs a program in given number of
I'm trying to write a function that takes two string arguments and returns the
I have to write a function that computes and returns the maximum benefit we
In Scala language, I want to write a function that yields odd numbers within
I'm trying to write a Python function that, given the path to a document
Given an array of unsorted positive ints, write a function that finds runs of

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.