+ 1
Find the maximum and minimum between n number of array
#include <iostream> using namespace std; int main() { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } int maxNo=0; int minNo =0; for(int i=0;i<n;i++){ maxNo=max(maxNo,arr[i]); minNo=min(minNo,arr[i]); } cout<<maxNo<<" "<<minNo; return 0; }
3 Antworten
+ 4
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int arr[n];
cin>>arr[0]
int maxNo=arr[0];
int minNo =arr[0];
for(int i=1;i<n;i++){
cin>>arr[i];
maxNo=max(maxNo,arr[i]);
minNo=min(minNo,arr[i]);
}
cout<<maxNo<<" "<<minNo;
return 0;
}
+ 3
Let's assume <n> is 5
1. Read value for 1st element (arr[0])
2. Assign that value for <minNo> and <maxNo>
3. Begin the loop while 1 < 5 rather than while 0 < 5
* No change is needed in loop body
+ 1
yeah it's working thanks 😁