Reduce the Array to Atmost one Element by the Given Operations


In this problem, we will reduce the array size to 1 or 0 by performing the given operations in each turn.

We can sort the array in each turn to get the maximum elements in each iteration. Also, we can use the head data structure to improve the performance of the code.

Problem statement − We have given a nums[] array. We need to decrease the array by performing the below operations.

  • Choose two maximum elements of the array.

  • If both elements are the same, remove both elements from the array.

  • If both elements are not the same, remove both elements from the array and insert the abs(first − second) into the array.

Print the last element of the array. If the array is empty, print 0.

Sample examples

Input

nums = {5, 9, 8, 3, 2, 5};

Output

0

Explanation 

  • In the first turn, we take 9 and 8 and add its difference to the array. So, the array becomes [5, 3, 2, 5, 1].

  • In the second turn, we take 5 and 5. So, the array becomes [3, 2, 1].

  • In the next turn, we take 3 and 2. So, array becomes [1, 1]

  • In the last turn, we take 1 and 1. So, the array becomes empty, and we print 0.

Input

nums = {5, 5, 5, 5, 5};

Output

5

Explanation − We remove pair of 5 two times, and one 5 remains as it is in the array.

Input

nums = {4, 8, 7, 6};

Output

1

Explanation − First, we select 8 and 7. So, the array becomes [4, 1, 6]. After that, we select 4 and 6. So, the array becomes [1, 2]. In the last operation, the array becomes [1].

Approach 1

In this approach, we will traverse the array until the size of the array becomes 1 or 0. In each iteration, we will sort the array and perform the given operation on the first 2 elements of the sorted array. At last, we will print the output according to the array size.

Algorithm

Step 1− Store the size of the array in the ‘len’ variable.

Step 2− Start traversing the array using the while loop.

Step 3− Use the sort() method in the loop to sort the array into the reverse order.

Step 4− Take the first and second elements of the array. Also, take the difference between the first and second elements of the array.

Step 5− If the difference is 0, remove the first elements of an array, and decrease the ‘len’ by 2. If the difference is not 0, remove the first 2 elements and decrease the ‘len’ by 1.

Step 6− At last, if the size of the array is 0, return 0. Otherwise, return the first element of the array.

Example

#include <bits/stdc++.h>
using namespace std;

int findLast(vector<int> &nums) {
    int len = nums.size();
    int p = 0;
    while (len > 1) {
        // Sort array in reverse order
        sort(nums.begin(), nums.end(), greater<int>());
        // Take the first and second elements of the array
        int a = nums[0];
        int b = nums[1];
        // Take the difference between the first and second element
        int diff = a - b;
        if (diff == 0) {
            nums.erase(nums.begin());
            nums.erase(nums.begin());
            len -= 2;
        } else {
            nums.erase(nums.begin());
            nums.erase(nums.begin());
            nums.push_back(diff);
            len -= 1;
        }
    }
    // When the size of the array is 0
    if (nums.size() == 0)
        return 0;
    return nums[0];
}
int main() {
    vector<int> nums = {5, 9, 8, 3, 2, 5};
    cout << "The last remaining element after performing the given operations is " << findLast(nums) << "\n";
    return 0;
}

Output

The last remaining element after performing the given operations is 0

Time complexity − O(N*NlogN), where O(N) is for traversing the array, and O(NlogN) is for sorting the array in each iteration.

Space complexity − O(N) to sort the array.

Approach 2

In this approach, we will use the priority queue, which implements the heap data structure. It always stores the element in a sorted order. So, we can remove the first 2 maximum elements easily.

Algorithm

Step 1− Define the ‘p_queue’ named priority queue.

Step 2− Insert all array elements into the priority queue.

Step 3− Make iterations until the size of the priority queue is greater than 1.

Step 4− Remove the first 2 elements of the priority queue one by one.

Step 5− Take the difference between both elements.

Step 6− If the difference is not 0, push it into the priority queue.

Step 7− Finally, if the queue size is 0, return 0.

Step 8 − Otherwise, return the top element from the queue.

Example

#include <bits/stdc++.h>
using namespace std;

int findLast(vector<int> &nums) {
    // Defining a priority queue
    priority_queue<int> p_queue;
    // Inserting array elements in priority queue
    for (int p = 0; p < nums.size(); ++p)
        p_queue.push(nums[p]);
    // Make iterations
    while (p_queue.size() > 1) {
        // Take the first element from queue
        int first = p_queue.top();
        p_queue.pop();
        // Get the second element from queue
        int second = p_queue.top();
        p_queue.pop();
        // Take the difference of first and second elements
        int diff = first - second;
        if (diff != 0)
            p_queue.push(diff);
    }
    // When queue is empty
    if (p_queue.size() == 0)
        return 0;
    // Return the last remaining element
    return p_queue.top();
}
int main() {
    vector<int> nums = {5, 9, 8, 3, 2, 5};
    cout << "The last remaining element after performing the given operations is " << findLast(nums) << "\n";
    return 0;
}

Output

The last remaining element after performing the given operations is 0

Time complexity − O(NlogN) for insertion and deletion of elements in the priority queue.

Space complexity − O(N) to store elements in the priority queue.

The priority queue data structure is always useful when we require array data in particular order after inserting or deleting any elements. It implements the heap data structure, which makes the insertion and deletion.

Updated on: 22-Jul-2023

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements