Programowanie konkurencyjneŁatwe

Podział na dwie podtablice

Szczegółowy przewodnik i implementacja Python dla problemu „Podział na dwie podtablice”.

Oświadczenie o problemie

Łatwe

Napisz funkcję max_difference_subarrays(arr, k), która pobiera tablicę arr o rozmiarze n i liczbę całkowitą k. Wybiera podzbiór elementów k z arr w taki sposób, że różnica bezwzględna między sumą wybranych elementów a sumą pozostałych elementów n-k jest zmaksymalizowana i zwraca tę maksymalną różnicę bezwzględną.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla Greedy, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.