-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarrayWithGivenSum.java
More file actions
31 lines (29 loc) · 879 Bytes
/
SubarrayWithGivenSum.java
File metadata and controls
31 lines (29 loc) · 879 Bytes
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
package myprog;
import java.util.Scanner;
public class subarrayofsum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements : ");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements : ");
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter the required sum : ");
int s = sc.nextInt();
int sum = 0;
int i=0,j=0;
for (i = 0; i < n; i++) {
sum = a[i];
for (j = i+1; j < n; j++) {
sum = sum + a[j];
if(sum == s)
{
System.out.println((i+1)+" to "+(j+1));
}
}
}
}
}