
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Number of Subtract Operations to Make an Array Decreasing
In this article, we will work upon sorting an array in decreasing order by applying some subtraction operations on it.
Problem Statement
We are given an array containing a series of n numbers from array[0], array[1], . . . . , array[ n-1 ].
We are also given an integer nums. Our task is to generate a decreasing array by subtracting nums from the array elements in every operation. We need to return the least possible number of such operations required in order to make the array decreasing in order.
Let us understand the problem with an example
Input: n = 5, nums = 2, array[] = {5, 8, 6, 9, 10} Output: 4
Explanation
Step 1 Start with the original array [5, 8, 6, 9, 10].
Step 2 Since array[1] > array[0] (8 > 5), we subtract array[1] by nums twice to make it smaller than or equal to array[0]. The updated array becomes [5, 4, 6, 9, 10].
Step 3 Now, array[2] > array[1] (6 > 4), so we subtract array[2] by nums twice to make it smaller than or equal to array[1]. The updated array becomes [5, 4, 2, 9, 10].
Step 4 Next, array[3] > array[2] (9 > 2), so we subtract array[3] by nums four times to make it smaller than or equal to array[2]. The updated array becomes [5, 4, 2, -7, 10].
Step 5 Finally, array[4] > array[3] (10 > -7), so we subtract array[4] by nums five times to make it smaller than or equal to array[3]. The updated array becomes [5, 4, 2, -7, -15].
Approach
Now, let us discuss the solution approach for this problem
For an array to be decreasing, we must have array[iterator] <= array[iterator+1] for any iterator between 0 to n, so whenever we encounter adjacent numbers such that array[iterator] > array[iterator+1], we have to perform subtraction operation on the array[iteratot+1].
Traverse each element of the array from index 1 to n-1. This means we iterate through the array beginning from the 2nd element.
Check if array[ iterator] is greater than array[ iterator-1]. If this condition is true, it means the current element surpasses the preceding element in magnitude, and we need to make it smaller.
Calculate the number of subtractions needed to make array[ iterator] smaller than or equal to array[ iterator-1]. The formula used is
Operations_needed = (array[ iterator] - array[iterator-1]) / nums
This calculates the number of times we need to subtract K from array[ iterator] to reach a value smaller than or equal to array[ iterator-1].
Check if there is a remainder when dividing (array[ iterator ] array[ iterator-1]) by nums. If the remainder is 0, we add 1 to Operations_needed to ensure the updated array[ iterator] becomes smaller than array[ iterator-1]. This step ensures that we make the necessary adjustments to ensure array[ iterator] is smaller than or equal to array[ iterator-1].
Modify array[iterator] by subtracting (nums * Operations_needed) from it. This step reduces array[iterator] by the calculated number of subtractions (Operations_needed * nums ) to make it smaller than or equal to array[ iterator-1 ].
Example
Now let us code the above discussion. The C++ implementation of the approach discussed above is
#include <bits/stdc++.h> using namespace std; int min_subtr_opr(int array[], int n, int nums){ int Operations_needed; int res = 0; for (int it = 1; it < n; it++) { Operations_needed = 0; if (array[it] > array[it - 1]) { Operations_needed = (array[it] - array[it - 1]) / nums; if ((array[it] - array[it - 1]) % nums != 0) Operations_needed++; array[it] = array[it] - nums * Operations_needed; } res = res + Operations_needed; } return res; } int main() { int array[] = { 5, 8, 6, 9, 10 }; int n = 5 ; int nums = 2 ; cout<< "The minimum number of subtraction operations needed to make the given array:"<< endl; cout<< "{ "; for (int it =0; it <n; it ++){ cout<<array[it] << ", "; } cout <<"}" << " decreasing is \n" << min_subtr_opr(array, n, nums) << endl; return 0; }
Output
The minimum number of subtraction operations needed to make the given array: { 5, 8, 6, 9, 10, } decreasing is 10
Time Complexity We run the for-loop n times, hence the time taken by the code to execute is O(N).
Space Complexity As we have not used any additional space, the space complexity is constant O(1).