Determine the time complexity of the following code. Please provide an explanation.
Posted: Wed Apr 27, 2022 3:48 pm
Determine the time complexity of the following code.
Please provide an explanation.
main.cpp Console Shell 1 2 3 #include <iostream> #include<algorithm> using namespace std; > make-s 3./main The maximum amount of money is: 17 The number of recursive calls is: 25 4 5 int count1 = 0; 6 7 v int maxAmount(int A[], int n){ 8 count1++; 9 if(n < 0) // base case 1 10 return 0; 11 if(n == 0) // base case 2 12 return A[0]; 13 14 return max(A[n] + maxAmount (A, n - 2), maxAmount(A, n - 1)); 15 16 // recursive call 17 } 18 19 int main() { 20 int n = 6; 21 int a[6] = {5, 1, 2, 10, 6, 2}; 22 cout<< "The maximum amount of money is: " << maxAmounta, n- 1); // call function maxAmount 23 cout << "\nThe number of recursive calls is: " << countl << endl; 24 return 0; 25 26 ]
Please provide an explanation.
main.cpp Console Shell 1 2 3 #include <iostream> #include<algorithm> using namespace std; > make-s 3./main The maximum amount of money is: 17 The number of recursive calls is: 25 4 5 int count1 = 0; 6 7 v int maxAmount(int A[], int n){ 8 count1++; 9 if(n < 0) // base case 1 10 return 0; 11 if(n == 0) // base case 2 12 return A[0]; 13 14 return max(A[n] + maxAmount (A, n - 2), maxAmount(A, n - 1)); 15 16 // recursive call 17 } 18 19 int main() { 20 int n = 6; 21 int a[6] = {5, 1, 2, 10, 6, 2}; 22 cout<< "The maximum amount of money is: " << maxAmounta, n- 1); // call function maxAmount 23 cout << "\nThe number of recursive calls is: " << countl << endl; 24 return 0; 25 26 ]