Max separations
Practice
2.6 (9 votes)
Real world
Introduction to dynamic programming 1
Algorithms
Dynamic programming
Problem
84% Success 3393 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

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

  \(2\leq N\leq 100\)

  \(1\leq K \leq100\)

  \(1\leq arr_i\leq 100\)

Code snippets (also called starter code/boilerplate code)

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

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
21 votes
Tags:
ApprovedBrute-force searchData StructuresDynamic ProgrammingEasyOpen
Points:20
66 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingEasyOpen
Points:20
26 votes
Tags:
AlgorithmsApprovedBinary SearchData StructuresDynamic ProgrammingMediumOpenopen