Swapping Pairs Make Sum Equal
In this class, We discuss Swapping Pairs Make Sum Equal.
Readers can prepare an entire competitive coding course to crack product development companies. Click Here.
The reader should have basic coding skills to work with competitive coding. Click here.
Question:
Given two arrays of size N and M.
Our task is to find a pair of elements from each array.
Swapping the elements makes the sum of elements in both arrays is same.
Example:
N = 6, M = 4
A = [4, 1, 2, 1, 1, 2]
B = [3, 6, 3, 3]
Output = 1
Swap elements one from A and three from B.
After swapping, the sum of elements is the same in both arrays.
There is a possibility to swap, so output 1.
If there is no such possibility, Then output -1.
Logic:
Two ways we can solve this example.
1) Using sorting
2) Using Heap
Both ways are important to understand.
The step-by-step explanation is provided in the video.
Code: