Skip to content

Commit f30fb40

Browse files
authored
Merge pull request adityabisoi#544 from Aarushi11H/main
Algo Search Easy-java soln
2 parents 8c92403 + 2ed1d12 commit f30fb40

File tree

4 files changed

+178
-3
lines changed

4 files changed

+178
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Way to go ahead:
3+
Keep track of the index and the complement value
4+
in a hasmap and as we can our input, check if we
5+
see a complement
6+
if we do see one then just print out its' index
7+
and our current index
8+
9+
10+
Time complexity: O(n) //The number of flavors we have to look at
11+
Space complexity: O(n) //The number of flavors we store the complement of
12+
*/
13+
14+
import java.io.*;
15+
import java.util.*;
16+
17+
public class solution {
18+
19+
public static void main(String[] args) {
20+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named solution. */
21+
Scanner input = new Scanner(System.in);
22+
int t = input.nextInt();
23+
for(int a0 = 0; a0 < t; a0++)
24+
{
25+
int money = input.nextInt();
26+
int flavors = input.nextInt();
27+
Map<Integer, Integer> complements = new HashMap<>();
28+
29+
for(int i = 1; i <= flavors; i++)
30+
{
31+
int cost = input.nextInt();
32+
if(complements.containsKey(cost))
33+
System.out.println(complements.get(cost) + " " + i);
34+
else
35+
complements.put(money-cost, i);
36+
}
37+
}
38+
}
39+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class solution {
10+
11+
// Complete the missingNumbers function below.
12+
static int[] missingNumbers(int[] arr, int[] brr) {
13+
14+
//Create an empty TreeMap to store array elements and their frequencies.
15+
TreeMap<Integer, Integer> integerFreqMap = new TreeMap<>();
16+
17+
// Add elements of original list
18+
//Traverse the array brr, and update the map with the frequency of each element.
19+
//This map now represents all the elements of the original array. Note that they are also in a sorted manner.
20+
for (int i : brr) {
21+
int freq = integerFreqMap.getOrDefault(i, 0);
22+
freq++;
23+
integerFreqMap.put(i, freq);
24+
}
25+
26+
// Remove elements of new list
27+
//Now traverse the array arr and for each element in the array, decrease the frequency by 1.
28+
for (int i : arr) {
29+
int freq = integerFreqMap.get(i);
30+
freq--;
31+
if (freq == 0) //If the frequency of any element becomes 0, then remove the key from the TreeMap.
32+
integerFreqMap.remove(i);
33+
else
34+
integerFreqMap.put(i, freq);
35+
}
36+
37+
// Create the result array
38+
int[] result = new int[integerFreqMap.size()];
39+
int i = 0;
40+
//At the end, scan the entire map for one last time and see all the elements who have a frequency greater than 1.
41+
for (Map.Entry<Integer, Integer> integerIntegerEntry : integerFreqMap.entrySet()) {
42+
result[i++] = integerIntegerEntry.getKey();
43+
}
44+
45+
return result;
46+
47+
}
48+
49+
private static final Scanner scanner = new Scanner(System.in);
50+
51+
public static void main(String[] args) throws IOException {
52+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
53+
54+
int n = scanner.nextInt();
55+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
56+
57+
int[] arr = new int[n];
58+
59+
String[] arrItems = scanner.nextLine().split(" ");
60+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
61+
62+
for (int i = 0; i < n; i++) {
63+
int arrItem = Integer.parseInt(arrItems[i]);
64+
arr[i] = arrItem;
65+
}
66+
67+
int m = scanner.nextInt();
68+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
69+
70+
int[] brr = new int[m];
71+
72+
String[] brrItems = scanner.nextLine().split(" ");
73+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
74+
75+
for (int i = 0; i < m; i++) {
76+
int brrItem = Integer.parseInt(brrItems[i]);
77+
brr[i] = brrItem;
78+
}
79+
80+
int[] result = missingNumbers(arr, brr);
81+
82+
for (int i = 0; i < result.length; i++) {
83+
bufferedWriter.write(String.valueOf(result[i]));
84+
85+
if (i != result.length - 1) {
86+
bufferedWriter.write(" ");
87+
}
88+
}
89+
90+
bufferedWriter.newLine();
91+
92+
bufferedWriter.close();
93+
94+
scanner.close();
95+
}
96+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
11+
Scanner in = new Scanner(System.in);
12+
int k = in.nextInt();
13+
for(int i=0; i<k; i++){
14+
int n = in.nextInt();
15+
int[] arr = new int[n];
16+
for(int j=0; j<n; j++){
17+
arr[j] = in.nextInt();
18+
}
19+
System.out.println(decision(arr));
20+
}
21+
}
22+
23+
public static String decision(int[] arr){
24+
int sum = 0;
25+
for(int i=0; i<arr.length; i++){
26+
sum += arr[i]; //sum stores the total array sum
27+
}
28+
29+
//curr is cimulated sum in the left
30+
int curr = 0;
31+
for(int j=0; j<arr.length; j++){
32+
//left part equals right part
33+
if(curr == sum - arr[j]-curr){ //sum - arr[j]-curr gives us the right part sum
34+
return "YES";
35+
}
36+
curr += arr[j];
37+
}
38+
return "NO";
39+
}
40+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
103103
||[Jumping on the Clouds](https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem)|Easy|[View](Algorithms/Implementation/Jumping%20on%20the%20Clouds/solution.cpp)||[View](/Algorithms/Implementation/Jumping%20on%20the%20Clouds/solution.py)|
104104
||[Repeated String](https://www.hackerrank.com/challenges/repeated-string/problem)|Easy|[View](Algorithms/Implementation/Repeated%20String/solution.cpp)||[View](/Algorithms/Implementation/Repeated%20String/solution.py)|
105105
||[Jim and the Orders](https://www.hackerrank.com/challenges/jim-and-the-orders/problem)|Easy|[View](Algorithms/Greedy/Jim%20and%20the%20Orders/solution.cpp)|[View](Algorithms/Greedy/Jim%20and%20the%20Orders/solution.java)|[View](Algorithms/Greedy/Jim%20and%20the%20Orders/solution.py)|
106-
||[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|Easy|[View](Algorithms/Search/Sherlock%20and%20Array/solution.cpp)|||
107-
||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|||
108-
||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|||
106+
||[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|Easy|[View](Algorithms/Search/Sherlock%20and%20Array/solution.cpp)|[View](Algorithms/Search/Sherlock%20and%20Array/solution.java)||
107+
||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)||
108+
||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)||
109109
||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|||
110110
||[Funny String](https://www.hackerrank.com/challenges/funny-string/problem)|Easy|[View](Algorithms/Strings/Funny%20String/solution.cpp)|||
111111
||[Gemstones](https://www.hackerrank.com/challenges/gem-stones/problem)|Easy|[View](Algorithms/Strings/Gemstones/solution.cpp)|||

0 commit comments

Comments
 (0)