You are viewing a single comment's thread. Return to all comments →
import java.io.*; import java.util.*; public class Solution { public static int countNegativeSubArrays(int[] arr,int n){ int result = 0; for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ int temp = 0; for(int k=i;k<=j && k<n;k++){ temp += arr[k]; } if((j==i && arr[i]<0) || temp<0) result++; } } return result; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n;i++){ arr[i] = scan.nextInt(); } System.out.print(countNegativeSubArrays(arr,n)); scan.close(); } }
Brute force approach by moving start and end index
Seems like cookies are disabled on this browser, please enable them to open this website
Java Subarray
You are viewing a single comment's thread. Return to all comments →
Brute force approach by moving start and end index