Skip to content

Commit 4062e76

Browse files
Merge pull request #497 from shivaabhishek07/sa-arr-cpp
Arranged all the cpp programs in a single file
2 parents dabcb62 + e78138a commit 4062e76

File tree

95 files changed

+891
-890
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+891
-890
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1-
#include <bits/stdc++.h>
2-
using namespace std;
3-
4-
int v; //No. of Vertices
5-
6-
void Add_edge(vector<int> adj[], int a, int b)
7-
{
8-
adj[a].push_back(b);
9-
}
10-
11-
/************************************* ALGORITHM FOR BREADTH FIRST SEARCH ******************************************************/
12-
13-
// Take the empty queue and bool type array (visit) initialise with FALSE.
14-
// Push the starting node in the queue and set the value TRUE for this node in visited array.
15-
// Pop out the front node of the queue and print the node.
16-
// Push the adjacent node of pop node in queue which are not visited. Set the value TRUE in visited array of adding node.
17-
// Repeat step 3 and 4 until the queue becomes empty.
18-
19-
void bfs(int n, vector<int>adj[])
20-
{
21-
vector<bool> visit(v,false);
22-
queue<int> q;
23-
q.push(n);
24-
visit[n]=true;
25-
while(!q.empty())
26-
{
27-
n=q.front();
28-
cout<<n<<" ";
29-
q.pop();
30-
for(int i=0;i<adj[n].size();i++)
31-
{
32-
if(!visit[adj[n][i]])
33-
{
34-
q.push(adj[n][i]);
35-
visit[adj[n][i]]=true;
36-
}
37-
}
38-
}
39-
}
40-
41-
/************************************* ALGORITHM FOR DEPTH FIRST SEARCH ******************************************************/
42-
43-
// Take the empty stack and bool type array (visit) initialise with FALSE.
44-
// Push the starting node in the stack and set the value TRUE for this node in visited array.
45-
// Pop the top node from the stack and print that node.
46-
// Push the adjacent node of pop node in the stack which is not visited. Set the value TRUE in visited array of adding node.
47-
// Repeat step 3 and 4 until the stack becomes empty.
48-
49-
50-
void dfs(int n, vector<int> adj[])
51-
{
52-
vector<bool> visit(v,false);
53-
stack<int> s;
54-
s.push(n);
55-
visit[n]=true;
56-
while(!s.empty())
57-
{
58-
n=s.top();
59-
cout<<n<<" ";
60-
s.pop();
61-
for(int i=0;i<adj[n].size();i++)
62-
{
63-
if(!visit[adj[n][i]])
64-
{
65-
s.push(adj[n][i]);
66-
visit[adj[n][i]]=true;
67-
}
68-
}
69-
}
70-
71-
}
72-
73-
int main()
74-
{
75-
cout<<"Enter the no. of vertices : ";
76-
cin>>v;
77-
vector<int> adj[v];
78-
vector<bool> visit(v,false);
79-
Add_edge(adj,0,1);
80-
Add_edge(adj,0,2);
81-
Add_edge(adj,1,2);
82-
Add_edge(adj,2,0);
83-
Add_edge(adj,2,3);
84-
Add_edge(adj,3,3);
85-
86-
int temp;
87-
cout<<"Choose the vertex from you want to start traversing : ";
88-
cin>>temp;
89-
cout<<"\nBFS traversal is"<<" ";
90-
bfs(temp,adj);
91-
cout<<"\nDFS traversal is"<<" ";
92-
dfs(temp,adj);
93-
94-
return 0;
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int v; //No. of Vertices
5+
6+
void Add_edge(vector<int> adj[], int a, int b)
7+
{
8+
adj[a].push_back(b);
9+
}
10+
11+
/************************************* ALGORITHM FOR BREADTH FIRST SEARCH ******************************************************/
12+
13+
// Take the empty queue and bool type array (visit) initialise with FALSE.
14+
// Push the starting node in the queue and set the value TRUE for this node in visited array.
15+
// Pop out the front node of the queue and print the node.
16+
// Push the adjacent node of pop node in queue which are not visited. Set the value TRUE in visited array of adding node.
17+
// Repeat step 3 and 4 until the queue becomes empty.
18+
19+
void bfs(int n, vector<int>adj[])
20+
{
21+
vector<bool> visit(v,false);
22+
queue<int> q;
23+
q.push(n);
24+
visit[n]=true;
25+
while(!q.empty())
26+
{
27+
n=q.front();
28+
cout<<n<<" ";
29+
q.pop();
30+
for(int i=0;i<adj[n].size();i++)
31+
{
32+
if(!visit[adj[n][i]])
33+
{
34+
q.push(adj[n][i]);
35+
visit[adj[n][i]]=true;
36+
}
37+
}
38+
}
39+
}
40+
41+
/************************************* ALGORITHM FOR DEPTH FIRST SEARCH ******************************************************/
42+
43+
// Take the empty stack and bool type array (visit) initialise with FALSE.
44+
// Push the starting node in the stack and set the value TRUE for this node in visited array.
45+
// Pop the top node from the stack and print that node.
46+
// Push the adjacent node of pop node in the stack which is not visited. Set the value TRUE in visited array of adding node.
47+
// Repeat step 3 and 4 until the stack becomes empty.
48+
49+
50+
void dfs(int n, vector<int> adj[])
51+
{
52+
vector<bool> visit(v,false);
53+
stack<int> s;
54+
s.push(n);
55+
visit[n]=true;
56+
while(!s.empty())
57+
{
58+
n=s.top();
59+
cout<<n<<" ";
60+
s.pop();
61+
for(int i=0;i<adj[n].size();i++)
62+
{
63+
if(!visit[adj[n][i]])
64+
{
65+
s.push(adj[n][i]);
66+
visit[adj[n][i]]=true;
67+
}
68+
}
69+
}
70+
71+
}
72+
73+
int main()
74+
{
75+
cout<<"Enter the no. of vertices : ";
76+
cin>>v;
77+
vector<int> adj[v];
78+
vector<bool> visit(v,false);
79+
Add_edge(adj,0,1);
80+
Add_edge(adj,0,2);
81+
Add_edge(adj,1,2);
82+
Add_edge(adj,2,0);
83+
Add_edge(adj,2,3);
84+
Add_edge(adj,3,3);
85+
86+
int temp;
87+
cout<<"Choose the vertex from you want to start traversing : ";
88+
cin>>temp;
89+
cout<<"\nBFS traversal is"<<" ";
90+
bfs(temp,adj);
91+
cout<<"\nDFS traversal is"<<" ";
92+
dfs(temp,adj);
93+
94+
return 0;
9595
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
#include <iostream>
2-
using namespace std;
3-
4-
// Method 1
5-
bool Palindrome(string s,int start,int end){
6-
if(start>=end){
7-
return true;
8-
}
9-
return (s[start]==s[end])&&(Palindrome(s,start+1,end-1));
10-
}
11-
12-
// Method 2
13-
void pal(string s){
14-
int i=0,j=s.length()-1;
15-
while(i<=j){
16-
if(s[i]!=s[j]){
17-
cout<<"String is not Palindrome";
18-
return ;
19-
}
20-
i++,j--;
21-
}
22-
cout<<"String is palindrome";
23-
return ;
24-
}
25-
26-
int main(){
27-
string s;
28-
cin>>s;
29-
cout<<s.length()<<endl;
30-
cout<<Palindrome(s,0,s.length()-1);
31-
cout<<endl;
32-
pal(s);
33-
34-
return 0;
1+
#include <iostream>
2+
using namespace std;
3+
4+
// Method 1
5+
bool Palindrome(string s,int start,int end){
6+
if(start>=end){
7+
return true;
8+
}
9+
return (s[start]==s[end])&&(Palindrome(s,start+1,end-1));
10+
}
11+
12+
// Method 2
13+
void pal(string s){
14+
int i=0,j=s.length()-1;
15+
while(i<=j){
16+
if(s[i]!=s[j]){
17+
cout<<"String is not Palindrome";
18+
return ;
19+
}
20+
i++,j--;
21+
}
22+
cout<<"String is palindrome";
23+
return ;
24+
}
25+
26+
int main(){
27+
string s;
28+
cin>>s;
29+
cout<<s.length()<<endl;
30+
cout<<Palindrome(s,0,s.length()-1);
31+
cout<<endl;
32+
pal(s);
33+
34+
return 0;
3535
}
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
class Solution {
2-
public:
3-
int solvetab(vector<int> &nums)
4-
{
5-
vector<vector<int>> dp(nums.size()+3,vector<int>(nums.size()+3,0));
6-
for(int curr= nums.size()-1;curr>=0;curr--)
7-
{
8-
for(int prev=curr-1;prev>=-1;prev--)
9-
{
10-
11-
int inc=0;
12-
//include
13-
if(prev==-1||nums[curr]>nums[prev])
14-
{
15-
inc= 1+dp[curr+1][curr+1];
16-
}
17-
int excl=dp[curr+1][prev+1];
18-
dp[curr][prev+1]=max(inc,excl);
19-
20-
}
21-
}
22-
return dp[0][0];
23-
}
24-
int solve(vector<int> &nums,int i,int prev,vector<vector<int>> &dp)
25-
{
26-
//base condition
27-
if(i>=nums.size())
28-
{
29-
return 0;
30-
}
31-
if(dp[i][prev+1]!=-1)
32-
{
33-
return dp[i][prev+1];
34-
}
35-
//exclude
36-
int excl=solve(nums,i+1,prev,dp);
37-
int inc=0;
38-
//include
39-
if(prev==-1||nums[i]>nums[prev])
40-
{
41-
inc= 1+solve(nums,i+1,i,dp);
42-
}
43-
44-
return dp[i][prev+1]=max(inc,excl);
45-
46-
}
47-
int lengthOfLIS(vector<int>& nums) {
48-
// vector<vector<int>> dp(nums.size()+3,vector<int>(nums.size()+3,-1));
49-
// return solve(nums,0,-1,dp);
50-
return solvetab(nums);
51-
52-
}
1+
class Solution {
2+
public:
3+
int solvetab(vector<int> &nums)
4+
{
5+
vector<vector<int>> dp(nums.size()+3,vector<int>(nums.size()+3,0));
6+
for(int curr= nums.size()-1;curr>=0;curr--)
7+
{
8+
for(int prev=curr-1;prev>=-1;prev--)
9+
{
10+
11+
int inc=0;
12+
//include
13+
if(prev==-1||nums[curr]>nums[prev])
14+
{
15+
inc= 1+dp[curr+1][curr+1];
16+
}
17+
int excl=dp[curr+1][prev+1];
18+
dp[curr][prev+1]=max(inc,excl);
19+
20+
}
21+
}
22+
return dp[0][0];
23+
}
24+
int solve(vector<int> &nums,int i,int prev,vector<vector<int>> &dp)
25+
{
26+
//base condition
27+
if(i>=nums.size())
28+
{
29+
return 0;
30+
}
31+
if(dp[i][prev+1]!=-1)
32+
{
33+
return dp[i][prev+1];
34+
}
35+
//exclude
36+
int excl=solve(nums,i+1,prev,dp);
37+
int inc=0;
38+
//include
39+
if(prev==-1||nums[i]>nums[prev])
40+
{
41+
inc= 1+solve(nums,i+1,i,dp);
42+
}
43+
44+
return dp[i][prev+1]=max(inc,excl);
45+
46+
}
47+
int lengthOfLIS(vector<int>& nums) {
48+
// vector<vector<int>> dp(nums.size()+3,vector<int>(nums.size()+3,-1));
49+
// return solve(nums,0,-1,dp);
50+
return solvetab(nums);
51+
52+
}
5353
};

0 commit comments

Comments
 (0)