-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question57.java
35 lines (29 loc) · 924 Bytes
/
Question57.java
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
import java.util.*;
class problem2{
int max(int[] array){
Arrays.sort(array);
int min=0,max= array.length-1;
while (min<=max){
if (array[min]+array[max]==0){
return array[max];
} else if (array[min]+array[max]>0) {
max--;
} else if (array[min]+array[max]<0) {
min++;
}
}
return -1;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int arr[]=new int[size];
for (int i=0;i<arr.length;i++){
System.out.println("Enter the elements of the array");
arr[i]=sc.nextInt();
}
problem2 p=new problem2();
System.out.println("");
System.out.println("The Largest positive number that exist with its negative in the array: "+p.max(arr));
}
}