📌  相关文章
📜  拆分数组以最大化具有相等数量的奇数和偶数元素的子数组,成本不超过 K

📅  最后修改于: 2021-10-27 08:55:44             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是将给定的数组拆分为具有相等数量的偶数和奇数元素的最大可能子数组,以便拆分数组的成本不超过K

例子:

朴素的方法:解决这个问题的最简单的方法如下:

  1. 在每个索引处拆分数组并检查成本是否小于K。
  2. 如果成本小于K ,则检查子数组中奇数和偶数元素的数量是否相等。
  3. 现在通过重复相同的步骤检查是否可以进行另一个拆分。
  4. 检查所有可能的拆分后,打印总和小于K的最小成本。

时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:这个想法是维护存储数组中偶数和奇数数量的计数器。以下是步骤:

  1. 初始化一个数组(比如poss[] ),它存储所有可能的拆分成本。
  2. 遍历数组arr[] 。对于每个索引,检查到此索引的子数组和从下一个索引开始的子数组是否具有相等的奇数和偶数元素。
  3. 如果满足上述条件,则可以进行拆分。将与此拆分相关的成本存储在poss[] 中
  4. 对数组中的所有元素重复上述步骤并存储每次拆分的成本。
  5. 可以通过abs(arr[i + 1] – arr[i])获得索引i处的拆分成本。
  6. 现在,为了找到可能拆分的最大数量,对包含每个可能拆分成本的数组poss[] 进行排序。
  7. 现在从poss[] 中选择总和小于或等于K 的所有最小成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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 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);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the cost of
// splitting the arrays into subarray
// with cost less than K
static int make_cuts(int arr[], int n, int K)
{
     
    // Store the possible splits
    int ans = 0;
 
    // Stores the cost of each split
    Vector poss = new Vector();
 
    // 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.add(Math.abs(arr[x] -
                              arr[x + 1]));
        }
    }
 
    // Sort the cost of splits
    Collections.sort(poss);
 
    // 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
public static void main(String[] args)
{
     
    // Given array and K
    int N = 6;
    int K = 4;
 
    int arr[] = { 1, 2, 5, 10, 15, 20 };
 
    // Function call
    System.out.print(make_cuts(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the cost of
# splitting the arrays into subarray
# with cost less than K
def make_cuts(arr, n, K):
 
    # Store the possible splits
    ans = 0
 
    # Stores the cost of each split
    poss = []
 
    # Stores the count of even numbers
    ce = 0
 
    # Stores the count
    # of odd numbers
    co = 0
 
    for x in range(n - 1):
 
        # Count of odd & even elements
        if(arr[x] % 2 == 0):
            ce += 1
        else:
            co += 1
 
        # Check the required conditions
        # for a valid split
        if(ce == co and co > 0 and ce > 0):
            poss.append(abs(arr[x] -
                            arr[x + 1]))
 
    # Sort the cost of splits
    poss.sort()
 
    # Find the minimum split costs
    # adding up to sum less than K
    for x in poss:
        if (K >= x):
            ans += 1
            K -= x
        else:
            break
 
    return ans
 
# Driver Code
 
# Given array and K
N = 6
K = 4
 
arr = [ 1, 2, 5, 10, 15, 20 ]
 
# Function call
print(make_cuts(arr, N, K))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the cost of
// splitting the arrays into subarray
// with cost less than K
static int make_cuts(int []arr, int n, int K)
{
     
    // Store the possible splits
    int ans = 0;
 
    // Stores the cost of each split
    List poss = new List();
 
    // 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.Add(Math.Abs(arr[x] -
                              arr[x + 1]));
        }
    }
 
    // Sort the cost of splits
    poss.Sort();
 
    // Find the minimum split costs
    // adding up to sum less than K
    foreach(int x in poss)
    {
        if (K >= x)
        {
            ans++;
            K -= x;
        }
        else
            break;
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array and K
    int N = 6;
    int K = 4;
 
    int []arr = { 1, 2, 5, 10, 15, 20 };
 
    // Function call
    Console.Write(make_cuts(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1

时间复杂度: O(N log(N))
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程