Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do you find continuous sub array whose sum is equal to a given number in Java?
To find continuous sub array whose sum is equal to a given number −
- Iterate through the array.
- At each element add the next n elements one by one, when the sum equals to the required value print the sub array.
Example
import java.util.Arrays;
import java.util.Scanner;
public class sub_arrays {
public static void main(String args[]){
//Reading the array from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array that is to be created: ");
int size = sc.nextInt();
int[] myArray = new int[size];
System.out.println("Enter the elements of the array: ");
for(int i=0; i
Output
Enter the size of the array that is to be created:
10
Enter the elements of the array:
5
4
1
2
3
4
1
4
5
5
Enter the required sum:
10
The array created is: [5, 4, 1, 2, 3, 4, 1, 4, 5, 5]
sub arrays whose sum is: 10
[5, 4, 1]
[4, 1, 2, 3]
[1, 2, 3, 4]
[2, 3, 4, 1]
[1, 4, 5]
[5, 5]
Advertisements
