-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck.java
More file actions
35 lines (31 loc) · 913 Bytes
/
Check.java
File metadata and controls
35 lines (31 loc) · 913 Bytes
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
35
public class Check {
/*find the subarray with largest sum and return its sum */
public static int maxArray(int []nums){
int n= nums.length;
int max = Integer.MIN_VALUE;
int sum = 0;
for(int i=0; i<n; i++){
sum += nums[i];
max=Math.max(sum, max);
if (sum<0){
sum=0;
}
}
return max;
}
public static int[] twoSum(int []nums , int target){
for(int i=0; i<nums.length; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]+nums[j]==target){
return new int[]{i,j};
}
}
}
return new int [] {};
}
public static void main(String args[]){
int nums[]={2,3,4,6,8};
System.out.println( maxArray(nums));
System.out.println(twoSum(nums, 9));
}
}