Related Articles

# Minimize operations to make all array elements -1 by changing maximums of K-size subarray to -1

• Difficulty Level : Hard
• Last Updated : 07 Oct, 2021

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:

1. Choosing the sub array from index 0 to 2 and by applying the operation, modifies the array to {-1, 11, -1, 11, 18}.
2. Choosing the sub array form index 1 to 3 and by applying the operation, modifies the array to {-1, -1, -1, -1, 18}.
3. 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 ``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 > 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);` `    ``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 vp = ``new` `Vector();``    ``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``    ``{``        ``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 vp = ``new` `List();``    ``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

 ``
Output:
`3`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up