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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int N, int K)
{
vector<pair< int , int > > vp;
for ( int i = 0; i < N; i++) {
vp.push_back({ arr[i], i });
}
sort(vp.begin(), vp.end());
int minCnt = 0;
while (!vp.empty()) {
int val, ind;
val = vp.back().first;
ind = vp.back().second;
minCnt++;
while (!vp.empty()
&& vp.back().first == val
&& ind - vp.back().second + 1 <= K)
vp.pop_back();
}
return minCnt;
}
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
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int minOperations( int arr[], int N, int K)
{
Vector<pair> vp = new Vector<pair>();
for ( int i = 0 ; i < N; i++) {
vp.add( new pair( arr[i], i ));
}
Collections.sort(vp,(a,b)->a.first-b.first);
int minCnt = 0 ;
while (!vp.isEmpty()) {
int val, ind;
val = vp.get(vp.size()- 1 ).first;
ind = vp.get(vp.size()- 1 ).second;
minCnt++;
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 minCnt;
}
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));
}
}
|
Python3
def minOperations(arr, N, K):
vp = []
for i in range (N):
vp.append([arr[i], i])
vp.sort()
minCnt = 0
while ( len (vp) ! = 0 ):
val = vp[ - 1 ][ 0 ]
ind = vp[ - 1 ][ 1 ]
minCnt + = 1
while ( len (vp) ! = 0
and vp[ - 1 ][ 0 ] = = val
and ind - vp[ - 1 ][ 1 ] + 1 < = K):
vp.pop()
return minCnt
if __name__ = = "__main__" :
arr = [ 18 , 11 , 18 , 11 , 18 ]
K = 3
N = len (arr)
print (minOperations(arr, N, K))
|
C#
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;
}
}
static int minOperations( int []arr, int N, int K)
{
List<pair> vp = new List<pair>();
for ( int i = 0; i < N; i++) {
vp.Add( new pair( arr[i], i ));
}
vp.Sort();
int minCnt = 0;
while (vp.Count!=0) {
int val, ind;
val = vp[vp.Count-1].first;
ind = vp[vp.Count-1].second;
minCnt++;
while (vp.Count!=0
&& vp[vp.Count-1].first == val
&& ind - vp[vp.Count-1].second + 1 <= K)
vp.RemoveAt(vp.Count-1);
}
return minCnt;
}
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));
}
}
|
Javascript
<script>
function minOperations(arr, N, K)
{
let vp = [];
for (let i = 0; i < N; i++)
{
vp.push({ "first" : arr[i], "second" : i });
}
vp.sort( function (a, b) { return a.first - b.first; });
let minCnt = 0;
while (vp.length > 0) {
let val, ind;
val = vp[vp.length - 1].first;
ind = vp[vp.length - 1].second;
minCnt++;
while (vp.length > 0
&& (vp[vp.length - 1].first == val)
&& (ind - vp[vp.length - 1].second + 1 <= K)) {
vp.pop();
}
}
return minCnt;
}
let arr = [18, 11, 18, 11, 18];
let K = 3;
let N = arr.length;
document.write(minOperations(arr, N, K));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...