While computing the sum of Leaders in an array using Visual Studio Code, I encountered an error that said: "The terminal process "C:\WINDOWS\System32\cmd.exe" terminated with exit code: 1."
My C++ implementation goes as follows:
#include<bits/stdc++.h>
using namespace std;
int leader_func(int arr[], int n)
{
int sum=0;
int max=arr[n-1];
for (int i = n-2; i>=0; i--)
{
if (max<arr[i])
{
sum+=arr[i];
}
}
return sum;
}
int main()
{
int arr[4]={33,7,21,14};
int n=4;
cout<<leader_func(arr, n);
return 0;
}
The expected output is: 54
(i.e. 33 + 21).
The actual result I received: The terminal process "C:\WINDOWS\System32\cmd.exe" terminated with exit code: 1.
What could be the possible reasons? And how can this problem be solved?