We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sorry for late reply, I think it is not a good idea to create or declare a very big array, if you are not sure about the size of the array, it is better using vector instead of the array. And If you want to find max or min values of vector or array,at first, you should initialize min or max value with array's element such as a[0].
longmax=0;longy=0;longsum=0;longarr[5];// we know the size of array and it is 5for(inti=0;i<5;i++){cin>>arr[i];sum=sum+arr[i];}max=arr[0];// initialized with first element and start your loop with 1for(intm=1;m<5;m++){if(max<arr[m]){max=arr[m];}}y=arr[0];for(intm=1;m<5;m++){if(y>arr[m]){y=arr[m];}}longmin_sum=(sum-max);longmax_sum=(sum-y);cout<<min_sum<<" "<<max_sum;return0;}
your logic is good but you might do it with one loop.
longsum=0;longarr[5];// we know the size of array and it is 5cin>>arr[0];longmax=arr[0];longy=arr[0];for(inti=1;i<5;i++){cin>>arr[i];sum=sum+arr[i];if(max<arr[m]){max=arr[m];}if(y>arr[m]){y=arr[m];}}longmin_sum=(sum-max);longmax_sum=(sum-y);cout<<min_sum<<" "<<max_sum;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
Sorry for late reply, I think it is not a good idea to create or declare a very big array, if you are not sure about the size of the array, it is better using vector instead of the array. And If you want to find max or min values of vector or array,at first, you should initialize min or max value with array's element such as a[0].
your logic is good but you might do it with one loop.