Stock span problem

This is code from geeks for geeks . I have a doubt
when i=4 i.e (price[4]=60) then the value of S[4] should be 2 instead of 1.
Please help .

#include <bits/stdc++.h>
using namespace std;

void calculateSpan(int price[], int n, int S[])
{
S[0] = 1;

for (int i = 1; i < n; i++) 
{ 
	S[i] = 1; 
	for (int j = i - 1; (j >= 0) && 
			(price[i] >= price[j]); j--) 
		S[i]++; 
} 

}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

int main()
{
int price[] = {100, 80, 60, 70, 60, 75, 85};
int n = sizeof(price) / sizeof(price[0]);
int S[n];
calculateSpan(price, n, S);
printArray(S, n);
return 0;
}

@divesh2000 it will be one only as there is no less than equal to no in the left.
hope its clear if yes dont forget to hit like and mark resolved :smiley:

when i=4

for (int i = 1; i < n; i++)
{
S[i] = 1; //S[4]=1
for (int j = i - 1; (j >= 0) &&
(price[i] >= price[j]); j–) //price[4]>=price[2] (condition satisfied)
S[i]++; //now, S[4]=+1;
} // so,S[4] =2
}
i am talking about this.

@divesh2000 but price[3]>price[4] so will not go too price[2]

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.