Max separations

2.6

9 votes
Real world, Introduction to Dynamic Programming 1, Algorithms, Dynamic Programming
Problem

You are working in the Data Consistency team of your company. You are allocated a task as follows:

  • You have a data stream consisting of an equal number of odd and even numbers. You can make separations in the data stream but the number of odd elements should be equal to the number of even elements in both partitions after separation. Also, if you make a separation between a number x and number y, then the cost of this operation will be |x-y| coins.

You are given the following:

  • An integer N
  • An array arr
  • An integer K

Task

Determine the maximum number of separations that can be made in the array by spending no more than K coins.

Example

Assumptions

  • N = 6
  • K = 4
  • arr = [1, 2, 5, 10, 5, 20]

Approach

  • The optimal answer is to split-sequence between 2 and 5. The price of this cut is equal to 3 coins, hence answer is 1.

Function description

Complete the function Decryptions() which takes an integer N and an array Arr. This function takes the following parameters and returns the required answer: 

  • N: Represents the size of the data stream
  • K: Represents the limit of coins
  • arr: Represents the data stream

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains the integer N.
  • The second line contains integer K.
  • The third line contains N integers denoting the data stream.

Output format

Print the maximum number of separations that can be made in the array by spending no more than K coins.

Constraints

  2N100

  1K100

  1arri100

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Sample Input
4 
10
1 3 2 4
Sample Output
0
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

It is not possible to make even one cut even with an unlimited number of coins as the condition for valid separation can not be fulfilled in any separation.

Editor Image

?