-2
public void computeDifference(){
int i=0,j=0;maximumDifference=0;
for (i=0;i < elements.length;i++){
    for (j=i+1;j < elements.length;j++){
        maximumDifference = (Math.abs(elements[j]-elements[i]) > maximumDifference)? Math.abs(elements[j]-elements[i]):maximumDifference;

What will be the time complexity of this code ?

TheSYNcoder
  • 123
  • 1
  • 9

1 Answers1

0

It would be O(n^2). Because the inner loop runs n-1 times to 1 time because of the outer loop(Outer loop runs n times). Therefore,

(n-1) + (n-2) + (n-3) + .... + 1 = n*(n+1)/2

So the inner statement runs approx. n^2/2 + n/2 times. So the upper bound is O(n^2). Where n is number of elements.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72