# Sudo Placement | Placement Tour

Given an array A of N positive integers and a budget B. Your task is to decide the maximum number of elements to be picked from the array such that the cumulative cost of all picked elements is less than or equal to budget B. Cost of picking the ith element is given by : A[i] + (i * K) where, K is a constant whose value is equal to the number of elements picked. The indexing(i) is 1 based. Print the maximum number and its respective cumulative cost.

**Examples:**

Input: arr[] = { 2, 3, 5 }, B = 11

Output: 2 11

Explanation: Cost of picking maximum elements = {2 + (1 * 2) } + {3 + (2 * 2)} = 4 + 7 = 11 (which is equal to budget)

Input: arr[] = { 1, 2, 5, 6, 3 }, B = 90

Output: 4 54

**Prerequisites** : Binary Search

**Approach**: The idea here is to use binary search on all possible values of K i.e. the optimal number of elements to be picked. *Start* with zero as lower bound and *End* with total number of elements i.e. N as upper bound. Check if by setting K as current *Mid*, obtained cumulative cost is less than or equal to budget. If it satisfies the condition, then try to increase K by setting *Start* as *(Mid + 1)*, otherwise try to decrease K by setting *End* as *(Mid – 1)*.

Checking of the condition can be done in a brute force manner by simply modifying the array according to the given formula and adding the K (current number of elements to be picked) smallest modified values to get the cumulative cost.

Below is the implementation of above approach.

`// CPP Program to find the optimal number of ` `// elements such that the cumulative value ` `// should be less than given number ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// This function returns true if the value cumulative ` `// according to received integer K is less than budget ` `// B, otherwise returns false ` `bool` `canBeOptimalValue(` `int` `K, ` `int` `arr[], ` `int` `N, ` `int` `B, ` ` ` `int` `& value) ` `{ ` ` ` `// Initialize a temporary array which stores ` ` ` `// the cumulative value of the original array ` ` ` `int` `tmp[N]; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `tmp[i] = (arr[i] + K * (i + 1)); ` ` ` ` ` `// Sort the array to find the smallest K values ` ` ` `sort(tmp, tmp + N); ` ` ` ` ` `value = 0; ` ` ` `for` `(` `int` `i = 0; i < K; i++) ` ` ` `value += tmp[i]; ` ` ` ` ` `// Check if the value is less than budget ` ` ` `return` `value <= B; ` `} ` ` ` `// This function prints the optimal number of elements ` `// and respective cumulative value which is less than ` `// the given number ` `void` `findNoOfElementsandValue(` `int` `arr[], ` `int` `N, ` `int` `B) ` `{ ` ` ` `int` `start = 0; ` `// Min Value or lower bound ` ` ` ` ` `int` `end = N; ` `// Max Value or upper bound ` ` ` ` ` `// Initialize answer as zero as optimal value ` ` ` `// may not exists ` ` ` `int` `ans = 0; ` ` ` ` ` `int` `cumulativeValue = 0; ` ` ` ` ` `while` `(start <= end) { ` ` ` `int` `mid = (start + end) / 2; ` ` ` ` ` `// If the current Mid Value is an optimal ` ` ` `// value, then try to maximize it ` ` ` `if` `(canBeOptimalValue(mid, arr, N, B, ` ` ` `cumulativeValue)) { ` ` ` `ans = mid; ` ` ` `start = mid + 1; ` ` ` `} ` ` ` `else` ` ` `end = mid - 1; ` ` ` `} ` ` ` `// Call Again to set the corresponding cumulative ` ` ` `// value for the optimal ans ` ` ` `canBeOptimalValue(ans, arr, N, B, cumulativeValue); ` ` ` ` ` `cout << ans << ` `" "` `<< cumulativeValue << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 5, 6, 3 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Budget ` ` ` `int` `B = 90; ` ` ` `findNoOfElementsandValue(arr, N, B); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4 54

**Time Complexity**: O(N * (log N)^{2}), where N is the number of elements in the given array.

## Recommended Posts:

- Sudo Placement[1.4] | K Sum
- Sudo Placement[1.5] | Partition
- Sudo Placement[1.5] | Wolfish
- Sudo Placement[1.4] | BST Traversal
- Sudo Placement[1.3] | Playing with Stacks
- Sudo Placement[1.3] | Final Destination
- Sudo Placement[1.3] | Stack Design
- Sudo Placement[1.5] | Second Smallest in Range
- Sudo Placement[1.4] | Jumping the Subtree
- Sudo Placement | Range Queries
- Sudo Placement | Special Subsequences
- Sudo Placement | Palindrome Family
- Sudo Placement 2 | Matrix Series
- Sudo Placement | Beautiful Pairs
- Sudo Placement[1.7] | Greatest Digital Root

This article is contributed by **Team GeeksforGeeks**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.