3

Maximize pair decrements required to reduce all array elements except one to 0

 1 year ago
source link: https://www.geeksforgeeks.org/maximize-pair-decrements-required-to-reduce-all-array-elements-except-one-to-0/
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.

Maximize pair decrements required to reduce all array elements except one to 0

Given an array arr[] consisting of N distinct elements, the task is to find the maximum number of pairs required to be decreased by 1 in each step, such that N – 1 array elements are reduced to 0 and the remaining array element is a non-negative integer.

Examples:

Input: arr[] = {1, 2, 3}
Output: 3
Explanation: 
Decrease arr[1] and arr[2] by 1 modifies arr[] = {1, 1, 2} 
Decrease arr[1] and arr[2] by 1 modifies arr[] = {1, 0, 1} 
Decrease arr[0] and arr[2] by 1 modifies arr[] = {0, 0, 0} 
Therefore, the maximum number of decrements required is 3.

Input: arr[] = {1, 2, 3, 4, 5}
Output: 7

Approach: The problem can be solved Greedily. Follow the steps below to solve the problem:

  • Initialize a variable, say cntOp, to store maximum count of steps required to make (N – 1) elements of the array equal to 0.
  • Create a priority queue, say PQ, to store the array elements.
  • Traverse the array and insert the array elements into PQ.
  • Now repeatedly extract the top 2 elements from the priority queue, decrease the value of both the elements by 1, again insert both the elements in priority queue and increment the cntOp by 1. This process continues while (N – 1) element of the PQ becomes equal to 0.
  • Finally, print the value of cntOp

Below is the implementation of the above approach:

  • Python3
  • Javascript
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count maximum number of steps
// to make (N - 1) array elements to 0
int cntMaxOperationToMakeN_1_0(int arr[], int N)
{
// Stores maximum count of steps to make
// (N - 1) elements equal to 0
int cntOp = 0;
// Stores array elements
priority_queue<int> PQ;
// Traverse the array
for (int i = 0; i < N; i++) {
// Insert arr[i] into PQ
PQ.push(arr[i]);
}
// Extract top 2 elements from the array
// while (N - 1) array elements become 0
while (PQ.size() > 1) {
// Stores top element
// of PQ
int X = PQ.top();
// Pop the top element
// of PQ.
PQ.pop();
// Stores top element
// of PQ
int Y = PQ.top();
// Pop the top element
// of PQ.
PQ.pop();
// Update X
X--;
// Update Y
Y--;
// If X is not equal to 0
if (X != 0) {
// Insert X into PQ
PQ.push(X);
}
// if Y is not equal
// to 0
if (Y != 0) {
// Insert Y
// into PQ
PQ.push(Y);
}
// Update cntOp
cntOp += 1;
}
return cntOp;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << cntMaxOperationToMakeN_1_0(arr, N);
return 0;
}
Output: 
7

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Approach: Using the Two Pointers approach

Algorithm steps:

  • Sort the array arr in descending order using std::sort and the greater<int> comparator.
    Initialize two pointers, left and right, where left starts at index 0 and right starts at index N – 2 (since we need to make (N – 1) elements equal to 0).
  • Initialize a variable cntOp to keep track of the count of steps.
  • Iterate until the left and right pointers meet or cross each other.
  • In each iteration:
    • Subtract 1 from both elements pointed to by the left and right pointers.
    • If either element becomes non-positive:
    • Move the left pointer to the right (increment it) if the left element becomes non-positive.
    • Move the right pointer to the left (decrement it) if the right element becomes non-positive.
    • Increment the count of steps cntOp by 1.
  • After the iteration is complete, return the value of cntOp, which represents the maximum number of steps required to make (N – 1) elements equal to 0.

Below is the implementation of the above approach:

//C++ code for the above approach
#include <iostream>
#include <algorithm>
using namespace std;
// Function to count maximum number of steps
// to make (N - 1) array elements equal to 0
int cntMaxOperationToMakeN_1_0(int arr[], int N)
{
// Sort the array in descending order
sort(arr, arr + N, greater<int>());
// Initialize two pointers
int left = 0;
int right = N - 2; // (N - 1) elements to make 0
// Count the number of steps
int cntOp = 0;
// Perform steps until left and right pointers meet
while (left <= right) {
// Subtract 1 from both elements
arr[left]--;
arr[right]--;
// If either element becomes non-positive,
// move the pointer accordingly
if (arr[left] <= 0)
left++;
if (arr[right] <= 0)
right--;
// Increment the count of steps
cntOp++;
}
return cntOp;
}
// Driver Code
int main()
{
int arr[] = {1, 2, 3,4,5};
int N = sizeof(arr) / sizeof(arr[0]);
cout << cntMaxOperationToMakeN_1_0(arr, N);
return 0;
}
Output
7

Time Complexity: O(N log N) , because of the sorting operation.
Auxiliary Space: O(1) because the algorithm only uses a constant amount of additional space for the pointers and variables. 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK