# My code returns correct result but leet code shows wrong

• import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
public int[] twoSum(int[] nums, int target) {
// Sort the array first in nlogn

``````	if (nums == null || nums.length == 0)
return new int[] { -1, -1 };
Arrays.sort(nums);
int left = 0;
int right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] == target) {
return new int[] { nums[left], nums[right]};
} else if (nums[left] + nums[right] > target) {
right--;
} else {
left++;
}
}
return new int[] { -1, -1 };
}

public static void main(String args[]) {
int[] numbers={3,2,4};
Map<Integer,Integer> numsMap = new HashMap<>();
for (int idx = 0; idx < numbers.length; idx ++) {
numsMap.put(numbers[idx], idx+1);
}
int target=6;
Solution sol = new Solution();
int [] nums = sol.twoSum(numbers, target);
int idx1 = numsMap.get(nums[0]);
int idx2= numsMap.get(nums[1]);

System.out.println("index1="+ idx1+", index2=" + idx2);
}
``````

}

• Sorry the following code is the accepted one

import java.util.Arrays;

public class Solution {
public int[] twoSum(int[] nums, int target) {
// Sort the array first in nlogn

``````	if (nums == null || nums.length == 0)
return new int[] { -1, -1 };

int [] numsCopy = nums.clone();
Arrays.sort(nums);
int left = 0;
int right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] == target) {
break;
} else if (nums[left] + nums[right] > target) {
right--;
} else {
left++;
}
}
int idx1=0,idx2=0;
for (int idx =0; idx < numsCopy.length; idx ++) {
if (nums[left] == numsCopy[idx] && idx1== 0) {
idx1= idx+1;
continue;
}
if (nums[right] == numsCopy[idx] && idx2 == 0) {
idx2= idx+1;
}
}
if (idx1 > idx2 ) {
return new int[] {idx2,idx1};
}
return new int[] { idx1, idx2 };
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.