Skip to content

Commit 91be816

Browse files
authored
Merge pull request ashutosh97#113 from sakshamb2113/master
Added hot potato problem
2 parents aaee040 + f1e9b90 commit 91be816

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

hot_potato/hot_potato.cpp

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<class Item>
5+
class LinearList{
6+
private:
7+
int MaxSize;
8+
Item *element; // 1D dynamic array
9+
int len;
10+
public:
11+
/* Default constructor.
12+
* Should create an empty list that not contain any elements*/
13+
LinearList()
14+
{
15+
element=nullptr;
16+
len=0;
17+
};
18+
19+
/* This constructor should create a list containing MaxSize elements. You can intialize the elements with any values.*/
20+
LinearList(const int& MaxListSize)
21+
{
22+
MaxSize=MaxListSize;
23+
element = new Item[MaxSize];
24+
len=0;
25+
/*for(int i=0;i<MaxSize;i++)
26+
{
27+
cout << element[i] << endl;
28+
}*/
29+
30+
};
31+
32+
/* Destructor.
33+
* Must free all the memory contained by the list */
34+
~LinearList(){};
35+
36+
/* Indexing operator.
37+
* It should behave the same way array indexing does. i.e,
38+
* LinearList L;
39+
* L[0] should refer to the first element;
40+
* L[1] to the second element and so on.
41+
* */
42+
Item& operator[](const int& i)
43+
{
44+
return element[i];
45+
}; //return i'th element of list
46+
47+
/* Returns true if the list is empty, false otherwise.
48+
* */
49+
bool isEmpty()
50+
{
51+
if(len==0)
52+
{
53+
return true;
54+
}
55+
else
56+
{
57+
return false;
58+
}
59+
};
60+
61+
/* Returns the actual length (number of elements) in the list.
62+
* */
63+
int length()
64+
{
65+
return len;
66+
};
67+
68+
/* Returns the maximum size of the list.
69+
* */
70+
int maxSize()
71+
{
72+
return maxSize;
73+
};
74+
75+
/* Returns the k-th element of the list.
76+
* */
77+
Item returnListElement(const int k)
78+
{
79+
return element[k-1];
80+
};
81+
/* Set x to the k-th element and
82+
* return true if k-th element is present otherwise return false.
83+
* */
84+
bool find(const int k, Item& x)
85+
{
86+
if(x==element[k-1])
87+
{
88+
return true;
89+
}
90+
else
91+
{
92+
93+
return false;
94+
}
95+
};
96+
97+
/* Search for x and
98+
* return the position if found, else return 0.
99+
* */
100+
int search(Item& x)
101+
{
102+
for(int i=0;i<len;i++)
103+
{
104+
if(element[i]==x)
105+
{
106+
return i+1;
107+
}
108+
}
109+
return 0;
110+
111+
};
112+
113+
/* Set x to the k-th element and delete that k-th element.
114+
* */
115+
void deleteElement(const int k, Item& x)
116+
{
117+
x=element[k-1];
118+
for(int i=k;i<len;i++)
119+
{
120+
element[i-1]=element[i];
121+
}
122+
len=len-1;
123+
};
124+
125+
/* Insert x after k-th element.
126+
* */
127+
void insert(const int k, Item& x)
128+
{
129+
for(int i=len;i>k;i--)
130+
{
131+
element[i]=element[i-1];
132+
}
133+
element[k]=x;
134+
len=len+1;
135+
};
136+
137+
138+
};
139+
140+
int main()
141+
{
142+
LinearList<int> a(250);
143+
int k,n,i,j,temp;
144+
cout << "Enter number of children: ";
145+
cin >> n;
146+
cout << "Enter elimination rule: ";
147+
cin >> i;
148+
for(j=0;j<n;j++)
149+
{
150+
temp=j+1;
151+
a.insert(j,temp);
152+
}
153+
for(j=0;j<n;j++)
154+
{
155+
cout << a[j] << " ";
156+
}
157+
cout << endl;
158+
j=1;
159+
k=0;
160+
while(1)
161+
{
162+
if(a.length()==1)
163+
{
164+
break;
165+
}
166+
if(j%i!=0)
167+
{
168+
a.deleteElement(1,temp);
169+
a.insert(a.length(),temp);
170+
j++;
171+
}
172+
else
173+
{
174+
/*cout << j << " " << i << endl;
175+
for(k=0;k<a.length();k++)
176+
{
177+
cout << a[k] << " ";
178+
}
179+
cout << endl;*/
180+
a.deleteElement(1,temp);
181+
cout << "person at position " << temp << " is removed" << endl;
182+
j++;
183+
}
184+
185+
}
186+
cout << "Hence person at position ";
187+
for(j=0;j<a.length();j++)
188+
{
189+
cout << a[j] << " ";
190+
}
191+
cout << "survives." << endl;
192+
193+
}

hot_potato/question.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Hot Potato (Josephus problem):
2+
In this game children line up in a circle and pass an item from neighbor to neighbor as
3+
fast as they can. At a certain point (say after a round) in the game, the action is stopped
4+
and the child who has the item (the potato) is removed from the circle. Play continues
5+
until only one child is left.
6+
7+
(a) Players : Let there are n children
8+
(b) Elimination Rule : Every i th child will be removed after a round .
9+
10+
Devise a winning strategy, it mean that one can choose a strategy to be the last person
11+
left who will be declared as the winner.
12+
13+
Input Data and Format
14+
• Enter number of children : n
15+
• Elimination Rule : i
16+
17+
18+
Expected Output for Correct and Incorrect Inputs
19+
Return: winning strategy i.e. a safe position s for some given values of n and i.
20+
21+
Example : If n = 7 and i = 3, then the safe position is s = 4. The persons at positions 3,
22+
6, 2, 7, 5, 1 are removed in order, and person at position 4 survives.

0 commit comments

Comments
 (0)