Maximum partitions possible of given Array with cost at most K and equal count of odd and even elements
Last Updated :
16 Jul, 2021
Given two integers N, K, and an array, arr[] of size N, containing an equal number of even and odd elements, and also given that the cost of partitioning the array by making a cut between index i and i+1 is equal to the abs(arr[i]-arr[i+1]), the task is to find the maximum partitions of the array, such that each partition has an equal number of odd and even elements and has a total cost less than or equal to K.
Examples:
Input: N = 6, K = 4, arr[] = {1, 2, 5, 10, 15, 20}
Output: 1
Explanation:
The only possible way to partition is by making a cut between index 1 and index 2.
Cost of partition = |arr[1]( =2)- arr[2](= 5)| = 3, which is less than and equal to K
Arrays after partition: {1, 2} and {5, 10, 15, 20}.
Input: N = 4, K = 10, arr[] = {1, 3, 2, 4}
Output: 0
Approach: The given problem can be solved based on observations that it is always possible to make a valid partition by making a cut between the index i and i+i, if the count of even and odd elements in the prefix of i is equal. Follow the steps to solve the problem.
- Initialize a vector V to store the costs of all possible cuts in the array.
- Also, initialize variables, say odd as 0 and even as 0 which store the count of even and odd elements.
- Traverse the array arr[], using the variable i and perform the following steps:
- If the current element is odd, then increment odd by 1. Otherwise, increment even by 1.
- If the value of odd is equal to the value of even, then append the value of |arr[i]–arr[i+1]| to V.
- Sort the vector V in ascending order.
- Initialize an integer variable ans as 1, to store the number of partitions of the array.
- Traverse the vector V, using the variable i and perform the following steps:
- If the value of V[i] is less than or equal to K then update the value K as K – V[i] and increment ans by 1.
- Otherwise, break out of the loop.
- Finally, after completing the above steps, print the value of ans as the answer obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumcut( int arr[], int N, int K)
{
int odd = 0;
int even = 0;
vector< int > V;
for ( int i = 0; i < N - 1; i++) {
if (arr[i] % 2 == 1) {
odd++;
}
else {
even++;
}
if (odd == even) {
int cost = abs (arr[i] - arr[i + 1]);
V.push_back(cost);
}
}
int ans = 0;
sort(V.begin(), V.end());
for ( int i = 0; i < V.size(); i++) {
if (V[i] <= K) {
K = K - V[i];
ans++;
}
else {
break ;
}
}
return ans;
}
int main()
{
int arr[] = {1, 2, 5, 10, 15, 20};
int N = sizeof (arr) / sizeof (arr[0]);
int K = 4;
cout << maximumcut(arr, N, K);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
class GFG
{
public static int maximumcut( int arr[], int N, int K)
{
int odd = 0 ;
int even = 0 ;
ArrayList<Integer> V = new ArrayList<Integer>();
for ( int i = 0 ; i < N - 1 ; i++) {
if (arr[i] % 2 == 1 ) {
odd++;
}
else {
even++;
}
if (odd == even) {
int cost = Math.abs(arr[i] - arr[i + 1 ]);
V.add(cost);
}
}
int ans = 0 ;
V.sort( null );
for ( int i = 0 ; i < V.size(); i++)
{
if (V.get(i) <= K)
{
K = K - V.get(i);
ans++;
}
else {
break ;
}
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 5 , 10 , 15 , 20 };
int N = arr.length;
int K = 4 ;
System.out.println(maximumcut(arr, N, K));
}
}
|
Python3
def maximumcut(arr, N, K):
odd = 0
even = 0
V = []
for i in range ( 0 , N - 1 , 1 ):
if (arr[i] % 2 = = 1 ):
odd + = 1
else :
even + = 1
if (odd = = even):
cost = abs (arr[i] - arr[i + 1 ])
V.append(cost)
ans = 0
V.sort()
for i in range ( len (V)):
if (V[i] < = K):
K = K - V[i]
ans + = 1
else :
break
return ans
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 10 , 15 , 20 ]
N = len (arr)
K = 4
print (maximumcut(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximumcut( int []arr, int N, int K)
{
int odd = 0;
int even = 0;
List< int > V = new List< int >();
for ( int i = 0; i < N - 1; i++)
{
if (arr[i] % 2 == 1)
{
odd++;
}
else
{
even++;
}
if (odd == even)
{
int cost = Math.Abs(arr[i] - arr[i + 1]);
V.Add(cost);
}
}
int ans = 0;
V.Sort();
for ( int i = 0; i < V.Count; i++)
{
if (V[i] <= K)
{
K = K - V[i];
ans++;
}
else
{
break ;
}
}
return ans;
}
public static void Main()
{
int []arr = { 1, 2, 5, 10, 15, 20 };
int N = arr.Length;
int K = 4;
Console.Write(maximumcut(arr, N, K));
}
}
|
Javascript
<script>
function maximumcut(arr, N, K) {
let odd = 0;
let even = 0;
var V = [];
for (let i = 0; i < N - 1; i++) {
if (arr[i] % 2 == 1) {
odd++;
}
else {
even++;
}
if (odd == even) {
let cost = Math.abs(arr[i] - arr[i + 1]);
V.push(cost);
}
}
let ans = 0;
V.sort();
for (let i = 0; i < V.length; i++) {
if (V[i] <= K) {
K = K - V[i];
ans++;
}
else {
break ;
}
}
return ans;
}
let arr = [1, 2, 5, 10, 15, 20];
let N = arr.length;
let K = 4;
document.write(maximumcut(arr, N, K));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...