Problem:- HostelRoom(starter26), What's wrong with this two solution ?

Descritpion:-
I create a vector nums and try to store their sum upto their index and then print out maximum .
All public test cases are passed and also one hidden test case but I got runtime error(sigabrt)

My solution link:-

  1. solution link

2.Solution Link

U didn’t consider the number of people at the start (i.e. the 0th minute) @mohitjais312

There are basically two errors in your solution.

  1. ( Runtime Error in Solution 2 ) You are not declaring the vector properly. Instead of using this, vector<int>nums={(x+arr[0])}. This only declares a vector of size 1. Declare it something like this.
    vector<int>nums(n); nums[0] = x+arr[0];
  2. ( Logical Error in Solution 1 and 2 ) Consider this example.
    x = 100 and arr[0] = -2.
    Your code will give output as 98. But the real output is 100.
    To fix this, int maxi= max(x, nums[0]);

Working Solution.

Thank you!

1 Like

we can use Kadanes algorithm