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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:09:02+00:00 2026-06-13T11:09:02+00:00

Problem: An algorithm to find the number of six digit numbers where the sum

  • 0

Problem: “An algorithm to find the number of six digit numbers where the sum of the first three digits is equal to the sum of the last three digits.”

I came across this problem in an interview and want to know the best solution. This is what I have till now.

Approach 1: The Brute force solution is, of course, to check for each number (between 100,000 and 999,999) whether the sum of its first three and last three digits are equal. If yes, then increment certain counter which keeps count of all such numbers.

But this checks for all 900,000 numbers and so is inefficient.

Approach 2: Since we are asked “how many” such numbers and not “which numbers”, we could do better. Divide the number into two parts: First three digits (these go from 100 to 999) and Last three digits (these go from 000 to 999). Thus, the sum of three digits in either part of a candidate number can range from 1 to 27.
* Maintain a std::map<int, int> for each part where key is the sum and value is number of numbers (3 digit) having that sum in the corresponding part.
* Now, for each number in the first part find out its sum and update the corresponding map.
* Similarly, we can get updated map for the second part.
* Now by multiplying the corresponding pairs (e.g. value in map 1 of key 4 and value in map 2 of key 4) and adding them up we get the answer.

In this approach, we end up checking 1K numbers.

My question is how could we further optimize? Is there a better solution?

  • 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-13T11:09:03+00:00Added an answer on June 13, 2026 at 11:09 am

    For 0 <= s <= 18, there are exactly 10 - |s - 9| ways to obtain s as the sum of two digits.

    So, for the first part

    int first[28] = {0};
    for(int s = 0; s <= 18; ++s) {
        int c = 10 - (s < 9 ? (9 - s) : (s - 9));
        for(int d = 1; d <= 9; ++d) {
            first[s+d] += c;
        }
    }
    

    That’s 19*9 = 171 iterations, for the second half, do it similarly, with the inner loop starting at 0 instead of 1, that’s 19*10 = 190 iterations. Then sum first[i]*second[i] for 1 <= i <= 27.

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

Sidebar

Related Questions

I wrote an algorithm to find a solution to the subset sum problem in
I came across this problem of finding said probability and my first attempt was
I would like to find a better algorithm to solve the following problem: There
Ok, so here's the problem: I need to find any number of intem groups
I am working on designing an algorithm to find the largest factorial number present
The problem is to find the n-th Catalan number mod m , where m
Problem Given a number n, 2<=n<=2^63. n could be prime itself. Find the prime
The problem is to extend the binary search algorithm to find all occurrences of
I am trying to find whether number is prime or not for 1000 digit
What's a good algorithm to solve this problem? I have three groups of people

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.