Programmazione competitivaFacile

Partizione in due sottoarray

Guida dettagliata e implementazione Python per il problema della "Partizione in due sottoarray".

Dichiarazione del problema

Facile

Scrivi una funzione max_difference_subarrays(arr, k) che accetta un array arr di dimensione n e un numero intero k. Seleziona un sottoinsieme di elementi k da arr in modo tale che la differenza assoluta tra la somma degli elementi selezionati e la somma dei rimanenti elementi n-k sia massimizzata e restituisca questa differenza assoluta massima.

Vincoli
  • 1 <= k < len(arr) <= 10^5
  • 1 <= arr[i] <= 10^4

Esempi

Example 1
Input
max_difference_subarrays([8, 4, 5, 2, 10], 2)
Output
17
Explanation

If we select the 2 smallest elements {2, 4} (sum 6), the remaining elements are {8, 5, 10} (sum 23). The difference is |23 - 6| = 17.

Example 2
Input
max_difference_subarrays([1, 1, 1, 1, 1], 3)
Output
1
Explanation

Select 3 elements (sum 3), remaining 2 sum to 2. The difference is 1.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Greedy come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.