-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJavaSubArray.java
73 lines (65 loc) · 2.09 KB
/
JavaSubArray.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
https://www.hackerrank.com/challenges/java-negative-subarray/problem
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[] arr=new int[size];
for(int i=0; i<size; i++){
arr[i]=sc.nextInt();
}
int count=0;
//make a sub array and add them and check if it is <0 then increase count
for(int i=0; i<size; i++){
int sum=0;
for(int j=i; j<size; j++){
// if(arr[j]<0){
// //System.out.print(arr[j]+" "); // it will give total no of negative number present in sub array
// // count=count+1;
// }
sum=sum+arr[j];
if(sum<0){
count++;
}
}
}
System.out.println(count);
}
}
// Another Approach
/*
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[] arr=new int[size];
for(int i=0; i<size; i++){
arr[i]=sc.nextInt();
}
int count=0;
//make a sub array and add them and check if it is <0 then increase count
for(int i=0; i<size; i++){
for(int j=i; j<size; j++){
int sum=0;
for(int k=i; k<=j; k++){
// System.out.print(arr[k]+" ");
// to print all elements of sub array
sum=sum+arr[k];
}
if(sum<0){
count++;
}
}
//System.out.println();
}
System.out.println(count);
}
}
*/