-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazon.java
More file actions
79 lines (76 loc) · 2.04 KB
/
Amazon.java
File metadata and controls
79 lines (76 loc) · 2.04 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Program: Find Subarray with Given Sum
// Topic: Arrays
// Description: Reads an array and a target sum, then finds a continuous subarray whose elements add up to the given sum.
// Uses nested loops and sliding window logic to identify the subarray indices.
package Array;
import java.util.*;
/**
*
* @author Samim
*/
public class Amazon {
int ar[];
int Sar[];
int S;
Amazon(int n,int value){
Sar=new int[2];
ar=new int[n];
S=value;
}
public void input(){
Scanner sc=new Scanner (System.in);
System.out.println("Enter The Elements Of Array :");
for(int i=0;i<ar.length;i++){
ar[i]=sc.nextInt();
}
}
public void Subarrya(){
int sum=0;
int flag;
int counter=0;
for(int i=0;i<ar.length;i++){
for(int j=i;j<ar.length;j++){
sum=sum+ar[j];
if(sum==S){
System.out.println("matched");
counter=j;
flag=1;
}
}
int start=0;
sum=sum+ar[i];
while(sum>S && start<i){
sum-=ar[start];
start++;
}
if(sum==S){
Sar[0]=start+1;
Sar[1]=i+1;
break;
}
else
{
Sar[0]=-1;
}
}
}
public void Display(){
for(int i=0;i<2;i++){
System.out.print(Sar[i]+" ");
}
System.out.println();
}
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n;
int rsum;
System.out.println("Enter length Of Array :");
n=sc.nextInt();
System.out.println("Enter Required Sum :");
rsum=sc.nextInt();
Amazon obj=new Amazon(n,rsum);
obj.input();
obj.Subarrya();
obj.Display();
}
}