-1

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.

Kavita Kulkarni
  • 193
  • 2
  • 4
  • 12
  • No, I can not use for loop. If its possible. @MCEmperor – Kavita Kulkarni Jul 12 '21 at 09:21
  • 4
    There are *many* possible solutions out there, including `list.stream().filter(Objects::nonNull).mapToInt(i -> i).toArray()`. And using streams instead of a for loop won't change the complexity. – MC Emperor Jul 12 '21 at 09:24

1 Answers1

5

This can be done by converting the Stream<Integer> into a IntStream which has a toArray() method which generates an int[]

l1.stream().filter(Objects::nonNull).mapToInt(Integer::intValue).toArray()
Yayotrón
  • 1,759
  • 16
  • 27
  • If I use this, it gives indexOfNums =l1.stream().filter(Objects::nonNull).mapToInt(Integer::intValue).toArray(); System.out.println(indexOfNums); //[I@4cdf35a9 [I@4cdf35a9 - this is coming as output – Kavita Kulkarni Jul 12 '21 at 09:28
  • 2
    @KavitaKulkarni But that's not because there's anything wrong with this answer; that's because you try to print an array directly. Try this instead: `System.out.println(Arrays.toString(indexOfNums));` – Jesper Jul 12 '21 at 09:33
  • yes, this works, but return type of method is int[]. So I can not write down return Arrays.toString(indexOfNums); @Jesper – Kavita Kulkarni Jul 12 '21 at 09:38
  • 1
    Now you are changing the problem! If your end goal is to produce a `String`, don't convert the `List` into an array. – Stephen C Jul 12 '21 at 09:42
  • And besides, there >>is<< an `Arrays.toString(int[])` overload. Check the javadocs. – Stephen C Jul 12 '21 at 09:45
  • No, my requirement is to put the final answer in int[]. I am aware about the fact that if I want to produce a String, I wont need any conversion. I have updated my question with code. @StephenC – Kavita Kulkarni Jul 12 '21 at 13:00