content: package org.redquark.tutorials.leetcode;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * @author Anirudh Sharma
   */
  public class TwoSum {
  
      /**
       * @param nums   - input array of numbers
       * @param target - the value of target
       * @return - array of indices of two numbers which will add up to target
       */
      private static int[] twoSum(int[] nums, int target) {
          // Array to store result
          int[] result = new int[2];
          // This map will store the difference and the corresponding index
          Map<Integer, Integer> map = new HashMap<>();
          // Loop through the entire array
          for (int i = 0; i < nums.length; i++) {
              // If we have seen the current element before
              // It means we have already encountered the other number of the pair
              if (map.containsKey(nums[i])) {
                  // Index of the current element
                  result[0] = i;
                  // Index of the other element of the pair
                  result[1] = map.get(nums[i]);
                  break;
              }
              // If we have not seen the current before
              // It means we have not yet encountered any number of the pair
              else {
                  // Save the difference of the target and the current element
                  // with the index of the current element
                  map.put(target - nums[i], i);
              }
          }
          return result;
      }
  
      public static void main(String[] args) {
          int[] nums = new int[]{2, 7, 11, 15};
          int target = 9;
          System.out.println("The indices of Pair: " + Arrays.toString(twoSum(nums, target)));
  
          nums = new int[]{3, 2, 4};
          target = 6;
          System.out.println("The indices of Pair: " + Arrays.toString(twoSum(nums, target)));
      }
  }
  