-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelebrityProblem.cpp
More file actions
96 lines (95 loc) · 1.69 KB
/
CelebrityProblem.cpp
File metadata and controls
96 lines (95 loc) · 1.69 KB
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
#include <iostream>
using namespace std;
#define NUM 3
class celebrity_problem
{
public:
int *celebrity;
int **Matrix;
int numOfPeople;
public:
celebrity_problem()
{
}
public:
void setNumOfPeople(int numOfPeople)
{
this->numOfPeople=numOfPeople;
}
void allocateMatrix()
{
this->Matrix=new int *[this->numOfPeople];
for(int i=0;i<this->numOfPeople;i++)
{
this->Matrix[i]=new int[this->numOfPeople];
}
}
void allocateCelebrity()
{
this->celebrity=new int[this->numOfPeople];
}
void setMatrix(int arr[NUM][NUM])
{
this->allocateMatrix();
for(int i=0;i<this->numOfPeople;i++)
{
for(int j=0;j<this->numOfPeople;j++)
{
this->Matrix[i][j]=arr[i][j];
}
}
}
void display_All()
{
for(int i=0;i<this->numOfPeople;i++)
{
cout<<"Is ith people is a celebrity?"<<(this->celebrity[i]==true?("true"):("false"))<<endl;
}
}
void findCelebrity()
{
this->allocateCelebrity();
for(int i=0;i<this->numOfPeople;i++)
{
bool isCelebrity=true;
for(int k=0;k<this->numOfPeople;k++)
{
if(i==k)
{
continue;
}
if(this->Matrix[i][k]!=1)
{
isCelebrity=false;
break;
}
}
if(isCelebrity==true)
{
for(int k=0;k<this->numOfPeople;k++)
{
if(i==k)
{
continue;
}
if(this->Matrix[k][i]!=0)
{
isCelebrity=false;
break;
}
}
}
this->celebrity[i]=isCelebrity;
}
}
};
int main()
{
celebrity_problem *C=new celebrity_problem();
C->setNumOfPeople(NUM);
int Matrix[NUM][NUM]={1,0,0,1,1,0,0,0,1};
C->setMatrix(Matrix);
C->findCelebrity();
C->display_All();
return 0;
}