-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoSumOptimal.java
43 lines (38 loc) · 1.46 KB
/
twoSumOptimal.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
36
37
38
39
40
41
42
43
package com.pluralsight.calcengine;
import java.util.Arrays;
import java.util.Scanner;
public class twoSumOptimal {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); //new instance and calling the input func
System.out.println("Enter the length of the array:");
int length = s.nextInt(); //defining size and getting the input
int[] array = new int[length]; // defining array of length provided
System.out.println("Enter the elements of the array:");
for (int i = 0; i < length; i++) {
array[i] = s.nextInt();
}
System.out.println("Enter the target:");
int target = s.nextInt();
// int[] array = new int[]{3, 5, -4, 8, 11, 1, -1, 6};
//nlog(n) |O(n)
Arrays.sort(array);
int end = 0;
int left = 0;
int right = array.length - 1;
while (left < right) {
int currentSum = array[left] + array[right];
if (currentSum == target) {
System.out.println("Output has been found " + array[left] + array[right]);
left++; //finds all left values
right--; //finds all right values
} else if (currentSum < target) {
left++;
}
else if (currentSum > target){
right--;}
else {
System.out.println("Not found");
}
}
}
}