Minimum number of working days required to achieve each of the given scores
Last Updated :
19 Dec, 2021
Given an array arr[] consisting of N integers and an array P[] consisting of M integers such that P[i] represents the score obtained by working on the ith day. The task is to find the minimum number of days needed to work to achieve a score of at least arr[i], for each array element arr[i].
Examples:
Input: arr[] = {400, 200, 700}, P[] = {100, 300, 400, 500, 600}
Output: 2 2 3 4 5
Explanation:
Following are the number of days required for each array elements:
- arr[0](= 400): To earn 400 points one has to work for first 2 days making the total points equal to 100 + 300 = 400(>= arr[0]).
- arr[1](= 200): To earn 200 points one has to work for first 2 days making the total points = 100 + 300 = 400(>= arr[1]).
- arr[2](= 700): To earn 700 points one has to work for first 3 days making the total points = 100 + 300 + 400 = 800(>= arr[2]).
Input: arr[] = {1400}, P[] = {100, 300}
Output: -1
Naive Approach: The simplest approach to solve the problem is to traverse the array arr[] and for every array, element find the minimum index of the array P[] such that the sum of the subarray over the range [0, i] is at least arr[i].
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by finding the prefix sum array of P[] and then using binary search to find the sum whose value is at least arr[i]. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySeach(vector< int > P, int N)
{
int i = 0;
int j = P.size() - 1;
int index = -1;
while (i <= j)
{
int mid = i + (j - i) / 2;
if (P[mid] >= N)
{
index = mid;
j = mid - 1;
}
else
{
i = mid + 1;
}
}
return index;
}
void minDays(vector< int > P, vector< int > arr)
{
for ( int i = 1; i < P.size(); i++)
{
P[i] += P[i] + P[i - 1];
}
for ( int i = 0; i < arr.size(); i++)
{
int index = binarySeach(P, arr[i]);
if (index != -1)
{
cout << index + 1 << " " ;
}
else
{
cout << -1 << " " ;
}
}
}
int main()
{
vector< int > arr = { 400, 200, 700, 900, 1400 };
vector< int > P = { 100, 300, 400, 500, 600 };
minDays(P, arr);
return 0;
}
|
Java
public class GFG {
static void minDays( int [] P, int [] arr)
{
for ( int i = 1 ; i < P.length; i++) {
P[i] += P[i] + P[i - 1 ];
}
for ( int i = 0 ;
i < arr.length; i++) {
int index = binarySeach(
P, arr[i]);
if (index != - 1 ) {
System.out.print(
index + 1 + " " );
}
else {
System.out.print(
- 1 + " " );
}
}
}
static int binarySeach(
int [] P, int N)
{
int i = 0 ;
int j = P.length - 1 ;
int index = - 1 ;
while (i <= j) {
int mid = i + (j - i) / 2 ;
if (P[mid] >= N) {
index = mid;
j = mid - 1 ;
}
else {
i = mid + 1 ;
}
}
return index;
}
public static void main(String[] args)
{
int [] arr = { 400 , 200 , 700 , 900 , 1400 };
int [] P = { 100 , 300 , 400 , 500 , 600 };
minDays(P, arr);
}
}
|
Python3
def minDays(P, arr):
for i in range ( 1 , len (P)):
P[i] + = P[i] + P[i - 1 ]
for i in range ( len (arr)):
index = binarySeach(P, arr[i])
if (index ! = - 1 ):
print (index + 1 , end = " " )
else :
print ( - 1 , end = " " )
def binarySeach(P, N):
i = 0
j = len (P) - 1
index = - 1
while (i < = j):
mid = i + (j - i) / / 2
if (P[mid] > = N):
index = mid
j = mid - 1
else :
i = mid + 1
return index
if __name__ = = '__main__' :
arr = [ 400 , 200 , 700 , 900 , 1400 ]
P = [ 100 , 300 , 400 , 500 , 600 ]
minDays(P, arr)
|
C#
using System;
class GFG{
static void minDays( int [] P, int [] arr)
{
for ( int i = 1; i < P.Length; i++)
{
P[i] += P[i] + P[i - 1];
}
for ( int i = 0; i < arr.Length; i++)
{
int index = binarySeach(P, arr[i]);
if (index != -1)
{
Console.Write(index + 1 + " " );
}
else
{
Console.Write(-1 + " " );
}
}
}
static int binarySeach( int [] P, int N)
{
int i = 0;
int j = P.Length - 1;
int index = -1;
while (i <= j)
{
int mid = i + (j - i) / 2;
if (P[mid] >= N)
{
index = mid;
j = mid - 1;
}
else
{
i = mid + 1;
}
}
return index;
}
public static void Main( string [] args)
{
int [] arr = { 400, 200, 700, 900, 1400 };
int [] P = { 100, 300, 400, 500, 600 };
minDays(P, arr);
}
}
|
Javascript
<script>
function binarySeach(P, N)
{
var i = 0;
var j = P.length - 1;
var index = -1;
while (i <= j)
{
var mid = i + parseInt((j - i) / 2);
if (P[mid] >= N)
{
index = mid;
j = mid - 1;
}
else
{
i = mid + 1;
}
}
return index;
}
function minDays(P, arr)
{
for ( var i = 1; i < P.length; i++)
{
P[i] += P[i] + P[i - 1];
}
for ( var i = 0; i < arr.length; i++)
{
var index = binarySeach(P, arr[i]);
if (index != -1)
{
document.write( (index + 1) + " " );
}
else
{
document.write( -1 + " " );
}
}
}
var arr = [400, 200, 700, 900, 1400];
var P = [100, 300, 400, 500, 600];
minDays(P, arr);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...