So I have a List in which there are say 2 elements (suppose [1,2]), now the return type of method is int[] so I need to convert this Listto int[].
Here is my so far code,
public int[] twoSum(int[] nums, int target) {
List<Integer> l1 = new ArrayList<Integer>();
int lengthOfnums = nums.length;
int[] indexOfNums = new int[nums.length];
if(lengthOfnums != 0)
{
for(int i=0; i <=lengthOfnums-1; i++)
{
for(int j =1; j<=lengthOfnums-1;j++)
{
if(nums[i] + nums[j] ==target)
{
//l1.add(i);
//l1.add(j);
indexOfNums[i]=i;
indexOfNums[i+1]=j; // here somewhere logic goes boom
}
}
}
}
return indexOfNums;
}
When I use (to operate on l1 arrayList)
Integer[] boxed = l1.stream().filter(Objects::nonNull).toArray(Integer[]::new);
indexOfNums = ArrayUtils.toPrimitive(boxed);
It gives output something like - [I@6bf2d08e, I think it is something related to address of memory, (not sure on this part). So this one didn't work.
Also I can not use the for loop as I have to think about the complexity O(n) for my method. Already complexity has reached to O(n)2 (square).
**What I want is ** if sum of two numbers is target then I want to get the index of those numbers.