1,062 views
1 votes
1 votes
$\begin{align*} & a[n] = \{x_1,x_2,x_3,x_4,....,x_n\} \text{ is an array of integers where } n,x_i > 0. \\ & A = \left [ \text{min}\left ( x_i,x_j \right ) \right ] \cdot \left ( j-i \right ) \text{ where } j > i \text{ and } i,j \leq n \\ & \text{What is the best time complexity to find out the value of } A_{\bf max} \; ? \end{align*}$

1 Answer

Best answer
3 votes
3 votes
// array : a[n]
int left = 1;
int right = n;
int max = -1;
int m = 0;
while(left <= right) {
	m = min(a[left],a[right])*( right - left );
	if(max < m) max = m;
	if(a[left] < a[right]) { 
		left++;
	} else if(a[left] > a[right]) { 
		right--;
	} else {
		left++;
		right--;
	}
}
return max;
selected by

Related questions

4 votes
4 votes
3 answers
1
0 votes
0 votes
1 answer
2
Naveen Pandey asked Jan 8, 2017
487 views
T(n)=2T(log n)+c c is a constant. Base condition is if(n<2) return 1What will be the tightest bound on time complexity?
0 votes
0 votes
1 answer
3
0 votes
0 votes
3 answers
4
shikharV asked Nov 17, 2015
2,029 views
Given answer: BPlease explain