Skip to content

Commit 7fba646

Browse files
Create 09-03-2024.cpp
1 parent 8f2889e commit 7fba646

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

2024/03 - March/09-03-2024.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Author : Saransh Bangar
3+
Date : 09/03/2024
4+
Problem : Minimum Common Value
5+
Difficulty : Easy
6+
Problem Link : https://leetcode.com/problems/minimum-common-value/description/
7+
Video Solution : NA
8+
*/
9+
10+
11+
class Solution {
12+
public:
13+
bool func(vector<int>& vec, int k)
14+
{
15+
int low=0, high=vec.size()-1;
16+
while (low<=high)
17+
{
18+
int mid=low+((high-low)/2);
19+
if (vec[mid]==k)
20+
return true;
21+
else if (vec[mid]>k)
22+
high=mid-1;
23+
else low=mid+1;
24+
}
25+
return false;
26+
}
27+
int getCommon(vector<int>& nums1, vector<int>& nums2)
28+
{
29+
int ans;
30+
for (int i=0, j=0;i<nums1.size() && j<nums2.size();)
31+
{
32+
if (nums1[i]>nums2[j])
33+
{
34+
if (func(nums1, nums2[j]))
35+
return nums2[j];
36+
else j++;
37+
}
38+
else
39+
{
40+
if (func(nums2, nums1[i]))
41+
return nums1[i];
42+
else i++;
43+
}
44+
}
45+
return -1;
46+
}
47+
};

0 commit comments

Comments
 (0)