Array -18: Find 1st occurrence of a value if adjacent values differ by at most K
Vložit
- čas přidán 16. 09. 2019
- Code: thecodingsimplified.com/find-...
Solution:
Brute force: Iterate each element of array starting from 0th index & check if value matches the given target value.
- Complexity - O(n)
Optimized Solution:
- We can use the given property that adjacent elemnents differ by atmost K
- We'll start from 0th index & take the diff = (difference between it's value & target value) / K.
- Now jump your index to diff as we're sure that if they're differ by at most k, it means target value can't occur in between
- Complexity: worst case O(n), but it'll be optmized & will have less than O(n)
Please check video for more info:
This problem is similar to:
array find value if adjacent values differ by at most k,
array find value in array,
adjacent values differ by atmost k ,
find,
adjacent,
differ by atmost K,
array,
java tutorial,
coding simplified,
java
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimplified.com
I started my CZcams channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.
★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
czcams.com/users/codingsimplif...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
Thak you sir.
Glad, it helped you. Keep Watching our other videos as well.
will the time complexity will be any better than linear search?
sir can you please explain the intution behind this optimization??
worst case?
sir how the diff=math.max(1,diff) is working
it is using the concept of how much difference b/w present element and target value and then comparing with key i.e how much times the difference is greater than key.
goat
Thanks.
Not cleared from start...can you please make a new video on this ?