-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubArrayOfSum.java
More file actions
44 lines (40 loc) · 1.53 KB
/
SubArrayOfSum.java
File metadata and controls
44 lines (40 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package prepare;
import java.util.Arrays;
import java.util.Scanner;
public class SubArrayOfSum {
public static void main(String[] args) {
System.out.println("Enter the size of the array : ");
try (Scanner s = new Scanner(System.in)) {
int size = s.nextInt();
System.out.println("Enter the elements of the array : ");
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
System.out.println(Arrays.toString(arr));
System.out.println("Enter the sum to be found in subarray : ");
int sumToFind = s.nextInt();
printSubarrayWithGivenSum(arr, size, sumToFind);
}
}
// creating the function for the finding the subarray of the array where the sum
// is matched with the indices...
static void printSubarrayWithGivenSum(int[] arr, int n, int sum) {
// A nested loop to get all subarray
for (int i = 0; i < n - 1; i++) {
int currentSum = arr[i];
if (currentSum == sum) {
System.out.println("Sum find at the indices " + i);
return;
} else {
for (int j = i + 1; j < n; j++) {
currentSum += arr[j];
if (currentSum == sum) {
System.out.println("Sum is finding in between the indices " + i + " and " + j);
return;
}
}
}
}
}
}