Given an array arr[] and an integer k. The task is to delete k elements which are smaller than next element (i.e., we delete arr[i] if arr[i] < arr[i+1]) or become smaller than next because next element is deleted.
Note: The deletion operation should start from left to right.
Example:Â
Input : arr[] = [20, 10, 25, 30, 40], k = 2
Output : 25 30 40
Explanation : First we delete 10 because it follows arr[i] < arr[i+1]. Then we delete 20 because 25 is moved next to it and it also starts following the condition.Input : arr[] = [3, 100, 1], k = 1
Output : 100 1
Explanation : arr[0] < arr[1] means 3 is less than 100, so delete 3Input: arr[] = [23, 45, 11, 77, 18], k = 3
Output : 77 18
Explanation : We delete 23, 45 and 11 as they follow the condition arr[i] < arr[i+1]
Using Stack - O(n) time and O(n) space
The idea is to use a stack to keep track of elements we want to retain while iterating through the input array. For each element, we compare it with the top of the stack. If the current element is greater than the stack's top element and we still have deletions available, we pop elements from the stack until this condition is no longer met.
Step by step approach:
- Create an empty stack to store elements.
- Iterate through each element in the input array.
- While the stack is not empty, current element > stack top, and k > 0, pop from stack and decrement k.
- Push the current element onto the stack.
- Convert the final stack to the result array (reversed since stack is LIFO).
// C++ program to delete Array Elements which
// are Smaller than Next or Become Smaller
#include <bits/stdc++.h>
using namespace std;
vector<int> deleteElement(vector<int> &arr, int k) {
int n = arr.size();
stack<int> st;
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than elements in stack
// and we still have k deletions left
while (!st.empty() && st.top() < arr[i] && k > 0) {
st.pop();
// Decrease the count of deletions
k--;
}
// Add current element to stack
st.push(arr[i]);
}
// Transfer remaining elements from stack to a vector
vector<int> ans(st.size());
for (int i = st.size() - 1; i >= 0; i--) {
ans[i] = st.top();
st.pop();
}
return ans;
}
int main() {
int k = 2;
vector<int> arr = {20, 10, 25, 30, 40};
vector<int> ans = deleteElement(arr, k);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
// Java program to delete Array Elements which
// are Smaller than Next or Become Smaller
import java.util.*;
class GfG {
static ArrayList<Integer> deleteElement(int[] arr, int k) {
int n = arr.length;
Stack<Integer> st = new Stack<>();
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than elements in stack
// and we still have k deletions left
while (!st.isEmpty() && st.peek() < arr[i] && k > 0) {
st.pop();
// Decrease the count of deletions
k--;
}
// Add current element to stack
st.push(arr[i]);
}
// Transfer remaining elements from stack to a list
ArrayList<Integer> ans = new ArrayList<>();
Stack<Integer> temp = new Stack<>();
while (!st.isEmpty()) {
temp.push(st.pop());
}
while (!temp.isEmpty()) {
ans.add(temp.pop());
}
return ans;
}
public static void main(String[] args) {
int k = 2;
int[] arr = {20, 10, 25, 30, 40};
ArrayList<Integer> ans = deleteElement(arr, k);
for (int val : ans) {
System.out.print(val + " ");
}
System.out.println();
}
}
# Python program to delete Array Elements which
# are Smaller than Next or Become Smaller
def deleteElement(arr, k):
n = len(arr)
st = []
# Process all elements in the array
for i in range(n):
# If current element is greater than elements in stack
# and we still have k deletions left
while st and st[-1] < arr[i] and k > 0:
st.pop()
# Decrease the count of deletions
k -= 1
# Add current element to stack
st.append(arr[i])
# Transfer remaining elements from stack to a list
ans = st.copy()
return ans
if __name__ == "__main__":
k = 2
arr = [20, 10, 25, 30, 40]
ans = deleteElement(arr, k)
for val in ans:
print(val, end=" ")
print()
// C# program to delete Array Elements which
// are Smaller than Next or Become Smaller
using System;
using System.Collections.Generic;
class GfG {
static List<int> deleteElement(int[] arr, int k) {
int n = arr.Length;
Stack<int> st = new Stack<int>();
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than elements in stack
// and we still have k deletions left
while (st.Count > 0 && st.Peek() < arr[i] && k > 0) {
st.Pop();
// Decrease the count of deletions
k--;
}
// Add current element to stack
st.Push(arr[i]);
}
// Transfer remaining elements from stack to a list
List<int> ans = new List<int>();
Stack<int> temp = new Stack<int>();
while (st.Count > 0) {
temp.Push(st.Pop());
}
while (temp.Count > 0) {
ans.Add(temp.Pop());
}
return ans;
}
static void Main(string[] args) {
int k = 2;
int[] arr = {20, 10, 25, 30, 40};
List<int> ans = deleteElement(arr, k);
foreach (int val in ans) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
// JavaScript program to delete Array Elements which
// are Smaller than Next or Become Smaller
function deleteElement(arr, k) {
let n = arr.length;
let st = [];
// Process all elements in the array
for (let i = 0; i < n; i++) {
// If current element is greater than elements in stack
// and we still have k deletions left
while (st.length > 0 && st[st.length - 1] < arr[i] && k > 0) {
st.pop();
// Decrease the count of deletions
k--;
}
// Add current element to stack
st.push(arr[i]);
}
// Transfer remaining elements from stack to an array
return st;
}
let k = 2;
let arr = [20, 10, 25, 30, 40];
let ans = deleteElement(arr, k);
for (let val of ans) {
process.stdout.write(val + " ");
}
console.log();
Output
25 30 40
Space Optimized - O(n) time and O(1) space
The idea for the single array approach is essentially the same, but we can directly use a vector as our answer array instead of a stack. Since we're building our result directly in the correct order, we don't need the extra step of transferring and reversing elements at the end, making this approach more efficient and cleaner.
// C++ program to delete Array Elements which
// are Smaller than Next or Become Smaller
#include <bits/stdc++.h>
using namespace std;
// Function to delete Array Elements which
// are Smaller than Next or Become Smaller
vector<int> deleteElement(vector<int> &arr, int k) {
int n = arr.size();
vector<int> ans;
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than last
// element in answer array and we still
// have k deletions left
while (!ans.empty() && ans.back() < arr[i] && k > 0) {
ans.pop_back();
// Decrease the count of deletions
k--;
}
// Add current element to answer array
ans.push_back(arr[i]);
}
return ans;
}
int main() {
int k = 2;
vector<int> arr = {20, 10, 25, 30, 40};
vector<int> ans = deleteElement(arr, k);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
// Java program to delete Array Elements which
// are Smaller than Next or Become Smaller
import java.util.*;
class GfG {
// Function to delete Array Elements which
// are Smaller than Next or Become Smaller
static ArrayList<Integer> deleteElement(int[] arr, int k) {
int n = arr.length;
ArrayList<Integer> ans = new ArrayList<>();
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than last
// element in answer array and we still
// have k deletions left
while (!ans.isEmpty() && ans.get(ans.size() - 1) < arr[i] && k > 0) {
ans.remove(ans.size() - 1);
// Decrease the count of deletions
k--;
}
// Add current element to answer array
ans.add(arr[i]);
}
return ans;
}
public static void main(String[] args) {
int k = 2;
int[] arr = {20, 10, 25, 30, 40};
ArrayList<Integer> ans = deleteElement(arr, k);
for (int val : ans) {
System.out.print(val + " ");
}
System.out.println();
}
}
# Python program to delete Array Elements which
# are Smaller than Next or Become Smaller
# Function to delete Array Elements which
# are Smaller than Next or Become Smaller
def deleteElement(arr, k):
n = len(arr)
ans = []
# Process all elements in the array
for i in range(n):
# If current element is greater than last
# element in answer array and we still
# have k deletions left
while ans and ans[-1] < arr[i] and k > 0:
ans.pop()
# Decrease the count of deletions
k -= 1
# Add current element to answer array
ans.append(arr[i])
return ans
if __name__ == "__main__":
k = 2
arr = [20, 10, 25, 30, 40]
ans = deleteElement(arr, k)
for val in ans:
print(val, end=" ")
print()
// C# program to delete Array Elements which
// are Smaller than Next or Become Smaller
using System;
using System.Collections.Generic;
class GfG {
// Function to delete Array Elements which
// are Smaller than Next or Become Smaller
static List<int> deleteElement(int[] arr, int k) {
int n = arr.Length;
List<int> ans = new List<int>();
// Process all elements in the array
for (int i = 0; i < n; i++) {
// If current element is greater than last
// element in answer array and we still
// have k deletions left
while (ans.Count > 0 && ans[ans.Count - 1] < arr[i] && k > 0) {
ans.RemoveAt(ans.Count - 1);
// Decrease the count of deletions
k--;
}
// Add current element to answer array
ans.Add(arr[i]);
}
return ans;
}
static void Main(string[] args) {
int k = 2;
int[] arr = {20, 10, 25, 30, 40};
List<int> ans = deleteElement(arr, k);
foreach (int val in ans) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
// JavaScript program to delete Array Elements which
// are Smaller than Next or Become Smaller
// Function to delete Array Elements which
// are Smaller than Next or Become Smaller
function deleteElement(arr, k) {
let n = arr.length;
let ans = [];
// Process all elements in the array
for (let i = 0; i < n; i++) {
// If current element is greater than last
// element in answer array and we still
// have k deletions left
while (ans.length > 0 && ans[ans.length - 1] < arr[i] && k > 0) {
ans.pop();
// Decrease the count of deletions
k--;
}
// Add current element to answer array
ans.push(arr[i]);
}
return ans;
}
let k = 2;
let arr = [20, 10, 25, 30, 40];
let ans = deleteElement(arr, k);
for (let val of ans) {
process.stdout.write(val + " ");
}
console.log();
Output
25 30 40