Find Min and Max element from an Array using less number of Comparisons

preview_player
Показать описание
JOIN ME
—————

COMPLETE PLAYLIST
————————————

INTERVIEW PLAYLIST
————————————

QUICK SHORT VIDEOS
————————————-

Time Lines
=========
0:05 Introduction
0:18 Brute Force Approach
2:22 Best Approach
2:22 Best Approach
4:48 Code Explanation

In this video we will learn how to find the minimum and the maximum element from an array using less number of comparisons.

This is beginner level interview question for computer science and electronics background students. Generally we compare each element with min and max both the variable and that is not good solutions.
In this video i have explained that we can compare elements in pair and get better complexity.

Let me know if there is any doubt, we can discussion in comment section.

#ArrayInterview #Array #dsa #InterviewQuestion #DataStructure
Рекомендации по теме
Комментарии
Автор

Python code for this--
def find_max_min(arr):
max=arr[0]
min=arr[0]
for i in arr:
if i>max:
max=i
elif i<min:
min=i
return max, min
arr=[1, 2, 4, 6, 9]
print(find_max_min(arr))

yashavantsingh
Автор

As per my analysis I observed that there is code redundancy as while loop code is self sufficient to evaluate the max min. There is no need for extra comparison while checking even and odd for first 2 element, we just need to initialise i = 1 and in modulus condition we can update the i = 0 if array length is zero.

public static int[] maxMin(int[] arr) {

int len = arr.length;
if (len == 0) {
throw new must not be null");
}

int max = arr[0];
int min = arr[0];
int i = 1;

if (len % 2 == 0) {
i = 0;
}

while (i < len - 1) {
if (arr[i] > arr[i + 1]) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i + 1] < min) {
min = arr[i + 1];
}

} else {
if (arr[i + 1] > max) {
max = arr[i + 1];
}
if (arr[i] < min) {
min = arr[i];
}
}
i += 2;
}

return new int[]{max, min};
}

ayushsingh
Автор

What if like this ..

for (int i= 2; i<size; i++){
if (arr[i]>max){
max = arr[i];
}
else{
if (arr[i]<min){
min = arr[i];
}
}
}

ajaysinghkushwaha
Автор

Thank you so much Sir! You explain it so nicely. I could get comfortable to solve coding problems because of your video series. Keep up the good work.

pallavichaudhary
Автор

Thanks a lot, finally started array based questions

LeelaseshuKumar
Автор

nice exlanation, but what f arr[i] and arr[i+1] both have equal values???

IjazAhmad-wvqx
Автор

Can we not just sort the array and we will get the min and max directly

PatyBoy-op
Автор

So we can have more efficient and easy way

Sort the array using Array.sort(name of the array)
Print( max = array(n-1), min = array(0)

Sort will be nlogn- no need to worry about.

Healthyfactsco
Автор

Why the difference between even or oddd in initialisation

humaneone
Автор

which coding language is used c or c++?

tushargupta
Автор

Sir ye program konse language me hai please batao

Sheru
Автор

Simply reading a code is not called explaining! Clickbait. Don't share half knowledge

AbhrajyotiKundu
welcome to shbcf.ru