-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavalab11.java
More file actions
45 lines (41 loc) · 1.23 KB
/
Copy pathjavalab11.java
File metadata and controls
45 lines (41 loc) · 1.23 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
import java.util.Scanner;
public class javalab11
{
public static void main(String []st)
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter limit:");
n = sc.nextInt();
int max = 0;
int min = 0;
int arr [] = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = sc.nextInt();
max = Math.max(max, arr[i]);
min = Math.min(min, arr[i]);
}
System.out.println("enter target:");
int target = sc.nextInt();
int map[] =new int[max-min+1];
for(int i = 0; i< n ; i++)
{
map[arr[i]-min]=i + 1;
}
for(int i = 0; i<n;i++)
{
int complement = target - arr[i];
if (complement >= min && complement <= max && map[complement - min] > 0)
{
int complementIndex = map[complement - min] - 1;
if (complementIndex > i)
{
System.out.println("arr[" + i + "] + arr[" + complementIndex + "] = " + target);
map[arr[i] - min] = 0;
map[complement - min] = 0;
}
}
}
}
}