Created
February 12, 2022 20:55
-
-
Save yashtibrewal/d1e3e2508247c39b750e3190addc9652 to your computer and use it in GitHub Desktop.
Solution attempt to the question (google: Hackerrank poisonous biohazard allergic)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
class Result { | |
private Result(){} | |
/* | |
* Complete the 'bioHazard' function below. | |
* | |
* The function is expected to return a LONG_INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER n | |
* 2. INTEGER_ARRAY allergic | |
* 3. INTEGER_ARRAY poisonous | |
*/ | |
public static long bioHazard(int n, List<Integer> allergic, List<Integer> poisonous) { | |
HashMap<Integer, HashSet<Integer>> hashMap = new HashMap<>(); | |
int index = 0; | |
HashSet<Integer> set = null; | |
for(int poison: poisonous) { | |
if(hashMap.containsKey(poison)){ | |
hashMap.get(poison).add(allergic.get(index)); | |
}else { | |
set = new HashSet<>(); | |
set.add(allergic.get(index)); | |
hashMap.put(poison, set); | |
} | |
index++; | |
} | |
int intervals = 0; | |
List<Integer> sequence = new ArrayList<>(); | |
for(int i=1;i<=n;i++) { | |
if(hashMap.containsKey(i) && hashMap.get(i).contains(i+1)){ | |
continue; | |
} | |
if(hashMap.containsKey(i+1) && hashMap.get(i+1).contains(i)){ | |
continue; | |
} | |
sequence.add(i); | |
} | |
int size = sequence.size(); | |
long result =(long)Math.pow(2, size) - 1 - size; | |
System.out.println(result + n); | |
return intervals; | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) { | |
List<Integer> a1 = new ArrayList<>(); | |
a1.add(2); | |
a1.add(1); | |
a1.add(3); | |
List<Integer> a2 = new ArrayList<>(); | |
a2.add(3); | |
a2.add(3); | |
a2.add(1); | |
long result = Result.bioHazard(3,a1,a2); | |
System.out.print(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Incorrect logic