Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 2 In this task you are given two strings of digits that represent ( possibly very large ) integers. Your goal is to make
Task
In this task you are given two strings of digits that represent possibly very large integers. Your goal is to make those numbers as close to one another as possible. In other words, you want to minimize the absolute value of their difference. You can swap some of the corresponding digits eg the first digit of the first number with the first digit of the second number, the second digit of the first number with the second digit of the second number, etc. Swapping the digits is an extremely tiring task, so you want to make as few swaps as possible.
Write a function:
int solutionstring &S string &T;
that, given two strings and both of length returns the minimum number of swaps needed to minimize the difference between the two numbers represented by the input strings.
For example, given and your function should return
We can swap the second and the fourth digits and obtain and The difference between the numbers is and the number of swaps is One can easily check that the difference is the smallest possible. Note that we could obtain the same difference by swapping the first, third and fifth digits, but this solution requires three swaps.
Write an efficient algorithm for the following assumptions:
lengths of and T are equal and within the range ;
S and T consist only of digits and no other characters;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started