Question

What is the run-time of this recursive code: int maximum(int array[], int index, int len); //define...

What is the run-time of this recursive code:

int maximum(int array[], int index, int len); //define maximum function.
int minimum(int array[], int index, int len);//define minimum function.


int main()
{
//Main method
int array[MAX_SIZE], N, max, min; //Defining all the variable.
int i;
printf("Enter size of the array: ");//Taking input from user as a array size
scanf("%d", &N);
printf("Enter %d elements in array: ", N);//Taking input from user
for(i=0; i<N; i++)
{
scanf("%d", &array[i]);//storing all the element in array.
}

max = maximum(array, 0, N);//calling maximum function which will return max element.
min = minimum(array, 0, N);//calling minimum function which will return min element.

printf("Minimum element in array = %d\n", min);//Displaying Min element.
printf("Maximum element in array = %d\n", max);//Displaying Max element.

return 0;
}


int maximum(int array[], int index, int len)
{ //maximum function will return maximum element from array.
int max;
  
if(index >= len-2)
{
if(array[index] > array[index + 1])
//If element array[index] is greater than array[index + 1] then return array[index]
return array[index];
else
return array[index + 1];
//Otherwise return array[index + 1]
}

max = maximum(array, index + 1, len); //Recursively calling maximum function.

if(array[index] > max)
//If array[index] is greater than max then return array[index]
return array[index];
else
return max;
//otherwise return max
}

int minimum(int array[], int index, int len)
{ //minimum function which will return min value from the array
int min;

if(index >= len-2)
{
if(array[index] < array[index + 1])
//If element array[index] is smaller than array[index + 1] then return array[index]
return array[index];
else
return array[index + 1];
//Otherwise return array[index + 1]
}

min = minimum(array, index + 1, len); //Recursively calling minimum function.

if(array[index] < min)
//if array[index] is smaller than min then return array[index]
return array[index];
else
return min;
//return min
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The time complexity of recursive maximum() method is O(n) where n is the number of elements in the array.

The time complexity of recursive minimum() method is O(n) where n is the number of elements in the array.

Add a comment
Know the answer?
Add Answer to:
What is the run-time of this recursive code: int maximum(int array[], int index, int len); //define...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Consider the following recursive method for finding the maximum element in an int array: public int...

    Consider the following recursive method for finding the maximum element in an int array: public int static max(int[] a, int lo, int hi) { if (lo > hi) return a[lo]; int mid = (lo + hi) / 2; int loMax = max(a, lo, mid); int hiMax = max(a, mid+1, hi); if (loMax > hiMax) return loMax; else return hiMax; } Write down the recurrence relation for counting the number of times the comparison if (loMax > hiMax) is performed. Use...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • Create a C project named login_l03t1. Create an array of int from parameters passed on the...

    Create a C project named login_l03t1. Create an array of int from parameters passed on the command line. Use argc to define the size of the array. Print the contents of the array in the format and Generate and print the total of the values in the array twice: once by using an index i, and a second time by incrementing a pointer to the array. I need help with this please! what I have done is below: int n...

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • Please help with this question (the two functions). I've attempted it but it's not working. We...

    Please help with this question (the two functions). I've attempted it but it's not working. We have to complete the 2 functions int largest(int * x); and void display(int *arr); (one prints the values in array and the other gets max value in array). But we can only use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the functions. C PROGRAMMING: ----------------- /* Passing an array to a function....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT