Thursday, July 8, 2021

Published July 08, 2021 by Anonymous with 0 comment

Minimum days to make Array elements with value at least K sum at least X

Given two integers X, K, and two arrays arr[] and R[] both consisting of N positive integers where R[i] denotes the amount by which arr[i] increases in one day, the task is to find the minimum number of days after which the sum of array elements having value greater than or equal to K becomes at least X.

Examples:

Input: X = 100, K = 45, arr[] = {2, 5, 2, 6}, R[] = {10, 13, 15, 12}
Output: 4
Explanation:
Consider the following values of array after each day:

  1. Day 1: After the day 1, all array element modifies to {12, 18, 17, 18}. The sum of elements having values >= K(= 45) is 0.
  2. Day 2: After the day 2, all array element modifies to {22, 31, 32, 30}. The sum of elements having values >= K(= 45) is 0.
  3. Day 3: After the day 3, all array element modifies to {32, 44, 47, 42}. The sum of elements having values >= K(= 45) is 47.
  4. Day 4: After the day 4, all array element modifies to {42, 57, 62, 54}. The sum of elements having values >= K(= 45) is 57 + 62 + 54 = 167, which is at least X(= 100).

Therefore, the minimum number of days required is 4.

Input: X = 65, K = 10, arr[] = {1, 1, 1, 1, 3}, R[] = {2, 1, 2, 2, 1}
Output: 9


Naive Approach: The simplest approach to solve the given problem is to keep incrementing the number of days and whenever the sum of the array elements having a value at least K becomes greater than or equal to X. After incrementing for D days, print the value of the current number of days obtained.

Time Complexity: O(N*X)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by using Binary Search. Follow the steps below to solve the problem:

  • Initialize two variables, say low as 0 and high as X.
  • Initialize a variable, say minDays that stores the minimum number of days.
  • Iterate until the value of low is at most high and perform the following steps:
    • Initialize a variable mid as low + (high – low)/2 and variable, say sum as 0 to store the sum of array elements after mid number of days.
    • Traverse the array, arr[] using the variable i and perform the following steps:
      • Initialize a variable temp as (arr[i] + R[i]*mid).
      • If the value of temp is not less than K add the value of temp to sum.
    • If the value of sum is at least X, then update the value of minDays to mid and the value of high to (mid – 1).
    • Otherwise, update the value of low to (mid + 1).
  • After completing the above steps, print the value of minDays as the resultant minimum number of days.

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

void findMinDays(int arr[], int R[],

                 int N, int X, int K)

{

    

    

    int low = 0, high = X;

    int minDays;

  

    

    while (low <= high) {

  

        

        int mid = (low + high) / 2;

  

        int sum = 0;

  

        

        for (int i = 0; i < N; i++) {

  

            

            

            int temp = arr[i] + R[i] * mid;

  

            

            

            if (temp >= K) {

  

                

                

                sum += temp;

            }

        }

  

        

        

        if (sum >= X) {

  

            

            minDays = mid;

            high = mid - 1;

        }

  

        

        else {

            low = mid + 1;

        }

    }

  

    

    

    cout << minDays;

}

  

int main()

{

    int X = 100, K = 45;

    int arr[] = { 2, 5, 2, 6 };

    int R[] = { 10, 13, 15, 12 };

    int N = sizeof(arr) / sizeof(arr[0]);

    findMinDays(arr, R, N, X, K);

  

    return 0;

}

Time Complexity: O(N*log X)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment