Sudo Placement | Placement Tour
Last Updated :
26 Aug, 2022
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.
C++
#include <bits/stdc++.h>
using namespace std;
bool canBeOptimalValue( int K, int arr[], int N, int B,
int & value)
{
int tmp[N];
for ( int i = 0; i < N; i++)
tmp[i] = (arr[i] + K * (i + 1));
sort(tmp, tmp + N);
value = 0;
for ( int i = 0; i < K; i++)
value += tmp[i];
return value <= B;
}
void findNoOfElementsandValue( int arr[], int N, int B)
{
int start = 0;
int end = N;
int ans = 0;
int cumulativeValue = 0;
while (start <= end) {
int mid = (start + end) / 2;
if (canBeOptimalValue(mid, arr, N, B,
cumulativeValue)) {
ans = mid;
start = mid + 1;
}
else
end = mid - 1;
}
canBeOptimalValue(ans, arr, N, B, cumulativeValue);
cout << ans << " " << cumulativeValue << endl;
}
int main()
{
int arr[] = { 1, 2, 5, 6, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int B = 90;
findNoOfElementsandValue(arr, N, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int value;
static boolean canBeOptimalValue( int K, int arr[],
int N, int B)
{
int [] tmp = new int [N];
for ( int i = 0 ; i < N; i++)
tmp[i] = (arr[i] + K * (i + 1 ));
Arrays.sort(tmp);
value = 0 ;
for ( int i = 0 ; i < K; i++)
value += tmp[i];
return value <= B;
}
static void findNoOfElementsandValue( int arr[],
int N, int B)
{
int start = 0 ;
int end = N;
int ans = 0 ;
value = 0 ;
while (start <= end)
{
int mid = (start + end) / 2 ;
if (canBeOptimalValue(mid, arr, N, B))
{
ans = mid;
start = mid + 1 ;
}
else
end = mid - 1 ;
}
canBeOptimalValue(ans, arr, N, B);
System.out.print(ans + " " +
value + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 5 , 6 , 3 };
int N = arr.length;
int B = 90 ;
findNoOfElementsandValue(arr, N, B);
}
}
|
Python3
value = 0
def canBeOptimalValue(K: int , arr: list , N: int , B: int ) - > bool :
global value
tmp = [ 0 ] * N
for i in range (N):
tmp[i] = (arr[i] + K * (i + 1 ))
tmp.sort()
value = 0
for i in range (K):
value + = tmp[i]
return value < = B
def findNoOfElementsandValue(arr: list , N: int , B: int ):
global value
start = 0
end = N
ans = 0
value = 0
while start < = end:
mid = (start + end) / / 2
if canBeOptimalValue(mid, arr, N, B):
ans = mid
start = mid + 1
else :
end = mid - 1
canBeOptimalValue(ans, arr, N, B)
print (ans, value)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 6 , 3 ]
N = len (arr)
B = 90
findNoOfElementsandValue(arr, N, B)
|
C#
using System;
class GFG
{
static int value;
static bool canBeOptimalValue( int K, int []arr,
int N, int B)
{
int [] tmp = new int [N];
for ( int i = 0; i < N; i++)
tmp[i] = (arr[i] + K * (i + 1));
Array.Sort(tmp);
value = 0;
for ( int i = 0; i < K; i++)
value += tmp[i];
return value <= B;
}
static void findNoOfElementsandValue( int []arr,
int N, int B)
{
int start = 0;
int end = N;
int ans = 0;
value = 0;
while (start <= end)
{
int mid = (start + end) / 2;
if (canBeOptimalValue(mid, arr, N, B))
{
ans = mid;
start = mid + 1;
}
else
end = mid - 1;
}
canBeOptimalValue(ans, arr, N, B);
Console.Write(ans + " " +
value + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 5, 6, 3 };
int N = arr.Length;
int B = 90;
findNoOfElementsandValue(arr, N, B);
}
}
|
Javascript
<script>
let cumulativeValue = 0;
function canBeOptimalValue(K, arr, N, B) {
let tmp = new Array(N);
for (let i = 0; i < N; i++)
tmp[i] = (arr[i] + K * (i + 1));
tmp.sort((a, b) => a - b);
cumulativeValue = 0;
for (let i = 0; i < K; i++)
cumulativeValue += tmp[i];
return cumulativeValue <= B;
}
function findNoOfElementsandValue(arr, N, B) {
let start = 0;
let end = N;
let ans = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (canBeOptimalValue(mid, arr, N, B)) {
ans = mid;
start = mid + 1;
}
else
end = mid - 1;
}
canBeOptimalValue(ans, arr, N, B, cumulativeValue);
document.write(ans + " " + cumulativeValue + "<br>" );
}
let arr = [1, 2, 5, 6, 3];
let N = arr.length;
let B = 90;
findNoOfElementsandValue(arr, N, B);
</script>
|
Time Complexity: O(N * (log N)2), where N is the number of elements in the given array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...