Whats The Error In This Question Answer , 2 Test Case Failing , Please Help!

   #include<iostream>
#include<algorithm>
using namespace std;

int main(){

    long long int n;
    cin>>n;
    long long int ans=0;

   long long  int *arr=new  long long  int[10000000]; // 4,4,1,3

    for(long long int i=0; i<n;i++){
        cin>>arr[i];
    }
    sort(arr,arr+n);
    
    //for(int i=0; i<n; i++){
        for(long long int j=0; j<n; j++){

            if(arr[j]<arr[j+1]){
               ans=arr[j+1];
               // cout<<ans;
            }
            // if(arr[j]==arr[j+1]){
            //     ans+=2;
            //     cout<<ans;
            // }
        }
        // cout<<ans;
       long long int max=0;
        for(long long int i=0; i<n; i++){
           if(arr[i]==ans){
               max=max+1;
           }
        }
        //cout<<endl;
        cout<<max;
    //}
}

There are two accesses to uninitialised values here, when j == n:

if(arr[j]<arr[j+1]){
               ans=arr[j+1];

(you’ve only initialised the first n values of arr), so the output of your program is undefined.

3 Likes

thanks for the help

1 Like