{"id":9209,"date":"2022-08-29T12:49:37","date_gmt":"2022-08-29T12:49:37","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=9209"},"modified":"2023-11-27T12:27:46","modified_gmt":"2023-11-27T12:27:46","slug":"the-celebrity-problem","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/","title":{"rendered":"The Celebrity Problem"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\" alt=\"\" \/><\/p>\n<p>TThe Celebrity Problem in Java is a classic algorithmic puzzle that challenges programmers to efficiently identify a special individual within a group who is known by everyone but knows no one. This intriguing problem finds its applications in various fields, from network analysis to social media and even in real-world scenarios involving influential figures. As coders strive to devise optimal solutions to this problem, they encounter complexities that illuminate key concepts in algorithm design, graph theory, and computational efficiency.<\/p>\n<p>Exploring the depths of the Celebrity Problem not only sharpens coding skills but also underscores the significance of problem-solving approaches and their practical implications. In this article, we will delve into the intricacies of the Celebrity Problem, examine different strategies to solve it, and understand its relevance in the realm of programming and beyond.<\/p>\n<h2>What is the Celebrity Problem in Java?<\/h2>\n<p>There is a party of N (numbered 0 to N-1) people. There might or might not be a celebrity at this party. A celebrity is someone who is known to everyone but does not know anyone. So, if you are a celebrity at a party, you will be known to everyone, but you won\u2019t know anyone there.<\/p>\n<p>This situation is represented in the form of an N x N binary <a href=\"https:\/\/prepbytes.com\/blog\/recursion-interview-programming\/matrix-and-combination\/\" title=\"matrix.\">matrix.<\/a> So, every cell can either have a value of 0 or 1. If matrix[i][j] = 1, this means that the ith person knows the jth person.<\/p>\n<p>You have to tell whether a celebrity is present at the party or not. If it is present, we also have to tell which element (person) is the celebrity.<\/p>\n<p><strong>Note:<\/strong> The elements of the diagonal, i.e., when i = j, are always set to 0.<\/p>\n<h3>Example of the Celebrity Problem<\/h3>\n<p>Let us consider the binary matrix shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776476252-The%20Celebrity%20Problem-01.png\" alt=\"\" \/><\/p>\n<p>So, in the above matrix, 1 is the celebrity. This is because the row with index 1 has all elements 0 which means that 1 does not know anyone and the column with index 1 has all the elements 1 (except where row=column=1) which means that every element (every person in the party) knows 1. Let us consider some more examples.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776497352-The%20Celebrity%20Problem-02.png\" alt=\"\" \/><\/p>\n<p>In the above matrix, there is a column (column 1) that has all the elements 1 (except row=col); however, row 1 has a 1 in it at column 2. This means that 1 knows 2. So, 1 cannot be a celebrity. In fact, there is no celebrity in the matrix.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776594221-The%20Celebrity%20Problem-03.png\" alt=\"\" \/><\/p>\n<p>In another case, when we have row = 1, all the elements are 0. However, in this case, column = 1 does not have all the elements 1. The element at row = 2 is 0, meaning 2 does not know 1. Hence, 1 cannot be a celebrity. In fact, there is no celebrity in this matrix.<\/p>\n<h3>How many Celebrities are possible in a Matrix?<\/h3>\n<p>Before we jump into solving the problem, a very genuine question to ask is the minimum and maximum number of celebrities possible in a matrix. Consider the matrix shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776629802-The%20Celebrity%20Problem-04.png\" alt=\"\" \/><\/p>\n<p>So, in the above matrix, there is no row with all elements 0 and, similarly, no column with all values 1 (except where row=col). As a result, the bare minimum of celebrities is 0.<br \/>\nAssume there are two celebrities in the matrix, X and Y. As a result, according to the definition of celebrity,<\/p>\n<ol>\n<li>Because X is a celebrity, X should not know Y, and Y should know X.<\/li>\n<li>Because Y is a celebrity, Y should not know X, and X should know Y.<\/li>\n<\/ol>\n<p>Statement 1 states that X should not know Y, while Statement 2 states that S should know Y. These statements are diametrically opposed. Our assumption that there are 2 celebrities in the matrix is never possible.<\/p>\n<p><strong>So, the maximum number of celebrities possible in a matrix is 1.<\/strong><\/p>\n<h3>Basic Approach to Solve the Celebrity Problem<\/h3>\n<p>The basic approach is actually very intuitive. Let us represent the people in the party with the help of the vertices (nodes) of a graph. An edge from vertex V1 to V2 means V1 knows V2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776651396-The%20Celebrity%20Problem-05.png\" alt=\"\" \/><\/p>\n<p>Now, consider the matrix that is shown below and the graph corresponding to it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776667536-The%20Celebrity%20Problem-06.png\" alt=\"\" \/><\/p>\n<p>So, in the matrix, we can see that matrix[0][1] = 1 and matrix[0][2] = 1. This means that 0 knows 1 and 2. This is shown in the graph by the directed edges from vertex 0 to vertices 1 and 2 respectively.<\/p>\n<p>Similarly, the entire graph is created by traversing the matrix once. So, a celebrity in this case will be a vertex whose indegree is V-1 (everyone knows the celebrity), where V is the total number of vertices, and whose outdegree is 0 (the celebrity knows no one).<\/p>\n<p>So, instead of constructing an actual graph, we can have two arrays of size V representing the indegree and outdegree of each vertex, respectively. So, let us consider the situation shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776689952-The%20Celebrity%20Problem-07.png\" alt=\"\" \/><\/p>\n<p>So, we have 2 arrays, in and out for the matrix. As shown, initially, they are filled with 0s. Now, let us traverse the 1st row of the matrix.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776724561-The%20Celebrity%20Problem-08.png\" alt=\"\" \/><\/p>\n<p>Since matrix[0][0] = 0, this means that currently, there is no edge. <strong>Since diagonal elements are 0, there will not be any self-loops in the graph.<\/strong><\/p>\n<p>Now, let us move to the next index. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776737549-The%20Celebrity%20Problem-09.png\" alt=\"\" \/><\/p>\n<p>Since matrix[0][1] = 1, this means that there should be an edge from 0 to 1. This means that the outdegree of 0 should increase by 1 and the in-degree of 1 should increase by 1. So, the conclusion that we derive from this is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776754787-The%20Celebrity%20Problem-10.png\" alt=\"\" \/><\/p>\n<p>So, we can traverse the entire matrix and fill the <a href=\"https:\/\/prepbytes.com\/blog\/tag\/arrays\/\" title=\"array\">array<\/a> using this condition.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776770054-The%20Celebrity%20Problem-11.png\" alt=\"\" \/><\/p>\n<p>Now, just traverse the arrays and find if for any vertex i, in[i] = V-1 and out[i] = 0. If found, this vertex will be the celebrity.<br \/>\nIn this case, it is vertex 1.<\/p>\n<p>Now that we have understood this method, let us write the code for it.<\/p>\n<h3>Code Implementation of the Celebrity Problem in Java<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9400 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9400 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9400 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9400 .wpsm_nav-tabs > li.active > a, #tab_container_9400 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9400 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9400 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9400 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9400 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9400 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9400 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9400 .wpsm_nav-tabs > li > a:hover , #tab_container_9400 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9400 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9400 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9400 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9400 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9400 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9400 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9400 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9400 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9400 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9400 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9400 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9400\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9400\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9400_1\" aria-controls=\"tabs_desc_9400_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9400_2\" aria-controls=\"tabs_desc_9400_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9400\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9400_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt; \r\nusing namespace std; \r\nint celebrity_solution(int mat[][4], int n) {\r\n    int in[n];\r\n    int out[n];\r\n    for(int i=0;i&lt;n;i++) {\r\n        in[i] = out[i] = 0;\r\n    }\r\n    for(int i=0;i&lt;n;i++) {\r\n        for(int j=0;j&lt;n;j++) {\r\n            if(mat[i][j] == 1) {\r\n                out[i]++;\r\n                in[j]++;\r\n            }\r\n        }\r\n    }\r\n    for(int i=0;i&lt;n;i++) {\r\n        if(in[i] == n-1 &amp;&amp; out[i] == 0) return i;\r\n    }\r\n    return -1;\r\n}\r\nint main() {\r\n    int matrix[][4] = {{0,1,1,0},\r\n                     {0,0,0,0},\r\n                     {0,1,0,0},\r\n                     {1,1,0,0}};\r\n    int res = celebrity_solution(matrix,4);\r\n    if(res == -1) {\r\n        cout&lt;&lt; &quot;There is no celebrity in the party&quot;;\r\n    } else {\r\n        cout&lt;&lt; res &lt;&lt;&quot; is the celebrity in the party&quot;;\r\n    }\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9400_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\npublic class Main {\r\n    public static int celebritySolution(int[][] mat) {  \r\n        int V = mat.length;\r\n        int[] in = new int[V];\r\n        int[] out = new int[V]; \r\n        for(int i=0;i&lt;V;i++) {\r\n            for(int j=0;j&lt;V;j++) {\r\n                if(mat[i][j] == 1) {\r\n                    out[i]++;\r\n                    in[j]++;\r\n                }\r\n            }\r\n        }\r\n        for(int i=0;i&lt;V;i++) {\r\n            if(in[i] == V-1 &amp;&amp; out[i] == 0) return i;\r\n        }\r\n        return -1;\r\n    }\r\n    public static void main(String[] args) {\r\n        int[][] matrix = {{0,1,1,0},\r\n                         {0,0,0,0},\r\n                         {0,1,0,0},\r\n                         {1,1,0,0}};                 \r\n        int res = celebritySolution(matrix);\r\n        if(res == -1) {\r\n            System.out.println(&quot;There is no celebrity in the party&quot;);\r\n        } else {\r\n            System.out.println(res + &quot; is the celebrity in the party&quot;);\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9400 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9400 a\"),jQuery(\"#tab-content_9400\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Time Complexity:<\/strong> The time complexity of the above approach is O(n2) because of the matrix traversal.<\/p>\n<p><strong>Space Complexity:<\/strong> O(n), as we have created 2 auxiliary arrays of size n, where n is the number of people in the party.<\/p>\n<h3>Optimized Approach to the Celebrity Problem using Stack<\/h3>\n<p>The time complexity of the previous solution was O(n2). We can write a solution in O(n) time without using a graph and instead using a stack. Consider the matrix that is shown below and a stack of integers as shown.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776788984-The%20Celebrity%20Problem-12.png\" alt=\"\" \/><\/p>\n<p>Let us push all the elements (people at the party) into the stack. Here, the order does not matter.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776803080-The%20Celebrity%20Problem-13.png\" alt=\"\" \/><\/p>\n<p>We will now process the stack until only one element remains. We will take two elements from the stack at a time and push only one.<\/p>\n<p>So, let us pop an element from the stack, and we&#8217;ll call it \u201ccol\u201d. Let us select another element from the stack and label it &quot;row.&quot; For the current iteration, col = 3 and row = 2. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776820374-The%20Celebrity%20Problem-14.png\" alt=\"\" \/><\/p>\n<p>Now, we check whether matrix[row][col] = 1 or 0. Here, matrix[row][col] = matrix[2][3] = 0. This means that 2 does not know 3. Since a celebrity is a person known to everyone, it is sure that 3 will not be a celebrity. However, 2 may or may not be a celebrity. Hence, we push 2 back into the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776834889-The%20Celebrity%20Problem-15.png\" alt=\"\" \/><\/p>\n<p>So, the conclusion devised from the above observation is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776853235-The%20Celebrity%20Problem-16.png\" alt=\"\" \/><\/p>\n<p>Again, let us continue the steps. So, we pop again from the stack, as shown.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776868004-The%20Celebrity%20Problem-17.png\" alt=\"\" \/><\/p>\n<p>So, again, the column will be discarded, and we will push the row into the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776883239-The%20Celebrity%20Problem-18.png\" alt=\"\" \/><\/p>\n<p>Again, let\u2019s pop out of the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776899051-The%20Celebrity%20Problem-19.png\" alt=\"\" \/><\/p>\n<p>Since matrix[0][1] = 1, this means that 0 knows 1. A celebrity is an element that does not know anyone. Since 0 knows 1, it cannot be a celebrity. Hence, we will discard it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776912794-The%20Celebrity%20Problem-20.png\" alt=\"\" \/><\/p>\n<p>The conclusion is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776925947-The%20Celebrity%20Problem-21.png\" alt=\"\" \/><\/p>\n<p>We stop iterating now that there is only one element left in the stack. Again, this element is unlikely to be a celebrity. This is due to the fact that it was pushed into the stack not because it was a celebrity, but because the other element was not. As a result, we must examine this single element.<\/p>\n<p>To do so, we traverse its row, and all of the elements should be 0. In addition, we traverse in its column, and all elements should be 1 (except where row=col).<\/p>\n<p>In this instance, we discover that 1 is a celebrity. Now that we&#8217;ve grasped the procedure, let&#8217;s write the code for it.<\/p>\n<h3>Code Implementation of the Celebrity Problem using Stack<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9403 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9403 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9403 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9403 .wpsm_nav-tabs > li.active > a, #tab_container_9403 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9403 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9403 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9403 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9403 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9403 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9403 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9403 .wpsm_nav-tabs > li > a:hover , #tab_container_9403 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9403 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9403 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9403 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9403 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9403 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9403 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9403 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9403 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9403 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9403 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9403 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9403\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9403\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9403_1\" aria-controls=\"tabs_desc_9403_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9403_2\" aria-controls=\"tabs_desc_9403_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9403\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9403_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt; \r\nusing namespace std; \r\n  \r\nint celebrity_solution(int mat[][4],int N) {\r\n        stack&lt;int&gt; stk;\r\n    \t\r\n    \tfor(int i=0;i&lt;N;i++) {\r\n    \t    stk.push(i);\r\n    \t}\r\n    \t\r\n    \twhile(stk.size() &gt; 1) {\r\n    \t    int col = stk.top();\r\n    \t    stk.pop();\r\n    \t    int row = stk.top();\r\n    \t    stk.pop();\r\n    \t    if(mat[row][col] == 1) {\r\n    \t        stk.push(col); \/\/col may or may not be a celeb\r\n    \t    } else {\r\n    \t        stk.push(row); \/\/row may or may not be a celeb\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \tint x = stk.top();\r\n    \tstk.pop();\r\n    \t\r\n    \tfor(int j=0;j&lt;N;j++) {\r\n    \t    if(j == x) continue;\r\n    \t    if(mat[x][j] == 1) {\r\n    \t        return -1;\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \tfor(int i=0;i&lt;N;i++) {\r\n    \t    if(i == x) continue;\r\n    \t    if(mat[i][x] == 0) {\r\n    \t        return -1;\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \treturn x;\r\n}  \r\n  \r\nint main() {\r\n    int matrix[][4] = {{0,1,1,0},\r\n                     {0,0,0,0},\r\n                     {0,1,0,0},\r\n                     {1,1,0,0}};\r\n                     \r\n    int res = celebrity_solution(matrix,4);\r\n    if(res == -1) {\r\n        cout&lt;&lt; &quot;There is no celebrity in the party&quot;;\r\n    } else {\r\n        cout&lt;&lt; res &lt;&lt;&quot; is the celebrity in the party&quot;;\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9403_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n    \r\n    public static int celebritySolution(int[][] mat) {\r\n        Stack&lt;Integer&gt; stk = new Stack&lt;&gt;();\r\n    \t\r\n    \tfor(int i=0;i&lt;mat.length;i++) {\r\n    \t    stk.push(i);\r\n    \t}\r\n    \t\r\n    \twhile(stk.size() &gt; 1) {\r\n    \t    int col = stk.pop();\r\n    \t    int row = stk.pop();\r\n    \t    if(mat[row][col] == 1) {\r\n    \t        stk.push(col); \/\/col may or may not be a celeb\r\n    \t    } else {\r\n    \t        stk.push(row); \/\/row may or may not be a celeb\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \tint x = stk.pop();\r\n    \t\r\n    \tfor(int j=0;j&lt;mat.length;j++) {\r\n    \t    if(j == x) continue;\r\n    \t    if(mat[x][j] == 1) {\r\n    \t        return -1;\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \tfor(int i=0;i&lt;mat.length;i++) {\r\n    \t    if(i == x) continue;\r\n    \t    if(mat[i][x] == 0) {\r\n    \t        return -1;\r\n    \t    }\r\n    \t}\r\n    \t\r\n    \treturn x;\r\n    }\r\n    \r\n    public static void main(String[] args) {\r\n          int[][] matrix = {{0,1,1,0},\r\n                         {0,0,0,0},\r\n                         {0,1,0,0},\r\n                         {1,1,0,0}};\r\n                         \r\n        int res = celebritySolution(matrix);\r\n        if(res == -1) {\r\n            System.out.println(&quot;There is no celebrity in the party&quot;);\r\n        } else {\r\n            System.out.println(res + &quot; is the celebrity in the party&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9403 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9403 a\"),jQuery(\"#tab-content_9403\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Time Complexity<\/strong>: The time complexity of this solution is O(N). This is because the maximum height of the Stack is also O(N) and for checking whether the last element is a celebrity or not, we are traversing just 1 row and 1 column. So, the time complexity becomes O(N + N) = O(2N) = O(N) only.<\/p>\n<p><strong>Space Complexity<\/strong>: The space complexity is O(N). This is because we are using a stack with maximum height = O(N). <\/p>\n<p><strong>Conclusion<\/strong><br \/>\nThe Celebrity Problem presents a captivating challenge for programmers, requiring them to devise elegant algorithms to pinpoint a unique individual in a group with specific attributes. As we&#8217;ve explored various strategies, from brute force approaches to more optimized solutions using graph theory and elimination techniques, we&#8217;ve witnessed the evolution of problem-solving techniques.<\/p>\n<p>Moreover, the significance of this problem transcends mere coding challenges; it reflects real-world scenarios where identifying influential entities within networks or communities holds considerable importance. By mastering this problem, programmers not only hone their algorithmic skills but also gain insights into broader applications across diverse domains.<\/p>\n<p>As the quest for efficient algorithms continues, the Celebrity Problem serves as a testament to the intricate nature of problem-solving in coding and its relevance in understanding complex networks and relationships.<\/p>\n<h2>Frequently Asked Questions (FAQs) Related to The Celebrity Problem<\/h2>\n<p>Here are some FAQs related to the Celebrity Problem.<\/p>\n<p><strong>1. What is the Celebrity Problem in coding?<\/strong><br \/>\nThe Celebrity Problem involves identifying an individual within a group who is known by everyone but knows no one. In coding terms, it requires finding a person in a group where everyone else knows this person (the celebrity), but the celebrity doesn&#8217;t know anyone else.<\/p>\n<p><strong>2. What are the practical applications of solving the Celebrity Problem?<\/strong><br \/>\nThe problem has applications in various fields such as network analysis, social media, event scheduling, and even in identifying influential figures in different contexts.<\/p>\n<p><strong>3. What are some common strategies to solve the Celebrity Problem?<\/strong><br \/>\nStrategies include brute force methods, graph-based algorithms like the graph elimination technique, and heuristic approaches that reduce the number of comparisons needed.<\/p>\n<p><strong>4. How does solving the Celebrity Problem contribute to coding skills?<\/strong><br \/>\nSolving this problem sharpens algorithmic thinking, encourages optimization of solutions, and enhances understanding of graph theory and computational efficiency, which are crucial aspects of coding.<\/p>\n<p><strong>5. Can the Celebrity Problem be modified for different scenarios?<\/strong><br \/>\nYes, the problem can be adapted to various scenarios by altering the attributes or constraints of the individuals within the group, making it applicable to different real-world situations where similar identification challenges exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TThe Celebrity Problem in Java is a classic algorithmic puzzle that challenges programmers to efficiently identify a special individual within a group who is known by everyone but knows no one. This intriguing problem finds its applications in various fields, from network analysis to social media and even in real-world scenarios involving influential figures. As [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[127],"tags":[],"class_list":["post-9209","post","type-post","status-publish","format-standard","hentry","category-stacks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Celebrity Problem | Stacks | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Celebrity Problem | Stacks | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-29T12:49:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-27T12:27:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"The Celebrity Problem\",\"datePublished\":\"2022-08-29T12:49:37+00:00\",\"dateModified\":\"2023-11-27T12:27:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\"},\"wordCount\":1789,\"commentCount\":1,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\",\"articleSection\":[\"Stacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\",\"name\":\"The Celebrity Problem | Stacks | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\",\"datePublished\":\"2022-08-29T12:49:37+00:00\",\"dateModified\":\"2023-11-27T12:27:46+00:00\",\"description\":\"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stacks\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/stacks\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"The Celebrity Problem\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Celebrity Problem | Stacks | PrepBytes Blog","description":"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/","og_locale":"en_US","og_type":"article","og_title":"The Celebrity Problem | Stacks | PrepBytes Blog","og_description":"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).","og_url":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-08-29T12:49:37+00:00","article_modified_time":"2023-11-27T12:27:46+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"The Celebrity Problem","datePublished":"2022-08-29T12:49:37+00:00","dateModified":"2023-11-27T12:27:46+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/"},"wordCount":1789,"commentCount":1,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg","articleSection":["Stacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/","url":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/","name":"The Celebrity Problem | Stacks | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg","datePublished":"2022-08-29T12:49:37+00:00","dateModified":"2023-11-27T12:27:46+00:00","description":"In this article we have discussed about the Celebrity problem using Graph(Basic Approach) and Stack (Optimized Approach).","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661776452080-The%20Celebrity%20Problem-%20Header.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/the-celebrity-problem\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Stacks","item":"https:\/\/prepbytes.com\/blog\/category\/stacks\/"},{"@type":"ListItem","position":3,"name":"The Celebrity Problem"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=9209"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9209\/revisions"}],"predecessor-version":[{"id":18386,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9209\/revisions\/18386"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=9209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=9209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=9209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}