Sort by

recency

|

638 Discussions

|

  • + 0 comments

    Hope this helps

        Scanner scanner = new Scanner(System.in);
                int size = scanner.nextInt();
                int[] input = new int[size]; 
    
                for (int i = 0; i < size; i++){
            input[i] = scanner.nextInt();
        }
    
        int count = 0;
        for (int i = 0; i < size; i++){
    
            int sum = 0;
            for (int j = i; j < size; j++){
    
                sum += input[j];
                if (sum < 0) count++; 
            }
        }
        System.out.println(count);
    
  • + 0 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

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; 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 n=sc.nextInt();
        int[] arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        int negCount=0;
        for(int i=0;i<n;i++){
            int num=0;
    
            for(int j=i;j<n;j++){
                num+=arr[j];
                 if(num<0){
                negCount++;
            }
            }
        }
        System.out.println(negCount);
    }
    

    }

  • + 0 comments

    The basic exercise is getting all subsequent arrays without repeating So you need to pass for all elements but don't need to create combinations that are not subsequently.. left to right, you can get this using a bigger array (I'll call move array) to move the edges Representing by {} in this example, for [ I'll call cut edge left, and for ] cut edge right,

    see this example. -> Passing through the array move

    // sum to verify if the sum is negative, // count to verify the quantity of negative sum int sum = 0, count = 0;

    //see {1,2,3,4,5}
    for (int i = 0; i < sizeOfArray; i++) { // move array
    	// the second array starts by the actual index
    	// {1,2,[3],4,5} -> example
    	for (int j = i; j < sizeOfArray; j++) { // edge left
    		sum = 0; 
    		// {1,2,[3,4,5]} -> example
    		// the third array starts by the actual index but goes only two j
    		for (int k = j; k < j; k++) { // right edge
    			// {1,2,[3,4],5} every loop the clip is more strict -> example
    			sum += array[k]
    		}
    		if (sum < 0) count++; // updates the count
    	}
    	
    }
    

    {[1,]2,3,4,5} -> {[1,2],3,4,5} -> {[1,2,3],4,5} => index = 0 {1,[2],3,4,5} -> {1,[2,3],4,5} -> {1,[2,3,4],5} => index = 1 .... {1,2,3,[4,]5} -> {1,2,3,[4,5]} => index = 3 until 4

  • + 0 comments

    It's my solution:

    Scanner scan = new Scanner(System.in); int size = scan.nextInt(); int[] array = new int[size]; for(int i=0; i for (int i=0; i <= j ; i++) {
    a = 0; for(int p = i; p < j ; p++) {
    a += array[p]; } if (a < 0) { negativeSum++; }} } System.out.println(negativeSum);