Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed of Two Sum problem #8

Open
tanzirbd opened this issue Mar 16, 2024 · 0 comments
Open

Fixed of Two Sum problem #8

tanzirbd opened this issue Mar 16, 2024 · 0 comments

Comments

@tanzirbd
Copy link

tanzirbd commented Mar 16, 2024

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] copyArray = Arrays.copyOf(nums, nums.length);
Arrays.sort(copyArray);
int i=0, j=copyArray.length-1;
int[] arr= new int[2];
int num1=0, num2=0;
while(i<j){
int sum= copyArray[i] + copyArray[j];
if(sum < target ) i++;
else if(sum > target) j--;
else {
num1=copyArray[i];
num2=copyArray[j];
break;
}
}
for(int k=0;k<nums.length;k++){
if(num1==nums[k] && arr[0]==0) arr[0]=k;
else if(num2==nums[k]) arr[1]=k;
}
return arr;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant