Given an array arr[] and a number 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.
Examples:
Input : arr[] = { 3, 100, 1 }
k = 1
Output : 100, 1
Explanation : arr[0] < arr[1] means 3 is less than
100, so delete 3
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[] = { 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]
Approach: Stack is used to solving this problem. First we push arr[0] in stack S and then initialize count as 0, then after traverse a loop from 1 to n and then we check that s.top() < arr[i] if condition is true then we pop the element from stack and increase the count if count == k then we stop the loop and then store the value of stack in another array and then print that array.
C++
#include <bits/stdc++.h>
using namespace std;
void deleteElements( int arr[], int n, int k)
{
stack< int > s;
s.push(arr[0]);
int count = 0;
for ( int i=1; i<n; i++) {
while (!s.empty() && s.top() < arr[i]
&& count < k) {
s.pop();
count++;
}
s.push(arr[i]);
}
int m = s.size();
vector< int > v(m);
while (!s.empty()) {
v[--m] = s.top();
s.pop();
}
for ( auto x : v)
cout << x << " " ;
cout << endl;
}
int main()
{
int n = 5, k = 2;
int arr[] = {20, 10, 25, 30, 40};
deleteElements(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void deleteElements( int arr[], int n, int k) {
Stack<Integer> s = new Stack<>();
s.push(arr[ 0 ]);
int count = 0 ;
for ( int i = 1 ; i < n; i++) {
while (!s.empty() && s.peek() < arr[i]
&& count < k) {
s.pop();
count++;
}
s.push(arr[i]);
}
int m = s.size();
Integer[] v = new Integer[m];
while (!s.empty()) {
v[--m] = s.peek();
s.pop();
}
for (Integer x : v) {
System.out.print(x + " " );
};
System.out.println( "" );
}
public static void main(String[] args) {
int n = 5 , k = 2 ;
int arr[] = { 20 , 10 , 25 , 30 , 40 };
deleteElements(arr, n, k);
}
}
|
Python3
def deleteElements(arr, n, k):
st = []
st.append(arr[ 0 ])
top = 0
count = 0
for i in range ( 1 , n):
while ( len (st) ! = 0 and count < k
and st[top] < arr[i]):
st.pop()
count + = 1
top - = 1
st.append(arr[i])
top + = 1
for i in range ( 0 , len (st)):
print (st[i], " " , end = "")
k = 2
arr = [ 20 , 10 , 25 , 30 , 40 ]
deleteElements(arr, len (arr), k)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void deleteElements( int []arr, int n, int k)
{
Stack< int > s = new Stack< int >();
s.Push(arr[0]);
int count = 0;
for ( int i = 1; i < n; i++)
{
while (s.Count != 0 && s.Peek() < arr[i]
&& count < k)
{
s.Pop();
count++;
}
s.Push(arr[i]);
}
int m = s.Count;
int [] v = new int [m];
while (s.Count != 0)
{
v[--m] = s.Peek();
s.Pop();
}
foreach ( int x in v)
{
Console.Write(x + " " );
};
Console.Write( "" );
}
public static void Main()
{
int n = 5, k = 2;
int []arr = {20, 10, 25, 30, 40};
deleteElements(arr, n, k);
}
}
|
Javascript
<script>
function deleteElements(arr, n, k)
{
var s = [];
s.push(arr[0]);
var count = 0;
for ( var i = 1; i < n; i++)
{
while (s.length != 0 && s[s.length - 1] < arr[i] &&
count < k)
{
s.pop();
count++;
}
s.push(arr[i]);
}
var m = s.length;
var v = Array(m).fill(0);
while (s.length != 0)
{
m--;
v[m] = s[s.length - 1];
s.pop();
}
v.forEach(x => {
document.write(x + " " );
});
}
var n = 5, k = 2;
var arr = [ 20, 10, 25, 30, 40 ]
deleteElements(arr, n, k);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n + m)
Another Approach
In the previous approach, we used a stack that took an extra space. So in this approach, we are not going to use stack.
Here first we will store all elements of the array into a vector. Now we will run a for loop from 0 to (n-2)th index on that vector. So that in the worst case if we have to remove all the first n-1 elements then we can remove that. Now there will be a nested loop in that loop we checks if an element is less than its next element then that element will be deleted.
We have a count variable that is initially initialized to 0 and whenever we delete any element then we will increment the count by 1 and when the count will be equal to “k” then we will stop the loop and print that vector.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void deleteElements( int arr[], int n, int k)
{
vector< int > vec;
for ( int i=0;i<n;i++){
vec.push_back(arr[i]);
}
int count=0;
for ( int i=0;i<n-1;i++){
auto ptr=vec.begin();
for ( auto itr=ptr+1; itr!=vec.end();itr++){
if (*ptr<*itr){
count++;
vec.erase(ptr);
break ;
} else {
ptr++;
}
}
if (count==k){ break ;}
}
for ( auto x : vec)
cout << x << " " ;
cout << endl;
}
int main()
{
int n = 5, k = 2;
int arr[] = {20, 10, 25, 30, 40};
deleteElements(arr, n, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void deleteElements( int [] arr, int n,
int k)
{
ArrayList<Integer> vec = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
vec.add(arr[i]);
}
int count = 0 , i = 0 ;
while (i < n - 1 ) {
int ptr = 0 ;
for ( int j = 1 ; j < vec.size(); j++) {
if (vec.get(ptr) < vec.get(j)) {
count++;
vec.remove(ptr);
break ;
}
else {
ptr++;
}
}
if (count == k) {
break ;
}
i++;
}
for ( int x : vec) {
System.out.print(x + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int n = 5 , k = 2 ;
int [] arr = { 20 , 10 , 25 , 30 , 40 };
deleteElements(arr, n, k);
}
}
|
Python3
from typing import List
def deleteElements(arr: List [ int ], n: int , k: int ) - > None :
vec = []
for i in range (n):
vec.append(arr[i])
count = 0
i = 0
while i < n - 1 :
ptr = 0
for j in range ( 1 , len (vec)):
if vec[ptr] < vec[j]:
count + = 1
del vec[ptr]
break
else :
ptr + = 1
if count = = k:
break
i + = 1
for x in vec:
print (x, end = " " )
print ()
if __name__ = = '__main__' :
n = 5
k = 2
arr = [ 20 , 10 , 25 , 30 , 40 ]
deleteElements(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
class Program {
public static void deleteElements( int [] arr, int n, int k)
{
List< int > vec = new List< int >();
for ( int j = 0; j < n; j++) {
vec.Add(arr[j]);
}
int count = 0, i = 0;
while (i < n - 1) {
int ptr = 0;
for ( int j = 1; j < vec.Count; j++) {
if (vec[ptr] < vec[j]) {
count++;
vec.RemoveAt(ptr);
break ;
}
else {
ptr++;
}
}
if (count == k) {
break ;
}
i++;
}
foreach ( int x in vec) {
Console.Write(x + " " );
}
Console.WriteLine();
}
public static void Main( string [] args){
int n = 5, k = 2;
int [] arr = { 20, 10, 25, 30, 40 };
deleteElements(arr, n, k);
}
}
|
Javascript
function deleteElements(arr, n, k) {
let vec = [];
for (let i = 0; i < n; i++) {
vec.push(arr[i]);
}
let count = 0,
i = 0;
while (i < n - 1) {
let ptr = 0;
for (let j = 1; j < vec.length; j++) {
if (vec[ptr] < vec[j]) {
count++;
vec.splice(ptr, 1);
break ;
} else {
ptr++;
}
}
if (count == k) {
break ;
}
i++;
}
console.log(vec.join( " " ));
}
let n = 5,
k = 2;
let arr = [20, 10, 25, 30, 40];
deleteElements(arr, n, k);
|
Time Complexity: O(n2),because of nested for loops
Auxiliary Space: O(n),because of vector