Skip to content
Related Articles

Related Articles

Algorithms Quiz | Sudo Placement [1.5] | Question 9

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2018

What is the time complexity of below code?




// CPP program to find the maximum value
// of i*arr[i]
#include<bits/stdc++.h>
using namespace std;
   
int maxSum(int arr[], int n)
  // Sort the array
  sort(arr, arr + n);
   
  // Finding the sum of arr[i]*i
  int sum = 0;
  for (int i = 0; i < n; i++)
    sum += (arr[i]*i);
   
  return sum;
}
   
// Driven Program
int main()
{
  int arr[] = { 3, 5, 6, 1 };
  int n = sizeof(arr)/sizeof(arr[0]);
   
  cout << maxSum(arr, n) << endl;
  return 0;
}


(A) O(N)
(B) O(logN)
(C) O(NlogN)
(D) O(N*N)


Answer: (C)

Explanation:

Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!