The stock market
Practice
2.7 (6 votes)
Algorithms
Dynamic programming
2d dynamic programming
Problem
53% Success 1492 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array A consisting of N integers and a special number K. You have to divide the array into non-empty subarrays. The sum of elements of the 1st, 3rd, 5th, 7th...(odd) subarrays is the price at which you buy the stocks and the sum of elements of the 2nd, 4th, 6th, 8th....(even) subarrays is the price at which you sell the stocks. The profit of this transaction would be equal to = selling price - buying price, where the selling price is the sum of elements of the even indexed subarrays and the buying price is the sum of elements of the odd indexed subarrays.

Task 

Determine the maximum profit you can make by buying and selling the stocks in an optimal manner.

Example 

 Assumptions

  • N = 5
  • A = [7, 7, 8, 2, 2]
  • K = 2

Approach

  • Check all the possible partitions of the array and take the one which gives the maximum profit
  • Possible partitions are as follows [- (7) + (7 + 8 + 2 + 2), - (7 + 7) + (8 + 2 + 2), - (7 + 7 + 8) + (2 + 2), - (7 + 7 + 8 + 2) + (2)]
  • The profits of these possible partitions are as follows [19 - 7, 12 - 14, 4 - 22, 2 - 24] = [12, -2, -18, -22] hence the answer is 12.

Function description

Complete the function solve provided in the editor. This function takes the following 3 parameters and returns the required answer:

  • N: Represents the size of array A
  • A: Represents the elements of array A
  • K: Represents the special number

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 denoting the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
  • For each test case:
    • The first line contains a single integer N denoting the size of the array A.
    • The second line contains space-separated integers denoting the elements of the array A.
    • The third line contains the special number K.

Output format

For each test case in a new line, print a single integer representing the maximum profit.

Constraints 

\(1\leq T \leq 10\)

\(1\leq N \leq 10^4\)

\(-10^9\leq A_i \leq 10^9\)

\(1\leq K \leq min(N,10^2)\)

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:30
58 votes
Tags:
ApprovedDynamic ProgrammingGraphsMathMediumOpenRecursionTwo dimensional
Points:30
25 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpenTwo dimensional
Points:30
Tags:
RecursionDynamic ProgrammingAlgorithmsRecursive dp2D dynamic programming