- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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<size; i++){ myArray[i] = sc.nextInt(); } //Reading the number System.out.println("Enter the required sum: "); int reqSum = sc.nextInt(); System.out.println("The array created is: "+Arrays.toString(myArray)); System.out.println("sub arrays whose sum is: "+reqSum); for(int i=0; i<myArray.length; i++){ int sum = 0; for (int j=i; j<myArray.length; j++){ sum = sum + myArray[j]; if(sum == reqSum){ System.out.println(Arrays.toString(Arrays.copyOfRange(myArray, i, j+1))); } } } } }
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]
- Related Articles
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Get the Triplets in an Array Whose Sum is Equal to a Specific Number in Java
- Find the largest area rectangular sub-matrix whose sum is equal to k in C++
- How do you find the sum of all the numbers in a java array
- Find a Number X whose sum with its digits is equal to N in C++
- Maximum Primes whose sum is equal to given N in C++
- How do you limit an array sub-element in MongoDB?
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Program to find number of sublists whose sum is given target in python
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4

Advertisements