
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
Reduce Array to Single Element by Removing Increasing Pairs
Reducing an array to a single element by repeatedly removing element is done by the following criteria
Select indices i and j such that i < j and arr[i] < arr[j] and convert one of the two elements to 0.
Problem Statement
Given an array arr[] containing positive integers. Find if the array can be reduced to a single element by repeatedly removing an element from any increasing pair. If possible return true along with the indices chosen and the index of the element that is removed.
Sample Example 1
Input
arr[] = {5, 7, 10, 2, 4, 8}
Output
True 0 1 1 0 2 2 4 5 4 3 5 3 0 5 5
Explanation
Step 1 Choose elements at index 0 and 1 i.e. 5 and 7. Then removing 7 i.e. element at 1st index.
Step 2 Choose elements at index 0 and 2 i.e. 5 and 10. Then removing 10 i.e. element at 2nd index.
Step 3 Choose elements at index 4 and 5 i.e. 4 and 8. Then removing 4 i.e. element at 4th index.
Step 4 Choose elements at index 3 and 5 i.e. 2 and 8. Then removing 2 i.e. element at 3rd index.
Step 5 Choose elements at index 0 and 5 i.e. 5 and 8. Then removing 8 i.e. element at 5th index.
Sample Example 2
Input
arr[] = {9, 3, 5}
Output
False
Explanation
Step 1 Choose elements at index 1 and 2 i.e. 3 and 5. Then removing 5 i.e. element at 2nd index.
Step 2 Choose elements at index 0 and 1 i.e. 9 and 3. Since 3 is not greater than 9, so the array cannot be reduced.
Solution Approach
The first step to the solution to this problem is to choose a valid set of indices first. Then thenest steps include deciding onto which element to delete. If we choose element of 0th index in the pair then remove the non-zero index element else remove the smaller element. Repeat the steps until a single element is left. If a single element is left, return true else return false.
Pseudocode
procedure order(a: array of integer, n: integer) g <- empty queue of integers first <- a[0] p <- 0 for i <- 1 to n - 1 if a[i] > first g.push(i) while g.size() > 0 do index <- g.front() g.pop() remove <- index while remove > p do print remove, " ", index, " ", remove remove <- remove - 1 end while print "0 ", index, " ", index p <- index end while end procedure procedure canReduced(arr: array of integer, N: integer) if arr[0] > arr[N - 1] then print "False" else print "True" order(arr, N) end if end procedure
Example: C++ Implementation
The following code uses the above approach to solve the problem.
#include <bits/stdc++.h> using namespace std; // Function to print the order of indices of converted numbers void order(int a[], int n){ // Values of indices with numbers > first index queue<int> g; int first = a[0]; // Index of the closest consecutive number to index 0 int p = 0; // Pushing the indices for (int i = 1; i < n; i++){ if (a[i] > first) g.push(i); } // Traverse the queue while (g.size() > 0){ // Index of the closest number > arr[0] int index = g.front(); g.pop(); int remove = index; // Remove elements present in indices [1, remove - 1] while (--remove > p) { cout << remove << " " << index << " " << remove << endl; } cout << 0 << " " << index << " " << index << endl; // Updating the previous index to index p = index; } } // Function to check if array arr[] can be reduced to single element or not void canReduced(int arr[], int N){ // Element can't be reduced to single element if (arr[0] > arr[N - 1]){ cout << "False" << endl; } else { cout << "True" << endl; order(arr, N); } } int main(){ // Given array arr[] int arr[] = {5, 7, 10, 2, 4, 8}; int N = sizeof(arr) / sizeof(arr[0]); canReduced(arr, N); return 0; }
Output
True 0 1 1 0 2 2 4 5 4 3 5 3 0 5 5
Conclusion
In conclusion, the given code provides solution to the problem using a queue data structure. The tiem complexity is O(N) where N is the length of the input array as we iterate over the array once to find the increasing pairs and performs an operation on each pair taking constant time. The space complexity is also O(N) due to the queue used to store the indices. The order of removing the elements can alter the final result. The above solution removes elements in decreasing order indices in na increasing pair.