You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Two-sum/Approach-to-solve.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,10 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
50
50
}
51
51
return ans;
52
52
53
+
<<<<<<< HEAD
54
+
=======
55
+
56
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
53
57
\*\*(For full code , refer to the .cpp file)
54
58
55
59
<h2>Complexity Analysis</h2>
@@ -61,6 +65,10 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
61
65
<span> - since we are not using any additional data structures.</span></li>
62
66
</ul>
63
67
68
+
<<<<<<< HEAD
69
+
=======
70
+
71
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
64
72
<hr>
65
73
<h2>Approach2: Optimal Apporoach</h2>
66
74
<h3>1. Optimal Solution: Using an unordered maps and 2 loops(not nested):</h3>
@@ -72,6 +80,10 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
72
80
<li>If the complement exists and it's not the same element, it adds the current index i and the index of the complement stored in the map to the result vector ans.</li>
73
81
</ul>
74
82
83
+
<<<<<<< HEAD
84
+
=======
85
+
86
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
75
87
<h3>Code Explanation:</h3>
76
88
<ul>
77
89
<li>Step 1: Initialization: <ul>
@@ -93,6 +105,10 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
93
105
94
106
</ul>
95
107
108
+
<<<<<<< HEAD
109
+
=======
110
+
111
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
96
112
vector<int> ans;
97
113
unordered_map<int, int> hash;
98
114
@@ -124,6 +140,11 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
124
140
<span> - due to the additional space required for the unordered_map.</span></li>
125
141
</ul>
126
142
143
+
<<<<<<< HEAD
144
+
=======
145
+
146
+
147
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
127
148
<hr>
128
149
<h2>Approach3: Most Optimal Apporoach: </h2>
129
150
<h3>1. Optimal Solution:Using unordered_map and One For Loop:</h3>
@@ -137,6 +158,11 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
137
158
<li>Update the unordered_map hash by inserting or updating the current element nums[i] and its index i.</li></ul>
138
159
</ul>
139
160
161
+
<<<<<<< HEAD
162
+
=======
163
+
164
+
165
+
>>>>>>> ae9917feeb2a6b3a1183b7502f8a6efda2d05aae
140
166
unordered_map<int, int> hash;
141
167
vector<int> ans;
142
168
@@ -161,3 +187,10 @@ In the given input array, nums[0] + nums[1] = 2 + 7 = 9, which matches the targe
161
187
<li><h3>Space Complexity: O(n)</h3>
162
188
<span> - due to the additional space required for the unordered_map.</span></li>
0 commit comments