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 6345165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:46:07+00:00 2026-05-24T20:46:07+00:00

I came across this question in an interview. Any number with 3 in its

  • 0

I came across this question in an interview.
Any number with 3 in its units position has at least one multiple containing all ones. For instance, a multiple of 3 is 111, a multiple of 13 is 111111. Given a number ending in 3, I was asked the best method to find its multiple containing all 1’s.
Now a straightforward approach is possible, where you do not consider space issues but as the number grows, and sometimes even if it doesn’t, an int (or a long int at that!) in C cannot hold that multiple.
What is the optimal way to implement such an algorithm in C?

  • 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-24T20:46:09+00:00Added an answer on May 24, 2026 at 8:46 pm

    UPDATE: Incorporating Ante’s observations and making the answer community wiki.

    As usual in this type of problems, coding any working brute-force algorithm is relatively easy, but the more math. you do with pencil and paper, the better (faster) algorithm you can get.

    Let’s use a shorthand notation: let M(i) mean 1111…1 (i ones).

    Given a number n (let’s say n = 23), you want to find a number m such that M(m) is divisible by n. A straightforward approach is to check 1, 11, 111, 1111, … until we find a number divisible by n. Note: there might exist a closed-form solution for finding m given n, so this approach is not necessarily optimal.

    When iterating over M(1), M(2), M(3), …, the interesting part is, obviously, how to check whether a given number is divisible by n. You could implement long division, but arbitrary-precision arithmetic is slow. Instead, consider the following:

    Assume that you already know, from previous iterations, the value of M(i) mod n. If M(i) mod n = 0, then you’re done (M(i) is the answer), so let’s assume it’s not. You want to find M(i+1) mod n. Since M(i+1) = 10 * M(i) + 1, you can easily calculate M(i+1) mod n, as it’s (10 * (M(i) mod n) + 1) mod n. This can be calculated using fixed-precision arithmetic even for large values of n.

    Here’s a function which calculates the smallest number of ones which are divisible by n (translated to C from Ante’s Python answer):

    int ones(int n) {
            int i, m = 1;
            /* Loop invariant: m = M(i) mod n, assuming n > 1 */
            for (i = 1; i <= n; i++) {
                    if (m == 0)
                            return i;  /* Solution found */
                    m = (10*m + 1) % n;
            }
            return -1;  /* No solution */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this question in one Interview. Please help me in getting the
I came across this question in an interview. You have a nxn matrix containing
Came across this question in one of the interview samples. A 16-byte aligned allocation
I came across this question.A number is called lucky if the sum of its
I came across this question on an interview questions thread. Here is the question:
I am preparing for my interview and came across this question: Consider that i
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
I came across this post , which reports the following interview question: Given two
I came across this in an java interview earlier. It was one of the
This is an interview question I came across: find K first digits of the

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.