-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e212bc
commit 9e54500
Showing
4 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Virus Outbreak (100 Marks) | ||
In the Martian land faraway, a new virus has evolved and is attacking the individuals at a fast pace. The scientists have figured out the virus composition, V. The big task is to identify the people who are infected. The sample of N people is taken to check if they are POSITIVE or NEGATIVE. A report is generated which provides the current blood composition B of the person. | ||
|
||
|
||
POSITIVE or NEGATIVE ? | ||
|
||
If the blood composition of the person is a subsequence of the virus composition V, then the person is identified as POSITIVE otherwise NEGATIVE. | ||
|
||
|
||
### Example: | ||
|
||
Virus Composition, V = coronavirus | ||
|
||
Blood Composition of the person , B = ravus | ||
|
||
The person in question is POSITIVE as B is the subsequence of the V. | ||
|
||
The scientists are busy with their research for medicine and request you to build a program which can quickly figure out if the person is POSITIVE or NEGATIVE. They will provide you with the virus composition V and all the people’s current blood composition. Can you help them? | ||
|
||
|
||
Note: The virus and blood compositions are lowercase alphabet strings. | ||
|
||
Input Format | ||
The first line of the input consists of the virus composition, V | ||
|
||
The second line of he input consists of the number of people, N | ||
|
||
Next N lines each consist of the blood composition of the ith person, Bi | ||
|
||
### Constraints | ||
1<= N <=10 | ||
|
||
1<= |B|<= |V|<= 10^5 | ||
|
||
|
||
### Output Format | ||
For each person, print POSITIVE or NEGATIVE in a separate line | ||
|
||
##### Sample TestCase 1 | ||
#### Input | ||
coronavirus\ | ||
3\ | ||
abcde\ | ||
crnas\ | ||
onarous | ||
|
||
#### Output | ||
NEGATIVE\ | ||
POSITIVE\ | ||
NEGATIVE |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Prime Game (100 Marks) | ||
Rax, a school student, was bored at home in the pandemic. He wanted to play but there was no one to play with. He was doing some mathematics questions including prime numbers and thought of creating a game using the same. After a few days of work, he was ready with his game. He wants to play the game with you. | ||
|
||
|
||
## GAME: | ||
|
||
Rax will randomly provide you a range [ L , R ] (both inclusive) and you have to tell him the maximum difference between the prime numbers in the given range. There are three answers possible for the given range. | ||
|
||
There are two distinct prime numbers in the given range so the maximum difference can be found. | ||
|
||
There is only one distinct prime number in the given range. The maximum difference in this case would be 0. | ||
|
||
There are no prime numbers in the given range. The output for this case would be -1. | ||
|
||
To win the game, the participant should answer the prime difference correctly for the given range. | ||
|
||
## Example: | ||
|
||
Range: [ 1, 10 ] | ||
|
||
The maximum difference between the prime numbers in the given range is 5. | ||
|
||
Difference = 7 - 2 = 5 | ||
|
||
Range: [ 5, 5 ] | ||
|
||
There is only one distinct prime number so the maximum difference would be 0. | ||
|
||
Range: [ 8 , 10 ] | ||
|
||
There is no prime number in the given range so the output for the given range would be -1. | ||
|
||
Can you win the game? | ||
|
||
### Input Format | ||
The first line of input consists of the number of test cases, T | ||
|
||
Next T lines each consists of two space-separated integers, L and R | ||
|
||
### Constraints | ||
1<= T <=10 | ||
|
||
2<= L<= R<=10^6 | ||
|
||
### Output Format | ||
For each test case, print the maximum difference in the given range in a separate line. | ||
|
||
#### Sample TestCase 1 | ||
#### Input | ||
5\ | ||
5 5\ | ||
2 7\ | ||
8 10\ | ||
10 20\ | ||
4 5 | ||
|
||
#### Output | ||
0 | ||
5\ | ||
-1\ | ||
8\ | ||
0 | ||
|
||
### Explanation | ||
|
||
Test Case 1: [ 5 - 5 ] = 0 | ||
|
||
Test Case 2: [ 7 - 2 ] = 5 | ||
|
||
Test Case 3: No prime number in the given range. Output = -1 | ||
|
||
Test Case 4: [ 19 - 11 ] = 8 | ||
|
||
Test Case 5: The difference would be 0 since there is only one prime number in the given range. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.demo.exercises; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
public class CompositionFinder { | ||
|
||
public static void main(String[] args) { | ||
CompositionFinder finder = new CompositionFinder(); | ||
finder.readInputsAndResolve(); | ||
} | ||
|
||
private void readInputsAndResolve(){ | ||
try { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer tokenizer = new StringTokenizer(br.readLine()); | ||
String _virusComposition = tokenizer.nextToken(); | ||
int _peopleCount = Integer.parseInt(br.readLine()); | ||
int cnt = 0; | ||
while(cnt< _peopleCount){ | ||
String _bloodComposition = br.readLine(); | ||
if(!validateComposition(_bloodComposition, _virusComposition)){ | ||
System.out.println("NEGATIVE"); | ||
} else{ | ||
System.out.println("POSITIVE"); | ||
} | ||
cnt++; | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static boolean validateComposition(String bloodComposition, String virusComposition){ | ||
char[] _bloodCompChars = bloodComposition.toCharArray(); | ||
if(_bloodCompChars.length<1){ | ||
return true; | ||
} | ||
String validationString = virusComposition; | ||
for(int i=0; i<_bloodCompChars.length; i++){ | ||
if(validationString.indexOf(_bloodCompChars[i])==-1){ | ||
return false; | ||
} | ||
validationString = buildNxtString(_bloodCompChars[i], validationString); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static String buildNxtString(char val, String input){ | ||
int i=input.indexOf(val); | ||
if(i!=-1){ | ||
return i<input.length() ?input.substring(i+1) :input.substring(i); | ||
} | ||
return ""; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.demo.exercises; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
public class MaxPrimeNumDifference { | ||
|
||
public static void main(String[] args) { | ||
MaxPrimeNumDifference code = new MaxPrimeNumDifference(); | ||
code.readInputsAndProcess(); | ||
} | ||
|
||
private void readInputsAndProcess(){ | ||
try { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer tokenizer = new StringTokenizer(br.readLine()); | ||
int _testCases = Integer.parseInt(tokenizer.nextToken()); | ||
int cnt = 0; | ||
while(cnt< _testCases){ | ||
final long startTime = System.nanoTime()/1000000; | ||
String[] numRange = br.readLine().split(" "); | ||
final int[] primes = new int[2]; | ||
primes[0] = findMinPrimeNumberInRange(Integer.parseInt(numRange[0]), Integer.parseInt(numRange[1])); | ||
primes[1] = findMaxPrimeNumberInRange(Integer.parseInt(numRange[0]), Integer.parseInt(numRange[1])); | ||
if(primes[0] == 0 && primes[1]==0){ | ||
System.out.println(-1); | ||
} else if(primes[1] == 0){ | ||
System.out.println(0); | ||
} else{ | ||
System.out.println(primes[1]-primes[0]); | ||
} | ||
final long endTime = System.nanoTime()/1000000; | ||
System.out.println("Time Taken for input(ms)"+(cnt+1)+"==>"+(endTime-startTime)); | ||
cnt++; | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private int findMinPrimeNumberInRange(int start, int end){ | ||
for(int i=start; i<=end; i++){ | ||
if(isPrime(i)){ | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private int findMaxPrimeNumberInRange(int start, int end){ | ||
for(int i=end; i>=start; i--){ | ||
if(isPrime(i)){ | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private static boolean isPrime(int num){ | ||
int count = 0; | ||
int i=1; | ||
while(i<=num){ | ||
if(num%i == 0){ | ||
count++; | ||
} | ||
i++; | ||
} | ||
|
||
if(count == 2){return true;} | ||
return false; | ||
} | ||
} |