Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Make Arrays Equal | Greedy on Arrays
Greedy Algorithms using Python
course content

Contenido del Curso

Greedy Algorithms using Python

Greedy Algorithms using Python

1. Greedy Algorithms: Overview and Examples
2. Greedy on Arrays
3. Greedy on Graphs

Make Arrays Equal

Given two arrays of the same size: arr1 and arr2, and you have to convert arr1 to arr2 with minimum operations (i.e make all elements of both arrays equal). Operation means increasing or decreasing some element by one. Note that the order of elements doesn’t matter.

Example:

arr1 = [8, 6, 1], arr2 = [6, 5, 5]

res = 7: for example, decrease 8 three times, and increase 1 four times. 7 operations in total.

Example:

arr1 = [1,1,1,1,10], arr2 = [1,1,1,1,1]

res = 9

This problem is quite easy if you use a greedy approach: change elements in such a way that the smallest one from arr1 must be converted to the smallest from arr2, to minimize the number of operations. Same way, every next element from arr1 must be converted to the appropriate element from arr2. Note that there is not the only solution, and it’s possible that you can reach the minimum number of operations by reducing in some other way. But we are greedy, and we use this approach.

So, the algorithm is:

  1. Sort both arrays to know the order of pairs of elements (arr1[i], arr2[i])
  2. Count the answer as sum of absolute differences between elements arr1[i] and arr2[j]

Tarea

Complete the algorithm; follow the comments in code.

Tarea

Complete the algorithm; follow the comments in code.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 1
toggle bottom row

Make Arrays Equal

Given two arrays of the same size: arr1 and arr2, and you have to convert arr1 to arr2 with minimum operations (i.e make all elements of both arrays equal). Operation means increasing or decreasing some element by one. Note that the order of elements doesn’t matter.

Example:

arr1 = [8, 6, 1], arr2 = [6, 5, 5]

res = 7: for example, decrease 8 three times, and increase 1 four times. 7 operations in total.

Example:

arr1 = [1,1,1,1,10], arr2 = [1,1,1,1,1]

res = 9

This problem is quite easy if you use a greedy approach: change elements in such a way that the smallest one from arr1 must be converted to the smallest from arr2, to minimize the number of operations. Same way, every next element from arr1 must be converted to the appropriate element from arr2. Note that there is not the only solution, and it’s possible that you can reach the minimum number of operations by reducing in some other way. But we are greedy, and we use this approach.

So, the algorithm is:

  1. Sort both arrays to know the order of pairs of elements (arr1[i], arr2[i])
  2. Count the answer as sum of absolute differences between elements arr1[i] and arr2[j]

Tarea

Complete the algorithm; follow the comments in code.

Tarea

Complete the algorithm; follow the comments in code.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 1
toggle bottom row

Make Arrays Equal

Given two arrays of the same size: arr1 and arr2, and you have to convert arr1 to arr2 with minimum operations (i.e make all elements of both arrays equal). Operation means increasing or decreasing some element by one. Note that the order of elements doesn’t matter.

Example:

arr1 = [8, 6, 1], arr2 = [6, 5, 5]

res = 7: for example, decrease 8 three times, and increase 1 four times. 7 operations in total.

Example:

arr1 = [1,1,1,1,10], arr2 = [1,1,1,1,1]

res = 9

This problem is quite easy if you use a greedy approach: change elements in such a way that the smallest one from arr1 must be converted to the smallest from arr2, to minimize the number of operations. Same way, every next element from arr1 must be converted to the appropriate element from arr2. Note that there is not the only solution, and it’s possible that you can reach the minimum number of operations by reducing in some other way. But we are greedy, and we use this approach.

So, the algorithm is:

  1. Sort both arrays to know the order of pairs of elements (arr1[i], arr2[i])
  2. Count the answer as sum of absolute differences between elements arr1[i] and arr2[j]

Tarea

Complete the algorithm; follow the comments in code.

Tarea

Complete the algorithm; follow the comments in code.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Given two arrays of the same size: arr1 and arr2, and you have to convert arr1 to arr2 with minimum operations (i.e make all elements of both arrays equal). Operation means increasing or decreasing some element by one. Note that the order of elements doesn’t matter.

Example:

arr1 = [8, 6, 1], arr2 = [6, 5, 5]

res = 7: for example, decrease 8 three times, and increase 1 four times. 7 operations in total.

Example:

arr1 = [1,1,1,1,10], arr2 = [1,1,1,1,1]

res = 9

This problem is quite easy if you use a greedy approach: change elements in such a way that the smallest one from arr1 must be converted to the smallest from arr2, to minimize the number of operations. Same way, every next element from arr1 must be converted to the appropriate element from arr2. Note that there is not the only solution, and it’s possible that you can reach the minimum number of operations by reducing in some other way. But we are greedy, and we use this approach.

So, the algorithm is:

  1. Sort both arrays to know the order of pairs of elements (arr1[i], arr2[i])
  2. Count the answer as sum of absolute differences between elements arr1[i] and arr2[j]

Tarea

Complete the algorithm; follow the comments in code.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 1
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt