Instead of going through the code line by line , i think you should focus on dry running the solution on any example. Will make things more clear. Everyone knows what a single piece of line is doing , no need to explain that.
u shud focus on the approach more and on the implementation less, going thru the snap of a code snippet is not going to HELP the viewers, viewers need to visualize the flow, implementation is what any one can do once the flow is clear!!!
Another approach arr=[5,84,8,8,1,6,90] max=arr[-1] diff=0 for i in range(-2,-len(arr)-1,-1): if arr[i]diff: diff=max-arr[i] else: max=arr[i] print(diff)
Another O(n) solution with O(1) space could be Start from rightmost element and obtain the max element for any element at index i (from i+1 to nth element ) And side by side updating the max difference found so far with only case that maximum element found at the right of any element must be greater than the element This requires n operations without using any extra space
Will it work for below input array? int arr[] = { 6, 7, 9, 5, 6, 3, 2 }; Expected Output: 2 => (9 - 7), However, this code will provide output as 3? I have written below code which will take care for above input. You may have to correct geeksforgeek as applicable? #include #include #include #include using namespace std; void printArray(vector a) { for(vector::iterator itr=a.begin(); itr!=a.end(); itr++) { cout
Your expected output is incorrect. Either you didn't understand the question properly or you just made an innocent mistake. The expected output is 3 (6-9) They need not be adjacent elements.
Instead of going through the code line by line , i think you should focus on dry running the solution on any example. Will make things more clear. Everyone knows what a single piece of line is doing , no need to explain that.
You are just reading the code line by line. Please explain it more efficiently.
u shud focus on the approach more and on the implementation less, going thru the snap of a code snippet is not going to HELP the viewers, viewers need to visualize the flow, implementation is what any one can do once the flow is clear!!!
The tricky solution was awesome. Thanks GfG and thanks a lot Harshit
Thanks Mazhar :)
how are you sure that arr1-arr0 will always return a postive integer
I got this problem in an interview for an internship at a startup
Another approach
arr=[5,84,8,8,1,6,90]
max=arr[-1]
diff=0
for i in range(-2,-len(arr)-1,-1):
if arr[i]diff:
diff=max-arr[i]
else:
max=arr[i]
print(diff)
roughly explain the analysis of each line
zzz every 1 here knows English , you just read whats written
Another O(n) solution with O(1) space could be
Start from rightmost element and obtain the max element for any element at index i (from i+1 to nth element )
And side by side updating the max difference found so far with only case that maximum element found at the right of any element must be greater than the element
This requires n operations without using any extra space
But bro this could take n2. See this test case 5 4 3 2 1
@@akshatjain6854 but I think it's worst case. Avg case would still be o(N).
We can read ourselves..go through hand simulation
What will be output for these inputs
Ip : 1 2 90 10 -110
I want a program to consider my negative value too
10-1 =9
89 because 90 will be the greatest element in the array and the smaller element in the array before 90 is 1 so 90-1=89.
How to be able to think faster and get quicker answer? please help
Practice is the only key.
not clear
yet another cool video from G4G!
Thanks reyou7!
Will it work for below input array?
int arr[] = { 6, 7, 9, 5, 6, 3, 2 };
Expected Output: 2 => (9 - 7), However, this code will provide output as 3?
I have written below code which will take care for above input. You may have to correct geeksforgeek as applicable?
#include
#include
#include
#include
using namespace std;
void printArray(vector a)
{
for(vector::iterator itr=a.begin(); itr!=a.end(); itr++)
{
cout
Your expected output is incorrect. Either you didn't understand the question properly or you just made an innocent mistake.
The expected output is 3 (6-9) They need not be adjacent elements.
Worst explanation ...just reading the code doesn't help much ... please understand the logic don't read