Arrays max value in array

what is the error i have done

Hey @Thilak
Can you post your code?

what is the mistake in the code

#include
using namespace std;
int main() {
int n;
cin>>n;
int currnum=0;
int maxnum=0;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
if(currnum<0){
maxnum=0;
}
else if(currnum>maxnum){
currnum=maxnum;
}
else{
maxnum=0;
}
currnum++;
maxnum= max(currnum,maxnum);
}
cout<<maxnum<<endl;
return 0;
}

@Thilak
Your approach is wrong, There’s nothing much that you need to do while writing this code:

  1. Just set the maxnum as INT_MIN initially, and then while taking input, just check if the current number is greater than maxnum, then update maxnum, then at the end, you’ll get the right maximum number,
    The simplest code goes like:

#include<bits/stdc++.h>
using namespace std;

int main() {
int n; cin >> n;
int arr[n];
int maxnum = INT_MIN;
for(int i = 0; i < n; i++){
cin >> arr[i];
if(arr[i]>maxnum)maxnum=arr[i];
}
cout << maxnum;
return 0;
}

what is the use of INT_MIN in this program.

INT_MIN is the minimum possible integer, so initially we suppose the maximum as INT_MIN, all integers(negative and positive) will be greater than it, so it will gradually be replaced by maximum integer after completion of all iterations in the loop.