Current streak:
0 days
Longest streak:
22 days
Less
More
#include <bits/stdc++.h>
string read(int n, vector<int> book, int target)
{
//brute force approach T.C-->O(N) S.C-->O(1)
// for(int i=0;i<book.size();i++)
// {
// for(int j=1;j<book.size();j++)
// {
// if(book[i]+book[j] == target) return "YES";
// }
// }
// return "NO";
// first approach T.C-->O(N) S.C-->O(1)
// sort(book.begin(),book.end());//O(nlogn)
// int i=0,j=book.size()-1;
// while(i<=j)//O(N)
// {
// if(book[i]+book[j] == target)
// return "YES";
// else if(book[i]+book[j]<target)
// {
// i++;
// }
// else
// {
// j--;
// }
// }
// return "NO";
//second approach T.C-->O(N) S.C-->O(N)
//can use map too but why bother to use and make key pair if asked to find the target only
unordered_set<int> s;
int val;
for(int i=0;i<book.size();i++)
{
val=target-book[i];
if(s.find(val)!= s.end())
{
return "YES";
}
else
{
s.insert(book[i]);
}
}
return "NO";
}
#include <bits/stdc++.h>
int minSubarraySum(int arr[], int n, int k)
{
// Write your code here
int i=0,j=0,sum=0,ans=INT_MAX;
while(j<=n-1)
{
sum+=arr[j];
if((j-i+1)<k)
{
j++;
}
else
{
ans=min(ans,sum);
// cout<<ans<<endl;
sum-=arr[i];
i++;
j++;
}
}
return ans;
}
int search(vector<int>& nums, int target) {
// Write Your Code Here
int i=0,j=nums.size()-1;
while(i<=j)
{
int mid=i+(j-i)/2;
if(nums[mid]==target)
return mid;
else if(nums[mid]>target)
{
j=mid-1;
}
else
{
i=mid+1;
}
}
return -1;
}