We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Data Structures
- Java Subarray
- Discussions
Java Subarray
Java Subarray
Sort by
recency
|
638 Discussions
|
Please Login in order to post a comment
Hope this helps
Brute force approach by moving start and end index
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.*;
public class Solution {
}
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;
{[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
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);