Created
October 15, 2022 08:58
-
-
Save jsbonso/1c1b09fec00d6af0476fa84a414020b3 to your computer and use it in GitHub Desktop.
Two Sum Solution #1 - Brute Force Approach
This file contains hidden or 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
/** | |
* | |
Two Sum Solution #1 - Brute Force Approach | |
Approach | |
- Brute force approach by using a nested loop | |
- Create the two loops to process the array, wherein the second loop is one step ahead of the previous loop and traversing the array elements in a quadratic manner | |
- The first loop uses the variable i to hold the first index while the second loop uses the variable j to hold the latter one. | |
- Compare the values of the i and j indices. If they are the same, return the indices as output | |
- Return an empty integer array if no match was found | |
Complexity | |
- Time complexity: O(n^2) | |
- Space complexity: O(1) | |
*/ | |
class TwoSumBruteForce { | |
public int[] twoSum(int[] nums, int target) { | |
for (int i=0; i < nums.length ;i++){ | |
for (int j=i+1; j < nums.length ;j++){ | |
if ((nums[i] + nums[j]) == target){ | |
return new int[]{i,j}; | |
} | |
} | |
} | |
// returns an empty array if no match found | |
return new int[]{}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision: