Problem statement: Given an array of integers, return two numbers such that they add up to a specific target.
e.g. - arr[] = { 4, 6, 5, -10, 8, 5, 20 };
target = 10
Output: [4,6] , [5,5], [-10,20]
Note: You may assume that each input would have exactly one solution, and you may not use the same element twice.
e.g. - arr[] = { 4, 6, 5, -10, 8, 5, 20 };
target = 10
Output: [4,6] , [5,5], [-10,20]
Note: You may assume that each input would have exactly one solution, and you may not use the same element twice.
// Method -I:
Space complexity: O(n) | reason - O(1) + O(n) = O(n).O(1) for the variable and O(n) for the hashmap. for hashmap, with the increase of the number of entries, the hashmap's space will increase linearly. so space complexity of hashmap is O(n).
Output:
pair of num: [4, 6]
pair of num: [5, 5]
pair of num: [-10, 20]
// Method-II: it works only for SORTED arrays
// Method-III:
- import java.util.HashMap;
- import java.util.Map;
- public class PairOfSumByMap {
- public static void main(String[] args) {
- int arr[] = { 4, 6, 5, -10, 8, 5, 20 };
- int num = 10;
- pairOfSum(arr, num);
- }
- public static void pairOfSum(int arr[], int num) {
- Map<Integer, Integer> map = new HashMap<>();
- for (int i = 0; i < arr.length; i++) {
- if (map.containsKey(num - arr[i])) {
- System.out.println("pair of num: [" + (num - arr[i]) + ", " + arr[i] + "]");
- } else
- map.put(arr[i], 0);
- }
- }
- }
Space complexity: O(n) | reason - O(1) + O(n) = O(n).O(1) for the variable and O(n) for the hashmap. for hashmap, with the increase of the number of entries, the hashmap's space will increase linearly. so space complexity of hashmap is O(n).
Output:
pair of num: [4, 6]
pair of num: [5, 5]
pair of num: [-10, 20]
// Method-II: it works only for SORTED arrays
- import java.util.Arrays;
- public class PairOfArraySum {
- static int arr[] = { 4, 6, 5, -10, 8, 5, 20 };
- public static void main(String[] args) {
- Arrays.sort(arr);
- int i = 0; // first index
- int j = arr.length - 1; // last index
- while (i < j) {
- // if arr[i]+arr[j] = num
- if (arr[i] + arr[j] == 10) {
- // then print the pair
- System.out.println("Pair of elements are: (" + arr[i] + "," + arr[j] + ")");
- i++; // increment i
- j--; // decrement j
- }
- // if arr[i]+arr[j]<num
- else if (arr[i] + arr[j] < 10) {
- i++; // then increment i
- }
- // if arr[i] + arr[j] >num
- else if (arr[i] + arr[j] > 10) {
- j--; // then decrement j
- }
- }
- }
- }
Output:
Pair of elements are: (-10,20)
Pair of elements are: (4,6)
Pair of elements are: (5,5)
- public class PairOfSumByBruteForce {
- static int arr[] = { 4, 6, 5, -10, 8, 5, 20 };
- public static void main(String[] args) {
- int sum = 10;
- // brute force
- for (int i = 0; i < arr.length; i++) {
- for (int j = i + 1; j < arr.length; j++) {
- if (arr[i] + arr[j] == sum)
- System.out.println("Pair of elements are: (" + arr[i] + ", " + arr[j] + ")");
- }
- }
- }
- }
Output:
Pair of elements are: (4, 6)
Pair of elements are: (5, 5)
Pair of elements are: (-10, 20)
Time complexity: O(n^n)
No comments:
Post a Comment