Answered step by step
Verified Expert Solution
Link Copied!

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 2
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 (e.g. 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 solution(string &S, string &T);
that, given two strings S and T, both of length N, returns the minimum number of swaps needed to minimize the difference between the two numbers represented by the input strings.
For example, given S=29162" and T=10524" your function should return
2. We can swap the second and the fourth digits and obtain "20122" and "19564". The difference between the numbers is 558 and the number of swaps is 2. 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 S and T are equal and within the range [1..100,000];
S and T consist only of digits and no other characters;
SOLVE IT IN CPP SIMPLE TERMS
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Decisions Based On Data Analytics For Business Excellence

Authors: Bastian Weber

1st Edition

9358681683, 978-9358681680

Students also viewed these Databases questions

Question

1. What is going on? What do you notice?

Answered: 1 week ago