Find Maximum Difference between Two Array Elements

  Рет қаралды 23,988

Programming Tutorials

Programming Tutorials

Күн бұрын

Пікірлер: 26
@iswariyaravi9134
@iswariyaravi9134 3 жыл бұрын
Man the most efficient solution!!!
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Thank you
@SugamMaheshwari
@SugamMaheshwari 2 жыл бұрын
Good Solution, just need to handle edge cases like {3,2,1} etc
@jaisharma8056
@jaisharma8056 2 жыл бұрын
what an awesome explaination sir. thank you so much
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
You are most welcome
@faktkhandeshi
@faktkhandeshi 3 жыл бұрын
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)
@weirdprogrammer415
@weirdprogrammer415 3 жыл бұрын
Doesn't work for your example. Your code is right only when we are asked to find the maximum difference between adjacent elements.
@sidharthmandal9957
@sidharthmandal9957 5 жыл бұрын
2nd optimisation code bounced from my head.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
What's the difficulty you faced in second case?
@edwythefair5215
@edwythefair5215 3 жыл бұрын
It helped a lot, thank you !
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
You're welcome!
@sunils.3385
@sunils.3385 4 жыл бұрын
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
@rupampaul
@rupampaul 5 жыл бұрын
Sir we can sort the array and take the difference between arr[0]-arr[n-1] that will work right?
@rupampaul
@rupampaul 5 жыл бұрын
@@ProgrammingTutorials1M can u upload some pattern printing videos also? It will be very helpful
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
@@rupampaul If you have any specific question in your mind then let me know. I'll definitely upload the videos in that topic.
@NitinVerma-uj3yp
@NitinVerma-uj3yp 4 жыл бұрын
we can't because of the constraint (j>i)
@ivanchan3673
@ivanchan3673 4 жыл бұрын
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!
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
Code link is present in the description box.
@yadnyeshrane7448
@yadnyeshrane7448 4 жыл бұрын
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
@Jyotigupta-vs4mz Жыл бұрын
brute fore is giving wrong output
@ayushjain9723
@ayushjain9723 2 жыл бұрын
Nice Explanation Thanks a lot Sir !
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
Most welcome!
@madhuneellapala4580
@madhuneellapala4580 2 жыл бұрын
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)
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
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.
@praveenchouhan6388
@praveenchouhan6388 4 жыл бұрын
nice explaination !!!!!thanks a lot!!!!
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
You are welcome!
Sorting Algorithms Explained Visually
9:01
Beyond Fireship
Рет қаралды 537 М.
escape in roblox in real life
00:13
Kan Andrey
Рет қаралды 86 МЛН
Electric Flying Bird with Hanging Wire Automatic for Ceiling Parrot
00:15
Find the Element that Appears Once in a Sorted Array in O(logn)
13:22
Programming Tutorials
Рет қаралды 7 М.
Maximum difference between two elements in array
19:53
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 4,6 М.
Calculate Sum and Average of N Numbers using Arrays: C Program
10:06
Print combinations of r elements in an array of size n
14:09
Techdose
Рет қаралды 61 М.
Leaders in an Array | Brute - Optimal | Strivers A2Z DSA Course
11:53
take U forward
Рет қаралды 134 М.
Find Common Elements in Three Sorted Arrays - Java Code
10:44
Programming Tutorials
Рет қаралды 32 М.
escape in roblox in real life
00:13
Kan Andrey
Рет қаралды 86 МЛН