-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
34 lines (28 loc) · 921 Bytes
/
TwoSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package LeetCodeArray;
import java.util.Arrays;
public class TwoSum {
public static int[] twosum(int nums[], int target) {
int[] b = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
b[0] = nums[i];
b[1] = nums[j];
return b;
}
}
}
return b;
}
public static void main(String args[]) {
// muze do paas karne hai ek nums[], target (equals to sum of three integers)
int nums[] = { 2, 7, 11, 15 };
int target = 9;
System.out.println("Two sum is" + Arrays.toString(twosum(nums, target)));
// System.out.println("Two sum is: " + Arrays.toString(twosum(nums, target)));
}
}
// Arrays.toString(): This method is used to convert the array into a readable
// format when printing.
// Without this, the output would just show the memory reference of the array
// rather than the actual numbers.