I don’t know why it is not in the solution review, but the problem is solvable in O(n)
public static int[] findSum(int[] arr, int n)
{
int[] result = new int[2];
Map<Integer, Integer> dic = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
Integer difference = n - arr[i];
if (dic.containsKey(difference)) {
result[0] = arr[i];
result[1] = difference;
break;
}
dic.put(arr[i], i);
}
// write your code here
return result; // return the elements in the array whose sum is equal to the value passed as parameter
}
Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers