Page 1 of 1

What is the time complexity of the following code?

Posted: Wed Jul 13, 2022 7:41 pm
by answerhappygod
#include <bits/stdc++.h>
using namespace std;
void convert(int arr[], int n)
{
int temp[n];
memcpy(temp, arr, n*sizeof(int));
sort(temp, temp + n);
unordered_map<int, int> map;
int sort_index = 0;
for (int i = 0; i < n; i++)
map[temp] = sort_index++;
for (int i = 0; i < n; i++)
arr = map[arr];
}
void printArr(int arr[], int n)
{
for (int i=0; i<n; i++)
cout << arr << " ";
}
int main()
{
int arr[] = {10, 20, 15, 12, 11, 50};
int n = sizeof(arr)/sizeof(arr[0]);
convert(arr , n);
printArr(arr, n);
return 0;
}
a) O(n)
b) O(1)
c) O(n log n)
d) O(n2)