Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem Statement:

Given an array of n integers, find and print its number of negative subarrays on a new line.(A subarray is negative if the total sum of its elements is negative.)

Sample Input

5
1 -2 4 -5 1

Sample Output

9

My code threw previously (before editing)

Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Solution.main(Solution.java:22)


The present output for the code after edit is.

Quote:
Input (stdin)
5
1 -2 4 -5 1
Your Output (stdout)
7
Expected Output
9
Compiler Message
Wrong Answer


Where am I going wrong ?

This is my code
AFTER THE EDIT

C#
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        int count=0;
        int i,j,sum = 0;
        for(i=0;i<n;i++)
        {
            a[i] = scan.nextInt();
        }
        for(i=0;i<n;i++)
        {
            if(a[i]<0){count++;}
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                sum = a[i] + sum;
                b[j] = sum;
            }
        }
        for(j=0;j<n;j++)
        {
            if(b[j]<0){count++;}
        }
        System.out.println(count);

    }
}
Posted
Updated 3-Oct-16 18:17pm
v13
Comments
[no name] 3-Oct-16 8:13am    
"Where am I going wrong", you are going wrong by trying to access an element of an array that doesn't exist.
GaneshRfromSpace 3-Oct-16 8:25am    
Yea that was a blunder from my side. Sorry . I have edited the query now.

Java
sum = a[i]+a[i+1];

If i points to the last element of the array then i + 1 will give the exception. You need to find a better method of scanning your array for negative values.
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Pay attention to what you do with sum:
Java
sum = a[i] + sum;


Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
 
Share this answer
 
v3
From the above answers I have made changes to my code and it is working fine now. Thanks.
Here is the code.

Java
import java.util.*;
public class Solution {

	  public static void main(String[] args) {
	        Scanner scan = new Scanner(System.in);
	        int n = scan.nextInt();
	        int a[] = new int[n];
	        int count=0;
	        int i,j,sum = 0;
	        for(i=0;i<n;i++)
	        {
	            a[i] = scan.nextInt();
	        }
	        scan.close();
	        for(i=0;i<n;i++)
	        {
	        	sum = 0;
	            for(j=i;j<n;j++)
	            {
	                sum = a[j] + sum;
	                if(sum<0){
		                count++;
	                }
	            }
	        }
	        System.out.println(count);
	    }
	}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900