-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindLongestSubsequence.cpp
More file actions
46 lines (44 loc) · 870 Bytes
/
findLongestSubsequence.cpp
File metadata and controls
46 lines (44 loc) · 870 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void printArray(vector<int> arr)
{
int n = arr.size();
int i = 0;
cout << endl
<< "ARRAY THUS FORMED IS : ";
while (i < n)
{
cout << arr[i++] << " ";
}
}
vector<int> longestSubsequence(vector<int> arr)
{
int n = arr.size();
vector<int> ans;
sort(arr.begin(), arr.end());
for (int i = 1; i < n; i++)
{
if (arr[i] == arr[i - 1])
{
ans.push_back(arr[i]);
}
}
return ans;
}
int main()
{
int n, i = 0, num;
cout << "Enter the number of Elements in Array : ";
cin >> n;
vector<int> arr;
cout << "Enter the Elements : ";
while (i < n)
{
cin >> num;
arr.push_back(num);
i++;
}
printArray(arr);
vector<int> ans = longestSubsequence(arr);
printArray(ans);
}