From 125191703a083fe45d2035ed22b386a7f8a25dce Mon Sep 17 00:00:00 2001 From: Samiksha Manjunath Date: Sat, 16 May 2026 17:41:05 -0700 Subject: [PATCH] Done Competitive-Coding-2 --- Problem1.java | 24 ++++++++++++++++++++++++ Problem2.java | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/Problem1.java b/Problem1.java index e69de29b..cca8deb7 100644 --- a/Problem1.java +++ b/Problem1.java @@ -0,0 +1,24 @@ +// Time Complexity : O(n) because we traverse the array once and HashMap operations take O(1) on average +// Space Complexity : O(n) for storing numbers and their indices in the HashMap +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// We use a HashMap to store each number along with its index as we iterate through the array. +// For every number, we calculate the required complement needed to reach the target and check whether it already exists in the map. +// If the complement exists, we return the stored index and current index as the required pair; otherwise, we store the current number and continue. + +class Solution { + public int[] twoSum(int[] nums, int target) { + Map hashMap = new HashMap<>(); + for (int i=0; i