Skip to content

Commit bc48a25

Browse files
authored
Merge pull request #176 from satyam-chaudhari/master
Added solutions to different patterns and Hacker Rank Problem Solution
2 parents 818e380 + 52370ab commit bc48a25

File tree

9 files changed

+328
-0
lines changed

9 files changed

+328
-0
lines changed

Hackerrank/c++/Day_15_Linked_List.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <cstddef>
3+
using namespace std;
4+
class Node
5+
{
6+
public:
7+
int data;
8+
Node *next;
9+
Node(int d){
10+
data=d;
11+
next=NULL;
12+
}
13+
};
14+
class Solution{
15+
public:
16+
17+
Node* insert(Node *head,int data)
18+
{
19+
//Complete this method
20+
Node* newNode = new Node(data);
21+
Node* tail = head;
22+
23+
if(head == NULL)
24+
{
25+
return newNode;
26+
}
27+
else
28+
{
29+
for (;tail->next ; tail = tail->next);
30+
tail->next = newNode;
31+
return head;
32+
}
33+
}
34+
35+
void display(Node *head)
36+
{
37+
Node *start=head;
38+
while(start)
39+
{
40+
cout<<start->data<<" ";
41+
start=start->next;
42+
}
43+
}
44+
};
45+
int main()
46+
{
47+
#ifndef ONLINE_JUDGE
48+
freopen("input.txt","r",stdin);
49+
freopen("output.txt","w",stdout);
50+
#endif
51+
52+
Node* head=NULL;
53+
Solution mylist;
54+
int T,data;
55+
cin>>T;
56+
while(T-->0){
57+
cin>>data;
58+
head=mylist.insert(head,data);
59+
}
60+
mylist.display(head);
61+
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k=1;
7+
cin>>n;
8+
9+
for (i=1 ; i<=n ; i++)
10+
{
11+
for(j=1 ; j<=i ;j++)
12+
{
13+
cout<<k<<" ";
14+
k++;
15+
}
16+
cout<<endl;
17+
}
18+
19+
return 0;
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k=1;
7+
cin>>n;
8+
9+
for (i=1 ; i<=n ; i++)
10+
{
11+
for(j=1 ; j<=i ; j++)
12+
{
13+
cout<<"*"<<" ";
14+
}
15+
cout<<endl;
16+
}
17+
i=0;j=0;
18+
for(i=n-1 ; i>=1 ; i--)
19+
{
20+
for(j=1 ; j<=i ; j++)
21+
{
22+
cout<<"*"<<" ";
23+
}
24+
cout<<endl;
25+
}
26+
27+
28+
return 0;
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k=1;
7+
cin>>n;
8+
9+
for(i=1, k=0 ; i<=n ; i++, k=0)
10+
{
11+
for(j=1 ; j<=(n-i) ; j++)
12+
{
13+
cout<<" ";
14+
}
15+
while(k != (2*i)-1 )
16+
{
17+
cout<<"*";
18+
k++;
19+
}
20+
cout<<endl;
21+
}
22+
for(i=n-1 , k=0 ; i>=1 ; i--, k=0)
23+
{
24+
for(j=1 ; j<=(n-i) ; j++)
25+
{
26+
cout<<" ";
27+
}
28+
while(k != (2*i)-1 )
29+
{
30+
cout<<"*";
31+
k++;
32+
}
33+
cout<<endl;
34+
}
35+
36+
return 0;
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k;
7+
cin>>n;
8+
9+
for(i=1 ; i<=n ; i++)
10+
{
11+
for(j=1 ; j<=n ; j++)
12+
{
13+
if((i==1) || (i==n) || (j==1) || (j==n))
14+
cout<<"* ";
15+
else
16+
cout<<" ";
17+
}
18+
cout<<endl;
19+
}
20+
21+
// for(i=1 ; i<=n ; i++)
22+
// {
23+
// if(i==1 || i==n)
24+
// {
25+
// for(j=1 ; j<=n ; j++)
26+
// {
27+
// cout<<"*";
28+
// }
29+
// cout<<endl;
30+
// }
31+
// else
32+
// {
33+
// cout<<"*";
34+
// for(k=1 ; k<=n-2 ; k++)
35+
// cout<<" ";
36+
// cout<<"*"<<endl;
37+
// }
38+
// }
39+
return 0;
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k;
7+
cin>>n;
8+
9+
for(i=0 ; i<n ; i++)
10+
{
11+
k=i;
12+
while (k)
13+
{
14+
cout<<" ";
15+
k--;
16+
}
17+
for(j=0 ; j<n-i ; j++)
18+
{
19+
cout<<"* ";
20+
}
21+
cout<<endl;
22+
}
23+
24+
return 0;
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k;
7+
cin>>n;
8+
9+
for(i=1 ; i<=n ; i++)
10+
{
11+
// k=n-i;
12+
// while (k)
13+
// {
14+
// cout<<" ";
15+
// k--;
16+
// }
17+
// for(j=1 ; j<=i ; j++)
18+
// {
19+
// cout<<"* ";
20+
// }
21+
// cout<<endl;
22+
k=0;
23+
while (k != (n-i))
24+
{
25+
cout<<" ";
26+
k++;
27+
}
28+
for ( j = 1; j <= (2*i)-1 ; j++)
29+
{
30+
if((j%2)==0)
31+
cout<<" ";
32+
else
33+
cout<<"*";
34+
}
35+
cout<<endl;
36+
37+
}
38+
39+
return 0;
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,i,j,k;
7+
cin>>n;
8+
9+
for(i=1 ; i<=n ; i++)
10+
{
11+
k=n-i;
12+
while (k)
13+
{
14+
cout<<" ";
15+
k--;
16+
}
17+
for(j=1 ; j<=i ; j++)
18+
{
19+
cout<<"* ";
20+
}
21+
cout<<endl;
22+
}
23+
24+
for(i=1 ; i<=n-1 ; i++)
25+
{
26+
k=i;
27+
while (k)
28+
{
29+
cout<<" ";
30+
k--;
31+
}
32+
for(j=1 ; j<=n-i ; j++)
33+
{
34+
cout<<"* ";
35+
}
36+
cout<<endl;
37+
}
38+
39+
return 0;
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int i,j,k,n;
7+
cin>>n;
8+
9+
for (i = 0; i <= n/2 ; i++)
10+
{
11+
for(j=0 ; j<n ; j++)
12+
{
13+
if((j >= (n/2)-i) && (j <= (n/2)+i))
14+
cout<<" ";
15+
else
16+
cout<<"* ";
17+
}
18+
cout<<endl;
19+
}
20+
21+
for (i = 0; i < n/2 ; i++)
22+
{
23+
for(j=0 ; j<n ; j++)
24+
{
25+
if((j <= i) || (j >= (n-i-1)))
26+
cout<<"* ";
27+
else
28+
cout<<" ";
29+
}
30+
cout<<endl;
31+
}
32+
33+
34+
return 0;
35+
}

0 commit comments

Comments
 (0)