Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.rampatra.adsj</groupId>
<artifactId>adsj</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<packaging>jar</packaging>

<name>Algorithms and Data Structures in Java</name>
<description>My solutions to some of the algorithm and data structure questions in Java</description>
Expand Down Expand Up @@ -60,6 +60,13 @@
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>

<profiles>
Expand Down Expand Up @@ -125,8 +132,19 @@
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.rampatra.arrays.CelebrityProblem</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>


</project>
45 changes: 30 additions & 15 deletions src/main/java/com/rampatra/arrays/CelebrityProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CelebrityProblem {
* @param b
* @return
*/
public static boolean haveAcquaintance(int[][] peoples, int a, int b) {
public static boolean isPersonAKnowPersonB(int[][] peoples, int a, int b) {
return peoples[a][b] == 1;
}

Expand All @@ -40,7 +40,7 @@ public static boolean haveAcquaintance(int[][] peoples, int a, int b) {
* - Repeat above two steps till we left with only one person.
* Find celebrity within remaining persons by performing the below operations:
* - Push all the celebrities into a stack.
* - Pop off top two persons from the stack, discard one person based on return status of HaveAcquaintance(A, B).
* - Pop off top two persons from the stack, discard one person based on return status of isPersonAKnowPersonB(A, B).
* - Push the remained person onto stack.
* - Repeat step 2 and 3 until only one person remains in the stack.
* - Check the remained person in stack does not have acquaintance with anyone else.
Expand All @@ -52,34 +52,49 @@ public static int findCelebrity(int[][] peoples) {

Stack<Integer> possibleCelebrities = new LinkedStack<>();

for (int i = 0; i < peoples.length; i++) {
for (int j = 0; j < peoples[0].length; j++) {
if (haveAcquaintance(peoples, i, j)) {
possibleCelebrities.push(j);
for (int a = 0; a < peoples.length; a++) {
for (int b = 0; b < peoples[0].length; b++) {
if (isPersonAKnowPersonB(peoples, a, b)) {
possibleCelebrities.push(b);
}
}
}

int firstPerson = -1, secondPerson;
int personA = -1;
int personB;
while (!possibleCelebrities.isEmpty()) {
firstPerson = possibleCelebrities.pop();
personA = possibleCelebrities.pop();

// we have found the celebrity
if (possibleCelebrities.isEmpty()) break;

secondPerson = possibleCelebrities.pop();
if (haveAcquaintance(peoples, firstPerson, secondPerson)) {
possibleCelebrities.push(secondPerson);
personB = possibleCelebrities.pop();
if (isPersonAKnowPersonB(peoples, personA, personB)) {
possibleCelebrities.push(personB);
} else {
possibleCelebrities.push(firstPerson);
possibleCelebrities.push(personA);
}
}

return firstPerson;
return personA;
}

public static void main(String[] args) {
System.out.println(findCelebrity(new int[][]{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 1, 0}}));
System.out.println(findCelebrity(new int[][]{{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}));
int[][] people1 = {
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0},
{0, 0, 1, 0}
};

int[][] people2 = {
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1}
};

System.out.println(findCelebrity(people1));
System.out.println(findCelebrity(people2));
}
}
88 changes: 88 additions & 0 deletions src/test/java/com/CelebrityProblemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com;

import com.rampatra.arrays.CelebrityProblem;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class CelebrityProblemTest {

private static int[][] noCelebrity;
private static int[][] celebrityAtPosition0;
private static int[][] celebrityAtPosition1;
private static int[][] celebrityAtPosition2;
private static int[][] celebrityAtPosition3;
private static int[][] emptyPeoplesArray;
private static int[][] peoplesArrayWithOnePerson;

@BeforeAll
static void initValue() {
noCelebrity = new int[][]{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
celebrityAtPosition0 = new int[][]{
{0, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}
};
celebrityAtPosition1 = new int[][]{
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
};
celebrityAtPosition2 = new int[][]{
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0},
{0, 0, 1, 0}
};
celebrityAtPosition3 = new int[][]{
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 0}
};
emptyPeoplesArray = new int[][]{};
peoplesArrayWithOnePerson = new int[][]{{0}};
}

@Test
public void testNoCelebrity() {
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(noCelebrity));
}

@Test
public void testCelebrityAtPosition0() {
Assertions.assertEquals(0, CelebrityProblem.findCelebrity(celebrityAtPosition0));
}

@Test
public void testCelebrityAtPosition1() {
Assertions.assertEquals(1, CelebrityProblem.findCelebrity(celebrityAtPosition1));
}

@Test
public void testCelebrityAtPosition2() {
Assertions.assertEquals(2, CelebrityProblem.findCelebrity(celebrityAtPosition2));
}

@Test
public void testCelebrityAtPosition3() {
Assertions.assertEquals(3, CelebrityProblem.findCelebrity(celebrityAtPosition3));
}

@Test
public void testEmptyPeoplesArray() {
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(emptyPeoplesArray));
}

@Test
public void testPeoplesArrayWithOnePerson() {
Assertions.assertEquals(-1, CelebrityProblem.findCelebrity(peoplesArrayWithOnePerson));
}
}