I felt like doing an algorithm and found this problem on leetcode
Given an array of integers, find two numbers such that they add up to a specific target num
We can do with HashMap and the time complexity would be O(n)
public class ReturnIndicesOfElementsAddToSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 18;
if(!getIndices(nums,target)) {
System.out.println("No such numbers found");
}
}
static boolean getIndices(int[] nums, int target) {
Map indexMap = new HashMap<>();
boolean numFound = false;
for(int i=0;i
}