-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Algo Folder - min_max_array.py #11817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Check failure on line 1 in data_structures/arrays/min_max-element.py
|
||
Author - Seema Kumari Patel - 2024 | ||
|
||
Retrieves minimum and maximum valued number from an array. | ||
There are two ways to retrieve value(s): | ||
|
||
1. Approach #01 -Linear Search. | ||
Initialize values of min and max as minimum and maximum of the first two elements respectively. | ||
Check failure on line 8 in data_structures/arrays/min_max-element.py
|
||
Starting from 3rd, compare each element with max and min, and change max and min accordingly. | ||
Check failure on line 9 in data_structures/arrays/min_max-element.py
|
||
|
||
2. Approach #02 - First sort the array in ascending order. | ||
Once the array is sorted, the first element of the array will be the minimum element and the last element of the array will be the maximum element. | ||
Check failure on line 12 in data_structures/arrays/min_max-element.py
|
||
|
||
Here is the code for second approach | ||
""" | ||
|
||
import java.io.*; | ||
Check failure on line 17 in data_structures/arrays/min_max-element.py
|
||
import java.util.*; | ||
Check failure on line 18 in data_structures/arrays/min_max-element.py
|
||
|
||
|
||
'''To get the values to min / max respectively''' | ||
class Pair { | ||
public int min; | ||
public int max; | ||
} | ||
|
||
'''To sort the array in ascending order''' | ||
class Main { | ||
static Pair getMinMax(int arr[], int n) { | ||
Pair minmax = new Pair(); | ||
Arrays.sort(arr); | ||
minmax.min = arr[0]; | ||
minmax.max = arr[n - 1]; | ||
return minmax; | ||
} | ||
|
||
'''To get the array elements''' | ||
public static void main(String[] args) { | ||
int arr[] = { 1000, 11, 445, 1, 330, 3000 }; | ||
int arr_size = arr.length; | ||
Pair minmax = getMinMax(arr, arr_size); | ||
System.out.println("Minimum element is " + minmax.min); | ||
System.out.println("Maximum element is " + minmax.max); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
data_structures/arrays/min_max-element.py