Good Solution, just need to handle edge cases like {3,2,1} etc
@jaisharma80562 жыл бұрын
what an awesome explaination sir. thank you so much
@ProgrammingTutorials1M2 жыл бұрын
You are most welcome
@faktkhandeshi3 жыл бұрын
You can also solve brute force in single iteration like this 😀 const array = [2, 3, 8, 150, 10, 6, 7, 400]; max = 0; for (let i = 0; i < array.length - 1; i++) { const current = array[i]; const next = array[i + 1]; max = Math.max(max, next - current); console.log(current, next, max); } console.log(max)
@weirdprogrammer4153 жыл бұрын
Doesn't work for your example. Your code is right only when we are asked to find the maximum difference between adjacent elements.
@sidharthmandal99575 жыл бұрын
2nd optimisation code bounced from my head.
@ProgrammingTutorials1M5 жыл бұрын
What's the difficulty you faced in second case?
@edwythefair52153 жыл бұрын
It helped a lot, thank you !
@ProgrammingTutorials1M3 жыл бұрын
You're welcome!
@sunils.33854 жыл бұрын
int a[] = {7,4,8,2,10,1}; int maxdif=a[1]-a[0]; int minele = a[0]; for (int i = 0; i < a.length; i++) { if(a[i]maxdif) { maxdif=a[i]-minele; } } System.out.println(maxdif+" "+minele); but the output is 8 1 not 9 1
@rupampaul5 жыл бұрын
Sir we can sort the array and take the difference between arr[0]-arr[n-1] that will work right?
@rupampaul5 жыл бұрын
@@ProgrammingTutorials1M can u upload some pattern printing videos also? It will be very helpful
@ProgrammingTutorials1M5 жыл бұрын
@@rupampaul If you have any specific question in your mind then let me know. I'll definitely upload the videos in that topic.
@NitinVerma-uj3yp4 жыл бұрын
we can't because of the constraint (j>i)
@ivanchan36734 жыл бұрын
May I ask how can we return an array of length 3, which can show exactly which element minus which element gives us the max difference? In your example, the array would be {5, 13, 8}, because 13-5 = 8 is the max difference. Thank you in advance!
@ProgrammingTutorials1M5 жыл бұрын
Code link is present in the description box.
@yadnyeshrane74484 жыл бұрын
sir plz check whether it is right or not int main() { int a[]={7,9,5,6,13,2}; int diff=a[1]-a[0]; int res; for(int i=0;i
@Jyotigupta-vs4mz Жыл бұрын
brute fore is giving wrong output
@ayushjain97232 жыл бұрын
Nice Explanation Thanks a lot Sir !
@ProgrammingTutorials1M2 жыл бұрын
Most welcome!
@madhuneellapala45802 жыл бұрын
Good explanation but one concern. If we have an array like {50,9,5,6,13,2} then we are getting wrong answer. Pls check it again (first public static int bruteForce method)
@ProgrammingTutorials1M2 жыл бұрын
As per the problem statement it is also mentioned that the larger element appears after the smaller element, please consider this also then check the output.