
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
Minimize Last Remaining Element of Array by Selecting Pairs
We are given an array of non-negative integers and we have to perform an operation of the given array any number of times possible so that we can choose any element of the array and can choose another element from the array that will be less than or equal to the current element and we will subtract it from the first element. After subtracting we will remove the first element if it becomes zero.
After applying the above-described method any number of possible times we have to find the minimum possible element present in the array.
Sample Examples
Input
int arr[] = {12, 18, 6, 9, 15}
Output
3
Explanation
We can remove 6 from the 15 then the array will become: {12,18, 6, 9,9}.
We can remove 9 from the 9 and remove zero from the array:
{12, 18, 6, 9 }
Remove three times 6 from 18 and 2 times from 12, we will get {6, 9}
At last, we can remove 6 from 9 => {6,3}.
Finally, remove 2 times 3 from 6 and we will get 3 as the minimum final answer.
Input
int arr[] = {2, 8, 4, 1}
Output
1
Explanation: We can remove 1 from all the three given array elements up to their own number of times and get 1 as the minimum answer.
Observation
We can always reduce the smallest number from the greatest element, so the thing is we can always reduce great elements by subtracting the smallest elements. But, after subtracting the value from the greater element it may become smaller and then we can use it to reduce the previous smaller element and this can go until one becomes zero.
So, for two elements x and y, the above approach can be implemented as the code below:
Pseudo Code
while(x != 0 || y != 0){ if(x > y){ swap(x,y); } y = y -x; }
The fact here is the last remaining element will be the gcd of both x and y, as the above pseudo-code is same for the gcd.
Also, the time complexity of the above code is O(min(X,Y)) which is very high instead, we can use the in-built gcd function of the C++ programming language to get the solution in logarithmic time.
Pseudo Code
while(x != 0 || y !=0 ){ if(x > y){ swap(x,y); } y = y % x; }
In the above method, we are using the mod instead of subtraction and get the same result in very less time.
Approach
We have seen the basics of the GCD above, so we are going to use the inbuilt gcd function in this approach. Let us see the steps of implementation of the code:
First, we will create a function and pass the given array and its size as the parameter and it will return the final answer as the return value.
In the function, we will create an integer to store the gcd of the complete array. Also, it will be initialized by 0 as it will not impact the gcd of the array.
We will use the for loop to traverse over the array and at each iteration we will take the gcd of the current index element and the variable.
We will use the in-built gcd function of the C++ programming language and store the result in the same variable.
At last, we will return the final answer and print it in the main function.
Example
#include <bits/stdc++.h> using namespace std; // helper function to get the answer int getMin(int arr[], int n){ int gcd = 0; // variable to store the gcd // traversing over the given array for(int i=0; i<n; i++){ gcd = __gcd(gcd, arr[i]); } // return the final answer return gcd; } // main function int main(){ int arr[] = {12, 18, 6, 9, 15}; // given array int n = 5; // size of the given array //calling the function int ans = getMin(arr, n); cout<<"The minimum element remains in the array after performing the given operations a maximum possible number of times is: "<<ans<<endl; return 0; }
Output
The minimum element remains in the array after performing the given operations a maximum possible number of times is: 3
Time and Space Complexity
The time complexity of the above code is O(N*log(max(arr element))), here we are traversing over the array which brings the factor of N, and due to the inbuilt gcd function, we are getting the factor of log.
The space complexity of the above code is O(1), as we are not using any extra space.
Conclusion
In this tutorial, we have implemented a program to find the minimum smallest number present in the array after removing the other elements. We can remove a number from the other number if the other number is greater than or equal to it and will remove it from the array if it becomes zero. We have used the gcd method to find answers with the time complexity of O(N*log(max_array_element)) and constant space complexity.