-
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.
Added more algo and TechGig code gladaitors
- Loading branch information
1 parent
a3f7767
commit b3d1f05
Showing
18 changed files
with
1,070 additions
and
2 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
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 |
---|---|---|
@@ -1,10 +1,25 @@ | ||
# Contest | ||
|
||
## [TechGig Code Gladiators](https://www.techgig.com/codegladiators/opencontest) | ||
|
||
## Code Gladiators 2022 | ||
1. [World Army vs Aliens](./src/main/java/com/contest/techgig/y2022/codegladiators-2022.md) | ||
|
||
# Algo-Solutions | ||
|
||
This Repo includes all the Practice coding problems which I took on various sites. Will try to maintain the source and description as well. | ||
Anyone who can come up with more better algo are free to raise Pull Request will be happy to merge them :) | ||
|
||
## [TechGig](https://www.techgig.com) | ||
|
||
## Algo Section | ||
|
||
1. [Count-Of-One](./src/main/java/com/malay/techgig/algo/Techgig.md#Count-Of-One) | ||
2. [Presents to Masha](./src/main/java/com/malay/techgig/algo/Techgig.md#Presents-to-Masha) | ||
3. [First repeating element](./src/main/java/com/malay/techgig/algo/Techgig.md#First-repeating-element) | ||
4. [Count Bigger](./src/main/java/com/malay/techgig/algo/Techgig.md#Count-Bigger) | ||
5. [Linear Search](./src/main/java/com/malay/techgig/algo/Techgig.md#Linear-Search) | ||
6. [Binary Search](./src/main/java/com/malay/techgig/algo/Techgig.md#Binary-Search) | ||
7. [Occurrence of M](./src/main/java/com/malay/techgig/algo/Techgig.md#Occurrence-of-M) | ||
|
||
|
86 changes: 86 additions & 0 deletions
86
src/main/java/com/contest/techgig/y2021/JazzyCricketBalls.java
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,86 @@ | ||
/*********************************** | ||
* AlgoSolutions Project | ||
* Filename: JazzyCricketBalls.java | ||
* Author : malay | ||
* Date : 04-Jul-2021 | ||
* | ||
**********************************/ | ||
package com.contest.techgig.y2021; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class JazzyCricketBalls { | ||
|
||
private static Map<Integer, Long> resultMap = new HashMap<Integer, Long>(); | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner s = new Scanner(System.in); | ||
int noOfPackets = s.nextInt(); | ||
s.nextLine(); | ||
|
||
if (noOfPackets < 1 || noOfPackets > 100) { | ||
System.exit(0); | ||
} | ||
|
||
String testStr = s.nextLine(); | ||
List<Integer> ballsInPacks = convertToList(testStr, noOfPackets); | ||
|
||
Long sum = 0L; | ||
for (Integer balls : ballsInPacks) { | ||
if (resultMap.containsKey(balls)) { | ||
sum = sum + resultMap.get(balls); | ||
} else { | ||
Long result = getNoOfMoves(balls); | ||
resultMap.put(balls, result); | ||
sum = sum + result; | ||
} | ||
} | ||
System.out.println(sum); | ||
|
||
s.close(); | ||
} | ||
|
||
private static Long getNoOfMoves(Integer balls) { | ||
if (resultMap.containsKey(balls)) { | ||
return resultMap.get(balls); | ||
} | ||
if (balls == 1) { | ||
return 1L; | ||
} | ||
if (balls == 2) { | ||
return getNoOfMoves(1) + getNoOfMoves(1) + 1; // +1 for splitting | ||
} | ||
Integer divisible = getDivisibleFactor(balls); | ||
Integer noOfGroups = balls / divisible; | ||
Long result = (noOfGroups * getNoOfMoves(divisible)) + 1; // +1 for splitting | ||
resultMap.put(balls, result); | ||
return result; | ||
} | ||
|
||
private static Integer getDivisibleFactor(Integer value) { | ||
Integer num = 2; | ||
while (value % num != 0) { | ||
if (num >= value / 2) { | ||
return 1; | ||
} | ||
num++; | ||
} | ||
return num; | ||
} | ||
|
||
private static List<Integer> convertToList(String testStr, int packs) { | ||
List<Integer> values = new ArrayList<Integer>(packs); | ||
if (testStr != null) { | ||
String[] strs = testStr.split(" "); | ||
for (String str : strs) { | ||
values.add(Integer.parseInt(str)); | ||
} | ||
} | ||
return values; | ||
} | ||
} |
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,88 @@ | ||
/*********************************** | ||
* AlgoSolutions Project | ||
* Filename: PrimeGame.java | ||
* Author : malay | ||
* Date : 21-Jun-2021 | ||
* | ||
**********************************/ | ||
package com.contest.techgig.y2021; | ||
|
||
import java.util.Scanner; | ||
|
||
public class PrimeGame { | ||
|
||
public static void main(String args[]) throws Exception { | ||
|
||
Scanner s = new Scanner(System.in); | ||
int noOfTest = s.nextInt(); | ||
s.nextLine(); | ||
|
||
if (noOfTest < 1 || noOfTest > 10) { | ||
System.exit(0); | ||
} | ||
|
||
for (int i = 0; i < noOfTest; i++) { | ||
String testStr = s.nextLine(); | ||
if (testStr == null || testStr.trim().length() == 0) { | ||
System.exit(0); | ||
} | ||
|
||
String[] testStrArr = testStr.split(" "); | ||
int left = Integer.parseInt(testStrArr[0]); | ||
int right = Integer.parseInt(testStrArr[1]); | ||
|
||
int result = getMaximumPrimeDiff(left, right); | ||
System.out.println(result); | ||
} | ||
s.close(); | ||
|
||
} | ||
|
||
private static int getMaximumPrimeDiff(int left, int right) { | ||
int minPrime = -1; | ||
int maxPrime = -1; | ||
|
||
// Start from left, stop when minimum prime no is found | ||
while (left <= right) { | ||
if (isPrime(left)) { | ||
minPrime = left; | ||
break; | ||
} | ||
left++; | ||
} | ||
|
||
// if reached end, then return -1, no prime number retieved | ||
if (minPrime == -1) { | ||
return minPrime; | ||
} | ||
|
||
// Start from right and navigate till we explore left, stop when maximum prime no is found | ||
while (right >= left) { | ||
if (isPrime(right)) { | ||
maxPrime = right; | ||
break; | ||
} | ||
right--; | ||
} | ||
|
||
return maxPrime - minPrime; | ||
} | ||
|
||
private static boolean isPrime(int value) { | ||
if (value <= 1) { | ||
return false; | ||
} | ||
if (value == 2) { | ||
return true; | ||
} | ||
|
||
int num = 2; | ||
while (value % num != 0) { | ||
if (num >= value / 2) { | ||
return true; | ||
} | ||
num++; | ||
} | ||
return false; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
src/main/java/com/contest/techgig/y2021/Techgig-codegladiators.md
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,105 @@ | ||
# Techgig-Code Gladiators 2021 | ||
|
||
#### Virus Outbreak | ||
|
||
*[VirusOutbreak.java](./VirusOutbreak.java)* | ||
|
||
``` | ||
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 | ||
``` | ||
|
||
#### Prime Game | ||
|
||
*[PrimeGame.java](./PrimeGame.java)* | ||
|
||
``` | ||
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 | ||
``` |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/contest/techgig/y2021/VirusOutbreak.java
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,60 @@ | ||
/*********************************** | ||
* AlgoSolutions Project | ||
* Filename: VirusOutbreak.java | ||
* Author : malay | ||
* Date : 13-Jun-2021 | ||
* | ||
**********************************/ | ||
package com.contest.techgig.y2021; | ||
|
||
import java.util.Scanner; | ||
|
||
public class VirusOutbreak { | ||
|
||
private static final String NEGATIVE = "NEGATIVE"; | ||
private static final String POSITIVE = "POSITIVE"; | ||
|
||
public static void main(String args[]) throws Exception { | ||
|
||
Scanner s = new Scanner(System.in); | ||
String compositionStr = s.nextLine(); | ||
int noOfTest = s.nextInt(); | ||
s.nextLine(); | ||
|
||
if (noOfTest < 1 || noOfTest > 10) { | ||
System.out.println("Error"); | ||
System.exit(0); | ||
} | ||
|
||
int i = 0; | ||
while (i < noOfTest) { | ||
String testStr = s.nextLine(); | ||
String result = getResult(compositionStr, testStr); | ||
System.out.println(result); | ||
i++; | ||
} | ||
s.close(); | ||
|
||
} | ||
|
||
private static String getResult(String compositionStr, String testStr) { | ||
char[] compoArr = compositionStr.toCharArray(); | ||
char[] testArr = testStr.toCharArray(); | ||
|
||
int iCompo = 0; | ||
int iTest = 0; | ||
while (iTest < testArr.length) { | ||
|
||
while (iCompo < compoArr.length && testArr[iTest] != compoArr[iCompo]) { | ||
iCompo++; | ||
} | ||
|
||
if (iCompo >= compoArr.length) { | ||
return NEGATIVE; | ||
} | ||
|
||
iTest++; | ||
} | ||
return POSITIVE; | ||
} | ||
} |
Oops, something went wrong.