-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminInsertionsToMakeASortedSequence.cpp
More file actions
57 lines (46 loc) · 1.08 KB
/
minInsertionsToMakeASortedSequence.cpp
File metadata and controls
57 lines (46 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int minDeletions(int arr[], int n)
{
if(n==1)
return 0;
int dp[n]={1};
for(int i=1;i<n;i++)
dp[i]=1;
int res=dp[1];
for(int i=1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(arr[i]>arr[j] and dp[j]+1>dp[i])
{
dp[i]=dp[j]+1;
res=max(dp[i],res);
}
}
}
return n-res;
}
};
// { Driver Code Starts.
int main()
{
//Given an array arr of size N, the task is to remove or delete the minimum number of elements from
//the array so that when the remaining elements are placed in the same sequence order form an increasing sorted sequence.
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
Solution ob;
cout << ob.minDeletions(arr, n) << "\n";
}
return 0;
} // } Driver Code Ends