Given an array arr[] of size N and an integer K, the task is to split the given array into maximum possible subarrays having equal count of even and odd elements such that the cost to split the array does not exceed K.

The cost to split an array into a subarray is the difference between the last and first elements of the subarrays respectively.

Examples:

Input:_ arr[] = {1, 2, 5, 10, 15, 20}, K = 4_

Output:_ 1_

Explanation:

The optimal way is to split the array between 2 and 5.

So it splits into {1, 2} and {5, 10, 15, 20}.

Also, both the subarrays contain an equal number of even and odd elements. The cost of the split is abs(2 – 5) = 3 which is ≤ K.

Input:_ arr[] = {1, 2, 3, 4, 5, 6}, K = 100_

Output:_ 2_

Explanation:

The optimal way is to make two splits such that the subarrays formed are {1, 2}, {3, 4}, {5, 6}.

The total cost is abs(2 – 3) + abs(4 – 5) = 2

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

Naive Approach: The simplest approach to solve this problem is as follows:

  1. Split the array at every index and check if the cost is less than K or not.
  2. If the cost is less than K, then check if the number of odd and even elements in the subarray are equal or not.
  3. Now check if another split is possible or not by repeating the same steps.
  4. After checking all possible splits, print the minimum cost which add up to a sum less than K.

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

Efficient Approach: The idea is to maintain the counters which store the number of even numbers and odd numbers in the array. Below are the steps:

  1. Initialize an array (say poss[]) which stores the cost of all possible splits.
  2. Traverse through the array arr[]. For every index, check if the subarray up to this index and the subarray starting from the next index has equal count of odd and even elements.
  3. If the above condition satisfies, then a split is possible. Store the cost associated with this split in poss[].
  4. Repeat the above steps for all the elements in the array and store the costs of every split.
  5. Cost of split at index i can be obtained by abs(arr[i + 1] – arr[i]).
  6. Now, in order to find the maximum number of possible splits, sort the array poss[] that contains the costs of each possible split.
  7. Now select all minimum costs from poss[] whose sum is less than or equal to K.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to find the cost of

// splitting the arrays into subarray

// with cost less than K

**int** make_cuts(``**int** arr[], **int** n, **int** K)

{

// Store the possible splits

**int** ans = 0;

// Stores the cost of each split

vector<``**int**``> poss;

// Stores the count of even numbers

**int** ce = 0;

// Stores the count

// of odd numbers

**int** co = 0;

**for** (``**int** x = 0; x < n - 1; x++) {

// Count of odd & even elements

**if** (arr[x] % 2 == 0)

ce++;

**else**

co++;

// Check the required conditions

// for a valid split

**if** (ce == co && co > 0

&& ce > 0) {

poss.push_back(

**abs**``(arr[x]

- arr[x + 1]));

}

}

// Sort the cost of splits

sort(poss.begin(), poss.end());

// Find the minimum split costs

// adding up to sum less than K

**for** (``**int** x : poss) {

**if** (K >= x) {

ans++;

K -= x;

}

**else**

**break**``;

}

**return** ans;

}

// Driver Code

**int** main()

{

// Given array and K

**int** N = 6;

**int** K = 4;

**int** arr[] = { 1, 2, 5, 10, 15, 20 };

// Function Call

cout << make_cuts(arr, N, K);

}

Output:

1

Time Complexity:_ O(N log(N))_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #greedy #hash #sorting #frequency-counting #subarray

Split an Array to maximize subarrays having equal count of odd and even elements
2.40 GEEK