Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ec93df

Browse files
committed
Added code for Finding Sum in array
1 parent 6bc2961 commit 5ec93df

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

‎src/arrays/ArraySumExists.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package arrays;
2+
3+
import java.util.HashSet;
4+
5+
//Given an array of integers and a value, determine if
6+
//there are any two integers in the array which sum equal to the given value.
7+
8+
public class ArraySumExists{
9+
public static void main(String args[]){
10+
int a[] = {5,7,1,2,4,3,8};
11+
int k = 6;
12+
13+
System.out.println(sum_exists(a,k));
14+
}
15+
16+
private static String sum_exists(int[] a, int k) {
17+
18+
HashSet<Integer> values = new HashSet<>();
19+
20+
for(int i = 0; i < a.length; i++){
21+
if (values.contains(k-a[i])){
22+
return "Sum exists : ("+a[i]+","+(k-a[i])+")";
23+
} else{
24+
values.add(a[i]);
25+
}
26+
}
27+
28+
return "Sum not found";
29+
}
30+
}

‎src/arrays/MergeOverlaps.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public static void main(String args[]){
1313
ArrayList<Tuple<Integer,Integer>> test = new ArrayList<Tuple<Integer,Integer>>();
1414

1515

16-
1716
}
1817

1918
public class Pair<F,S>{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /