i am not able to pass any testcases of this problem
Stock span(hackers block)
Hello @Ayushi21,
There are multiple issues with your code:
-
The input format is wrong.
You are not taking any input.
You are just passing the same array and value of n for all testcases. -
How are you so sure that there would not be more than 6 elements in the array:
void stockSpan(int arr[6], int n)
Solution:
void stockSpan(int arr[], int n) -
Your output format is also wrong.
0 0 0 1 0 END
You are not required to print END at last.
Solution:
Read the question carefully. -
Please review your logic as it will print 0 for the first element always.
Thus, the other values would get effected.
in span we have to consider elements which are equal and less.
So, for the first element it should be 1 as we are considering current element and all elements before it.
Do you want me to explain the correct logic?
umm yes it would be of great help.
To understand this better, observe the following figure carefully:
Observations:
- The stock of any day is the count of consecutive days before (i+1)th day having values less and equal to the price at ith day, but before the first value on left that is greater than ith day’s price.
- The stock of first day is always one.
Refer to the following function:
Hope, this would help.
Give a like, if you are satisfied.
https://ide.codingblocks.com/s/110150 - hey this is the url of the code but this code still is not working
Hello @Ayushi21,
-
You have not followed the point 3 of my previous reply:
cout<<“END”<<endl;
Why are you printing this? Is it mentioned in the question? Please check. -
How can you access an index of a non- array variable?
int price = arr[i];
price[st.top()]
How is it possible?
It would work properly, once you would correct the above mentioned mistakes.
Refer the code, i have shared.
Hope, this would help.
hey can you explain what do you mean by print S like what do i have to print
https://ide.codingblocks.com/s/116366 - btw thias is my code . please tell me where i have to make corrections.
Hello @Ayushi21,
You have missed the most important part of the problem:
-
TAKING INPUTS
Input Format:
First line contains the number of test cases, T. For each test case, first line contains a single integer N, denoting the number of days. Next line contains N integers, ith of which denotes the stock’s price on ith day. -
1<=N<=10^5
Why are you taking the size of array as 6? -
Why are you printing END
I have corrected your code, check it here:
Hope, this would help.
Give a like, if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.