Skip to content

Commit d935dfc

Browse files
solves two sum
0 parents  commit d935dfc

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
out

CONTRIBUTING.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Contributing Guidelines
2+
3+
This project aims to create a common source from all algorithms questions on HackerRank and values every single
4+
contribution made by the community.
5+
6+
We aim to solve all questions in the HackerRank Algorithms domain in as many languages as possible to show the
7+
different code styles and approaches that can be used to solve the same problem.
8+
9+
Contributing is very simple, simply identify a question from the [README](README.md) file that has yet to be solved in
10+
a programming language of your choice.
11+
12+
Solve that question and make sure that all tests pass on HackerRank and then create a pull request with the
13+
added solution and updated readme.
14+
15+
## Pull Request Process
16+
17+
1. Fork this repository to your github account and then clone your forked repository to your machine.
18+
2. Say you solve some problem called __cars-and-trucks__ in the _Implementation_ sub domain on
19+
HackerRank using your favorite programming language, say Python then simple add a new file called
20+
`cars-and-trucks.py` in the `implimentation` directory.
21+
3. In the [README](README.md) file, add a Python logo along with an empty link.
22+
4. Updating the link for the solution will be done by the Project maintainers (aka. [anishLearnsToCode]())
23+
24+
## Programming Language Logos
25+
To add a logo in the [README](README.md) table simply add a link with an embedded picture as
26+
```markdown
27+
[![Python](icon-link)](this link will be filled by the author if the pull request is accepted)
28+
```
29+
30+
For the icon link refer to the table below to obtain the link for a specific programming language:
31+
32+
| Programming Language | Icon | Icon Link|
33+
|:--------------------:|:----:|:----:|
34+
| Java | ![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png) | https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png |
35+
| Python | ![Python](https://img.icons8.com/color/35/000000/python.png) | https://img.icons8.com/color/35/000000/python.png |
36+
| C | ![C](https://img.icons8.com/color/35/000000/c-programming.png) | https://img.icons8.com/color/35/000000/c-programming.png |
37+
| C++ | ![C++](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png) | https://img.icons8.com/color/35/000000/c-plus-plus-logo.png |
38+
| C# | ![C#](https://img.icons8.com/color/35/000000/c-sharp-logo.png) | https://img.icons8.com/color/35/000000/c-sharp-logo.png |
39+
| JavaScript | ![JavaScript](https://img.icons8.com/color/40/000000/javascript.png) | https://img.icons8.com/color/40/000000/javascript.png |
40+
| TypeScript | ![TypeScript](https://img.icons8.com/color/40/000000/typescript.png) | https://img.icons8.com/color/40/000000/typescript.png |
41+
| Ruby | ![Ruby](https://img.icons8.com/office/35/000000/ruby-programming-language.png) | https://img.icons8.com/office/35/000000/ruby-programming-language.png |
42+
| Scala | ![Scala](https://img.icons8.com/dusk/35/000000/scala.png) | https://img.icons8.com/office/35/000000/ruby-programming-language.png |
43+
| PGH | ![PHP](https://img.icons8.com/officel/40/000000/php-logo.png) | https://img.icons8.com/officel/40/000000/php-logo.png |
44+
| Swift | ![Swift](https://img.icons8.com/fluent/40/000000/swift.png) | https://img.icons8.com/fluent/40/000000/swift.png |
45+
46+
47+
48+
49+
50+
51+
52+

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Leetcode Algorithms
2+
3+
## Problems
4+
| # Number | Name | Difficulty | Solution |
5+
|:--------:|------|:----------:|:--------:|
6+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | |

leetcode-algorithms.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

src/TwoSum.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://leetcode.com/problems/two-sum/
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Scanner;
7+
8+
public class TwoSum {
9+
private static final Scanner scanner = new Scanner(System.in);
10+
11+
public static void main(String[] args) {
12+
int length = scanner.nextInt();
13+
int[] array = getArray(length);
14+
int target = scanner.nextInt();
15+
System.out.println(Arrays.toString(twoSum(array, target)));
16+
}
17+
18+
private static int[] getArray(int length) {
19+
int[] array = new int[length];
20+
for (int index = 0 ; index < array.length ; index++) {
21+
array[index] = scanner.nextInt();
22+
}
23+
return array;
24+
}
25+
26+
public static int[] twoSum(int[] array, int target) {
27+
Map<Integer, Integer> indices = getIndices(array);
28+
for (int index = 0 ; index < array.length ; index++) {
29+
int number = array[index];
30+
if (indices.containsKey(target - number) && indices.get(target - number) != index) {
31+
return new int[] {index, indices.get(target - number)};
32+
}
33+
}
34+
return new int[0];
35+
}
36+
37+
private static Map<Integer, Integer> getIndices(int[] array) {
38+
Map<Integer, Integer> indices = new HashMap<>();
39+
for (int index = 0 ; index < array.length ; index++) {
40+
indices.put(array[index], index);
41+
}
42+
return indices;
43+
}
44+
}

0 commit comments

Comments
 (0)