Minimum steps required to reach the end of Array with a given capacity
source link: https://www.geeksforgeeks.org/minimum-steps-required-to-reach-the-end-of-array-with-a-given-capacity//
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Minimum steps required to reach the end of Array with a given capacity
Given an array arr[] of size n. You will have to traverse the array from left to right deducting each element of the array from the initial capacity, such that your initial capacity should never go below 0. In case, the current capacity is less than the current element, we can traverse back to the start of the array to refill our capacity, and then start where we left off, the task is to return the minimum number of steps required to reach the end of the array, given you are initially at the start of the array or index (-1).
Examples:
Input: arr = [3, 3, 4, 4], capacity = 7
Output: 14
Explanation: Start at the index 1:
- Walk to index 0 (1 step) and subtract it, capacity = 4
- Walk to index 1 (1 step) and subtract it. capacity = 1
- Since after subtraction of element at index 2 capacity = -3 which is negative so we have to return to index -1(2 steps) and capacity becomes 7 again.
- Walk to index 2 (3 steps) and subtract it. capacity = 3
- Since after subtraction of element at index 3 capacity = -1 which is negative so we have to return to index -1(3 steps) and capacity becomes 7 again.
- Walk to index 3 (4 steps) and subtract it.capacity = 3
- Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.
Input: arr = [1, 1, 1, 4, 2, 3], capacity = 4
Output: 30
Explanation: Start at the index 1:
- subtract arr 0, 1, and 2 (3 steps). Return to index -1(3 steps).
- subtract arr 3 (4 steps). Return to index -1(4 steps).
- subtract arr 4 (5 steps). Return to index -1 (5 steps).
- subtract arr 5 (6 steps).
- Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.
Input: arr = [8, 8, 8, 8, 8], capacity = 10
Output: 25
Explanation: You have to return to index -1 after each iteration.
Steps needed = 25.
Naive Approach: The basic way to solve the problem is as follows:
We can declare a prefix array and store the results before hand of each and every arr at place. Then using binary search we could search for our specified fruit and calculate the steps in the whole iteration.
Steps that were to follow the above approach:
- Call minSteps function to return the minimum number of steps to reach the end of the array.
- Create pre array which contains the prefix sum up to that point in the input array.
- Then, loop until we reach the end of the array, and do the following:
- Binary search for the current range we can go.
- Add up to the answer, and return it ans+n, once the end is reached of the array.
Below is the code to implement the above approach:
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; int minSteps(vector< int >& arr, int c) { int n = arr.size(); // Find the size of array vector< int > pre(n, 0); // Declare a prefix sum vector for // storing the result before hand pre[0] = arr[0]; for ( int i = 1; i < n; i++) pre[i] = pre[i - 1] + arr[i]; // Fill the prefix sum vector int curr = 0, sc = c, ans = 0; while (1) { int ind = lower_bound(pre.begin(), pre.end(), sc) - pre.begin(); // Finding the lowerbound using // binary search and substracting // the perviously computed value if (ind >= n) return ans + n; // If index goes beyond the array // size then return the size // summed up with array size else if (pre[ind] == sc) { // If element at prefix array // is equal to source then // check if index has reached // the last element if (ind == n - 1) return ans + n; curr = ind; } else curr = ind - 1; sc = c + pre[curr]; // Add prefix value at current // index to source ans += (2 * (curr + 1)); } return 0; } // Drivers code int main() { vector< int > v{ 8, 8, 8, 8, 8 }; int c = 10; // Function Call cout << minSteps(v, c) << endl; return 0; } |
25
Time Complexity: O(n + log n), this is for prefix sum and then binary search.
Auxiliary Space: O(n), for declaring a prefix array.
Efficient Approach: To solve the problem follow the below idea:
We can substract elements going in order from left to right and get back to index -1 once capacity becomes negative and calculate the whole iteration of going to index -1 and coming back to the unsubstracted element. This will be a simple linear traversal.
Steps to implement the above approach:
- Loop over the entire array from left to right and do the following:
- If the current capacity is less than the current array element,
- increase steps for both-way movement, and add 1 to move to the next element.
- else, increment the steps and subtract the element from the total capacity left.
- If the current capacity is less than the current array element,
- Finally, return the total number of steps traveled.
Below is the code to implement the above approach:
// C++ Code for the above approach: #include <bits/stdc++.h> using namespace std; int subelement(vector< int >& arr, int capacity) { int steps = 0; // Declare a steps variable for // keeping the count of steps int c = capacity; for ( int i = 0; i < arr.size(); i++) { if (c < arr[i]) { // If our capapcity is less // than current array element // increment the steps for // bi-directional traversal // and adding 1 to move to // next element steps = steps + ((i * 2) + 1); c = capacity; // Making the capacity element // as it was originally c = c - arr[i]; // Subtracting the current // position value from // capacity } else { c = c - arr[i]; // If capacity is greater than // current element we simply // subtract the same from // capacity steps++; // Incrementing the steps // for each iteration } } return steps; } // Drivers code int main() { vector< int > v{ 8, 8, 8, 8, 8 }; int c = 10; // Function Call cout << subelement(v, c) << endl; return 0; } |
25
Time Complexity: O(n), this is due to linear traversal.
Space Complexity: O(1), as we don’t need any extra space.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK