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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:47:50+00:00 2026-05-25T11:47:50+00:00

Here’s the description of this problem: You are given two integers a and b.

  • 0

Here’s the description of this problem:

You are given two integers a and b. You want to find the shortest sequence of operations necessary to transform a into b, where at each step you are allowed to add or subtract 5, 7, or 12.

For example, if you are given a = -5 and b = 19, the shortest path is

-5 + 12 + 12 = 19

If you were given 1 and 3, the shortest path would be

1 + 7 - 5 = 2

The only way I can think about solving this is using BFS and maybe some more pruning. Is there a better algorithm I could use instead?

Thanks!

  • 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-25T11:47:51+00:00Added an answer on May 25, 2026 at 11:47 am

    Let’s start off with a set of interesting observations. As many others have noted, the goal is to find some linear combination 5x + 7y + 12z = b – a with integer coefficients such that |x| + |y| + |z| is minimized. But there are some very interesting connections between these three numbers that we can exploit:

    1. If we ever have a combination 5x + 7y + 12z where x and y are both positive or both negative, we can cancel out some number of x’s and y’s to add an equivalent number of 12s. In other words, no optimal solution has the same sign on both x and y, because we could always make this solution better.
    2. If we ever have a combination 5x + 7y + 12z where y and z have opposite signs, we can always remove a 7 and 12 and add in a 5 of the appropriate sign to get a better solution. Similarly, if x and z have opposite signs, we can always remove a 5 and 12 and add a 7 of the appropriate sign. This means that we never need to consider any solution where z has the same sign as either x or y, because it means that there would have to be a better solution.

    Let’s think about what (1) and (2) collectively tell us. (1) says that the signs on x and y can’t be the same, since we can always do better. (2) says that if x and z have opposite signs or if y and z have opposite signs, we can always do better. Collectively this means that

    Lemma: At least one of x, y, or z must be zero in the optimal solution.

    To see this, if all three are nonzero, if x and y have the same sign, then we can clearly make the solution better by replacing them with 12s. Otherwise, x and y have opposite signs. Thus if x and z have different signs, by (2) we can replace them with fewer 7’s, otherwise y and z have different signs and by (2) we can replace them with fewer 5’s.

    Okay, this is looking really great! This means that we just need to solve these three integer equations and find which one has the smallest sum of coefficients:

    • 5x + 7y = b – a
    • 5x + 12z = b – a
    • 7y + 12z = b – a

    Fortunately, by Bezout’s identity, because gcd(5, 7) = gcd(5, 12) = gcd(7, 12) = 1, all of these systems of equations have a solution for any value of b – a.

    Now, let’s see how to solve each of these equations. Fortunately, we can use some cute tricks to greatly reduce our search space. For example, for 5x + 7y = b – a, the value of x can’t be outside of [-6, +6], since if it were we could just replace seven of the 5’s with five 7’s. This means that we can solve the above equation by doing the following:

    For x = -6 to +6, see if 5x + 7y = b – a has an integer solution by computing (b – a) – 5x and seeing if it’s divisible by seven. If so, the number of steps required to solve the problem is given by |x| + |((b – a) – 5x) / 7|.

    We can use similar tricks to solve the latter two equations – for the second equation, x ranges from -11 to +11, and for the third y ranges from -11 to +11 as well. We can then just take the best answer out of all three equations to see what the answer is.

    Here’s some pseudocode to record the fewest number of steps possible. This can easily be modified to return what those steps are by just recording which of the solutions was used and then expanding it out into a full path:

    Let best = infinity
    
    # Solve 5x + 7y = b - a
    for x = -6 to +6:
        if ((b - a) - 5 * x) mod 7 = 0:
            best = min(best, |x| + |((b - a) - 5 * x) / 7|)
    
    # Solve 5x + 12y = b - a
    for x = -11 to +11:
        if ((b - a) - 5 * x) mod 12 = 0:
            best = min(best, |x| + |((b - a) - 5 * x) / 12|)
    
    # Solve 7x + 12y = b - a
    for x = -11 to +11:
        if ((b - a) - 7 * x) mod 12 = 0:
            best = min(best, |x| + |((b - a) - 7 * x) / 12|)
    
    return best;
    

    This algorithm is amazingly fast – it runs in O(1) time because the number of iterations required to solve each three of the linear systems is a constant (at most 23). It requires only O(1) memory to hold the possible values, and I think that in practice it’s probably the fastest algorithm you’ll be able to write.

    Hope this helps!

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

Sidebar

Related Questions

Here's the jquery from my view: $(#btnSelect1).click(function () { var donationTypeID = $(this).closest('p').find('#selectList').val(); var
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is another spoj problem that asks how to find the number of distinct
Here's a coding problem for those that like this kind of thing. Let's see
Here's my code Form1 TicketDetails td = new TicketDetails(getTicketID()); td.Show(); I want to close
Here are my Codes ... I want to remove Elements that are listed in
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:

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.