-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG LL-4 RotateInGroups.cpp
125 lines (100 loc) · 1.5 KB
/
GFG LL-4 RotateInGroups.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <iostream>
#include <bits/stdc++.h>
#include <map>
#include <string.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *head;
void newnode(int t)
{
struct node *temp = new node;
temp->data = t;
temp -> next = head;
head = temp;
}
void display()
{
struct node *curr;
curr=head;
while(curr!=NULL)
{
cout << curr->data <<" ";
curr=curr-> next;
}
cout << endl;
}
void reversee()
{
struct node *temp,*curr, *prev;
prev=NULL;
curr=head;
while(curr!=NULL)
{
temp=curr->next;
curr->next=prev;
prev=curr;
curr=temp;
}
head = prev;
display();
}
int main()
{
int n,k;
cin >>n;
vector <vector<int> > v;
for (int i=0;i<n;i++)
{
cin >> k;
newnode(k);
}
cin >>k;
k=k%n;
reversee();
struct node *curr;
curr = head;
int i=0,j=0,t=0;
v.push_back(vector<int>());
while(t<n)
{
t++;
v[i].push_back(curr->data);
curr=curr->next;
if (t%k==0)
{
i++;
v.push_back(vector<int>());
}
}
t=0; i=0;
while (t<n)
{
cout << v[i][j++] <<" ";
if (j==k)
{
i++; j=0;
}
t++;
}
cout << endl;
i=0; t=0;
curr=head;
while (t<n)
{
j=v[i].size() - 1;
while (j>=0)
{
curr->data = v[i][j--];
curr=curr->next;
t++;
}
i++;
}
display();
return 0;
}