Computer Science, asked by gangaharasarmadantu, 11 hours ago

Given two integer arrays, arr_a and arr_b of lengths n and m respectively, the following operation can be performed any number of times: • In one move, choose any one element from any of the two arrays and either increase or decrease its value by 1. The goal is to find the minimum number of moves required to change the arrays in such a way that the minimum element of arr_a is greater than or equal to the maximum element of arr_b.​

Answers

Answered by pratibhakalaskar111
1

Answer:

Given two arrays that have the same values but in a different order, we need to make a second array the same as a first array using the minimum number of swaps.

Examples:

Input : arrA[] = {3, 6, 4, 8},

arrB[] = {4, 6, 8, 3}

Output : 2

we can make arrB to same as arrA in 2 swaps

which are shown below,

swap 4 with 8, arrB = {8, 6, 4, 3}

swap 8 with 3, arrB = {3, 6, 4, 8}

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

This problem can be solved by modifying the array B. We save the index of array A elements in array B i.e. if ith element of array A is at jth position in array B, then we will make arrB[i] = j

For above given example, modified array B will be, arrB = {3, 1, 0, 2}. This modified array represents the distribution of array A element in array B and our goal is to sort this modified array in a minimum number of swaps because after sorting only array B element will be aligned with array A elements.

Similar questions