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 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[]{}; | |
} | |
} |
Revision:
/**
*
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) where n is the number of input.
- Space complexity: O(1)
*/
import java.util.Arrays;
class Main {
public static void main(String args[]){
int nums[] = {3,4,8,11,5};
int target = 10;
int indices[] = twoSum(nums,target);
if (indices.length > 0){
System.out.println("Result: " + Arrays.toString(indices));
System.out.println("The sum of : " + nums[0] + " + " +nums[1] + " is " + target );
} else{
System.out.println("No match found.");
}
}
public static int[] twoSum(int[] nums, int target) {
int ctr = 0;
for (int i=0; i < nums.length ;i++){
for (int j=i+1; j < nums.length ;j++){
// Don't allow the same number
if (nums[i] == nums[j]){
continue;
}
System.out.println(nums[i] + " "+ nums[j]);
if ((nums[i] + nums[j]) == target){
System.out.println("Total number of steps: " + ctr);
return new int[]{i,j};
}
ctr++;
}
System.out.println();
}
System.out.println("Total number of steps: " + ctr);
// 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
Online demo: https://www.online-java.com/I7FO5Exn0X