-
-
Save techisbeautiful/1379f488ed471c3fd3ca8b53b3d96b5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
class TwoSumSolution1{ | |
static int[] findTwoSumSolution1(int nums[], int target) { | |
int length = nums.length; | |
for (int i = 0; i < (length - 1); i++) { | |
for (int j = (i + 1); j < length; j++) { | |
if (nums[i] + nums[j] == target) { | |
int result[] = { i, j }; | |
return result; | |
} | |
} | |
} | |
return null; | |
} | |
public static void main(String [] args) { | |
int testCase1nums[] = {5, 7, 12,1}; | |
int testCase1Target = 12; | |
int testCase2nums[] = {3, 5, 0}; | |
int testCase2Target = 6; | |
int testCase3nums[] = {0, 3}; | |
int testCase3Target = 3; | |
int testCase4nums[] = {0, -1, 2, -3, 1}; | |
int testCase4Target = -2; | |
int testCase5nums[] = {4, -11, 22, -23, 31}; | |
int testCase5Target = -20; | |
System.out.println(Arrays.toString(findTwoSumSolution1(testCase1nums,testCase1Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution1(testCase2nums,testCase2Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution1(testCase3nums,testCase3Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution1(testCase4nums,testCase4Target))); | |
System.out.println(Arrays.toString(findTwoSumSolution1(testCase5nums,testCase5Target))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment