Minimize operations to make all array elements -1 by changing maximums of K-size subarray to -1
Last Updated :
05 Dec, 2022
Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum of operations required to make all the array elements -1 such that in each operation, choose a subarray of size K and change all the maximum element in the subarray to -1.
Examples:
Input: arr[] = {18, 11, 18, 11, 18}, K = 3
Output: 3
Explanation:
Following are the operations performed:
- Choosing the sub array from index 0 to 2 and by applying the operation, modifies the array to {-1, 11, -1, 11, 18}.
- Choosing the sub array form index 1 to 3 and by applying the operation, modifies the array to {-1, -1, -1, -1, 18}.
- Choosing the sub array form index 2 to 4 and by applying the operation, modifies the array to {-1, -1, -1, -1, -1}.
After the above operations all the array elements become -1. Therefore, the minimum number of operations required is 3.
Input: arr[] = {2, 1, 1}, K = 2
Output: 2
Approach: The given problem can be solved by sorting the array arr[] with indices and then counting the number of operations by choosing the array elements from the end where the difference between the indices is less than K. Follow the below steps to solve the problem:
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
int minOperations(int arr[], int N, int K)
{
// Stores the array elements with
// their corresponding indices
vector<pair<int, int> > vp;
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.push_back({ arr[i], i });
}
// Sort the elements according
// to it's first value
sort(vp.begin(), vp.end());
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (!vp.empty()) {
int val, ind;
// Stores the first value of vp
val = vp.back().first;
// Stores the second value of vp
ind = vp.back().second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (!vp.empty()
&& vp.back().first == val
&& ind - vp.back().second + 1 <= K)
vp.pop_back();
}
// Return the minCnt
return minCnt;
}
// Driver Code
int main()
{
int arr[] = { 18, 11, 18, 11, 18 };
int K = 3;
int N = sizeof(arr) / sizeof(arr[0]);
cout << minOperations(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
static int minOperations(int arr[], int N, int K)
{
// Stores the array elements with
// their corresponding indices
Vector<pair> vp = new Vector<pair>();
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.add(new pair( arr[i], i ));
}
// Sort the elements according
// to it's first value
Collections.sort(vp,(a,b)->a.first-b.first);
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (!vp.isEmpty()) {
int val, ind;
// Stores the first value of vp
val = vp.get(vp.size()-1).first;
// Stores the second value of vp
ind = vp.get(vp.size()-1).second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (!vp.isEmpty()
&& vp.get(vp.size()-1).first == val
&& ind - vp.get(vp.size()-1).second + 1 <= K)
vp.remove(vp.size()-1);
}
// Return the minCnt
return minCnt;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 18, 11, 18, 11, 18 };
int K = 3;
int N = arr.length;
System.out.print(minOperations(arr, N, K));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
# Function to find minimum the number
# of operations required to make all
# the array elements to -1
def minOperations(arr, N, K):
# Stores the array elements with
# their corresponding indices
vp = []
for i in range(N):
# Push the array element
# and it's index
vp.append([arr[i], i])
# Sort the elements according
# to it's first value
vp.sort()
# Stores the minimum number of
# operations required
minCnt = 0
# Traverse until vp is not empty
while (len(vp) != 0):
# Stores the first value of vp
val = vp[-1][0]
# Stores the second value of vp
ind = vp[-1][1]
# Update the minCnt
minCnt += 1
# Pop the back element from the
# vp until the first value is
# same as val and difference
# between indices is less than K
while (len(vp) != 0
and vp[-1][0] == val
and ind - vp[-1][1] + 1 <= K):
vp.pop()
# Return the minCnt
return minCnt
# Driver Code
if __name__ == "__main__":
arr = [18, 11, 18, 11, 18]
K = 3
N = len(arr)
print(minOperations(arr, N, K))
# This code is contributed by mukesh07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
class pair : IComparable<pair>
{
public int first,second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair p)
{
return this.first - p.first;
}
}
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
static int minOperations(int []arr, int N, int K)
{
// Stores the array elements with
// their corresponding indices
List<pair> vp = new List<pair>();
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.Add(new pair( arr[i], i ));
}
// Sort the elements according
// to it's first value
vp.Sort();
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (vp.Count!=0) {
int val, ind;
// Stores the first value of vp
val = vp[vp.Count-1].first;
// Stores the second value of vp
ind = vp[vp.Count-1].second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (vp.Count!=0
&& vp[vp.Count-1].first == val
&& ind - vp[vp.Count-1].second + 1 <= K)
vp.RemoveAt(vp.Count-1);
}
// Return the minCnt
return minCnt;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 18, 11, 18, 11, 18 };
int K = 3;
int N = arr.Length;
Console.Write(minOperations(arr, N, K));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
function minOperations(arr, N, K)
{
// Stores the array elements with
// their corresponding indices
let vp = [];
for (let i = 0; i < N; i++)
{
// Push the array element
// and it's index
vp.push({ "first": arr[i], "second": i });
}
// Sort the elements according
// to it's first value
vp.sort(function (a, b) { return a.first - b.first; });
// Stores the minimum number of
// operations required
let minCnt = 0;
// Traverse until vp is not empty
while (vp.length > 0) {
let val, ind;
// Stores the first value of vp
val = vp[vp.length - 1].first;
// Stores the second value of vp
ind = vp[vp.length - 1].second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (vp.length > 0
&& (vp[vp.length - 1].first == val)
&& (ind - vp[vp.length - 1].second + 1 <= K)) {
vp.pop();
}
}
// Return the minCnt
return minCnt;
}
// Driver Code
let arr = [18, 11, 18, 11, 18];
let K = 3;
let N = arr.length;
document.write(minOperations(arr, N, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Similar Reads
Minimum number of operations to make maximum element of every subarray of size K at least X Given an array A[] of size N and an integer K, find minimum number of operations to make maximum element of every subarray of size K at least X. In one operation, we can increment any element of the array by 1. Examples: Input: A[] = {2, 3, 0, 0, 2}, K = 3, X =4Output: 3Explanation: Perform followin
7 min read
Sum of minimum and maximum elements of all subarrays of size k. Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.Examples: Input : arr[] = {2, 5, -1, 7, -3, -1, -2} K = 4Output : 18Explanation : Subarrays of size 4 are : {2, 5, -1, 7}, min + max = -1 + 7 = 6 {5, -1, 7, -3
15+ min read
Minimize flips on K-length subarrays required to make all array elements equal to 1 Given a binary array arr[] of size N and a positive integer K, the task is to find the minimum number of times any subarray of size K from the given array arr[] is required to be flipped to make all array elements equal to 1. If it is not possible to do so, then print "-1". Examples: Input: arr[] =
15+ min read
Minimum number of subtract operation to make an array decreasing You are given a sequence of numbers arr[0], arr[1], ..., arr[N - 1] and a positive integer K. In each operation, you may subtract K from any element of the array. You are required to find the minimum number of operations to make the given array decreasing. An array arr[0], arr[1], ....., arr[N-1] is
7 min read
Minimum number of operations required to make all the elements positive Given an array A[] of length N. You can apply the below operation: Choose an index i such that (i+1) element exists. After that choose any number X (positive or negative). Then subtract X from the ith element and add it into (i+1)th element.The task is to find the minimum number of operations requir
9 min read