Skip to content
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

Min Max Functions in Array #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Array_Functions/Min_Max_Functions/min_max.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# include <iostream>
# include <vector>
using namespace std;

vector<int> max_ind(int arr[],int n){
vector<int> mx_ind_v;
int mx_ind=0; //initializing the index with the max value as 0
for(int i=0; i<n;i++){
if (arr[i]>arr[mx_ind]){
mx_ind=i;
mx_ind_v.clear();
mx_ind_v.push_back(mx_ind); //updating the max index
}
else if(arr[i]==arr[mx_ind]){
mx_ind_v.push_back(i); //handling edge cases in case of multiple max indexes
}
}
return mx_ind_v;
}
vector<int> min_ind(int arr[], int n){
vector<int> mn_ind_v={};
int mn_ind=0; //initialising the index with the minimum value as 0
for(int i=0; i<n;i++){
if (arr[i]<arr[mn_ind]){
mn_ind=i; //updating the min index
mn_ind_v.clear();
mn_ind_v.push_back(mn_ind);
}
else if(arr[i]==arr[mn_ind]){
mn_ind_v.push_back(i); //handling edge case in case of multiple min indexes
}
}
return mn_ind_v;
}



int main(){
int k []={8,7,8,5,6,5};//sample test case with edge case for max_ind and min_ind functions
int n = (sizeof(k)/sizeof(k[0]));
vector<int> a = max_ind(k,n);
vector<int> b = min_ind(k,n);
return 0;

}
42 changes: 42 additions & 0 deletions Array_Functions/Min_Max_Functions/min_max.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.ArrayList;

public class min_max {
public static ArrayList<Integer> max_ind( int [] arr){
int mx_ind = 0; //initializing the index with the max value as 0
ArrayList<Integer> mx_ind_arr = new ArrayList<>();
for(int i = 0; i< arr.length; i++){
if(arr[i]> arr[mx_ind]){
mx_ind=i; //updating the max index
mx_ind_arr.clear();
mx_ind_arr.add(i);
}
else if( arr[mx_ind]== arr[i]){
mx_ind_arr.add(i); //handling edge cases in case of multiple max indexes
}
}
return mx_ind_arr;
}

public static ArrayList<Integer> min_ind( int [] arr){
int mn_ind = 0; //initialising the index with the minimum value as 0
ArrayList<Integer> mn_ind_arr = new ArrayList<>();
for(int i = 0; i< arr.length; i++){
if(arr[i] < arr[mn_ind]){
mn_ind=i; //updating the min index
mn_ind_arr.clear();
mn_ind_arr.add(i);
}
else if( arr[mn_ind]== arr[i]){
mn_ind_arr.add(i);
}
}
return mn_ind_arr;
}

public static void main(String[] args) {
int [] k = {8,7,8,5,6,5}; //sample test case with edge case for max_ind and min_ind functions
System.out.println(max_ind(k));
System.out.println(min_ind(k));

}
}
27 changes: 27 additions & 0 deletions Array_Functions/Min_Max_Functions/min_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def max_ind(arr):
mx_ind=0 #initializing the index with the max value as 0
l=[]
for i in range(len(arr)):
if arr[i]>arr[mx_ind]:
l=[] #handling edge cases in case of multiple max indexes
l.append(i)
mx_ind=i #updating the max index
elif arr[i]==arr[mx_ind]:
l.append(i)
return l

def min_ind(arr):
mn_ind=0 #initialising the index with the minimum value as 0
l=[]
for i in range(len(arr)):
if arr[i]<arr[mn_ind]:
l=[] #handling edge case in case of multiple min indexes
l.append(i)
mn_ind=i #updating the min index
elif arr[i]==arr[mn_ind]:
l.append(i)
return l

if __name__ == "__main__":
print(max_ind([3,3,2,1]))
print(min_ind([0,8,8,6,0]))
2 changes: 1 addition & 1 deletion RoadMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ These are fundamental algorithms and functions often used in competitive program
### 1. Array functions
- [x] [Reverse an Array (copy)](Array_Functions/Reverse_an_Array)
- [x] [Reverse an Array (in place)](Array_Functions/Reversing_an_array(in_place))
- [ ] Find Max-Min of Array
- [x] [Find Max-Min of Array](Array_Functions/Min_Max_Functions)
- [ ] Prefix Array
- [ ] Suffix Array
- [ ] Finding Mex of Array (Smallest Positive integer which is not present in array)
Expand Down