In simple words # if the current number is greater than ans , than ans can never be formed simply return it # else we can form answer till ans+ arr[i] as everything between it can be formed
The video is exceptionally good!! Itna achhe se question samjhaye Karan sir ne!! Intuition bhi samajh aa gaya aur code to cake-walk tha. Summary: 1. Sort the array so that we move in sequence 2. Iterate from i = 0 till N 3. ans = 1. If we find any number in array which is greater than ans, we return it 4. Else we add the number to the ans 5. At the end, we return ans if we traverse the complete array. Intuition: If nums[i] ans, we cannot form it using the the elements we have encountered till now, so smallest positive is our ans only
In simple words
# if the current number is greater than ans , than ans can never be formed simply return it
# else we can form answer till ans+ arr[i] as everything between it can be formed
This is the focus with which one should watch video....great!!
long long smallestpositive(vector arr, int n)
{
sort(arr.begin(), arr.end());
long long curr = arr[0];
if(arr[0] != 1)
return 1;
for(int i = 1; i curr+1)
return curr+1;
curr += arr[i];
}
return curr+1;
}
TC - NLOGN
SC - 1
The video is exceptionally good!! Itna achhe se question samjhaye Karan sir ne!! Intuition bhi samajh aa gaya aur code to cake-walk tha.
Summary:
1. Sort the array so that we move in sequence
2. Iterate from i = 0 till N
3. ans = 1. If we find any number in array which is greater than ans, we return it
4. Else we add the number to the ans
5. At the end, we return ans if we traverse the complete array.
Intuition:
If nums[i] ans, we cannot form it using the the elements we have encountered till now, so smallest positive is our ans only
Wow , such a nice explanation
Thanks a lot 😊
Very Good Solution Approach
Thanks!!
great explaination
Thanks Gaurav!!
Do share among your friends!!
how you said time complexity of it...i got understand
day 3 problem 1
Hope you reach to very end!!
thanks bro
Welcome
great intuition. Thanks, brother.
Welcome!!!
respect++
Thanks!!