Recursive function that returns the number of numbers greater then the first index of an array

caiomartins1

Recursive function that returns the number of numbers greater then the first index of an array

I've got a function that solves the problem but I'm not sure if it is the correct way to write the recursive function

int greaterThanFistIndex(int *v,int n){
    int a;
    if(n == 1){
        return 0;
    }else{
        a = greaterThanFistIndex(v,n-1);
        if(v[0] < v[n-1]){
            a++;
        }
    }
    return a;
}

the output of [3,5,1,6] should be 2

chux - Reinstate Monica

I'm not sure if it is the correct way to write the recursive function

The code looks OK yet it breaks the spirit of using recursion.1

Such code could readily be done with a simple loop instead of n recursive calls.

int greaterThanFistIndex_iterate(const int *v,int n){
  count = 0;
  for (int i = 1; i<n; i++) {
    if (v[i] > v[0]) count++;
  }
  return count;
}

Could could halve the problem at each step. At least the would have only log2(n) recursion depth rather than n as in OP's code.

static int greaterThanFistIndex_r_helper(const int *v,int n, int first){
  if (n <= 0) {
    if (n == 0) return 0;
    return *v > first;
  }
  int first_half = n/2; 
  int second_half = n - first_half; 
  return greaterThanFistIndex_r_helper(v, first_half, first) +  
      greaterThanFistIndex_r_helper(v + first_half, second_half, first);
}

int greaterThanFistIndex_r(const int *v,int n){
  if (n <= 0) return 0;
  return greaterThanFistIndex_r_helper(v+1, n, *v);
}

Some compilers will detect OP's construct and perform tail end recursion optimization yet I see a simple loop a better approach.


Use recursion when it makes sense. Use a loop when possible.

Note: better to use size_t than int for array size and indexing. (not shown here)


1 Code fails for n <= 0.
A better function signature would be size_t greaterThanFistIndex(const int *v, size_t n)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

A function that returns the number of even numbers of an array

LISP function which, given a number and a list, returns the first even number greater than n

Function that returns the index of the first parameter in the array passed as the second parameter

How to create a function that returns an index of the first repeated value in an array?

First index of an array returns an IndexOutOfRangeException

Recursive function - count number of even numbers in tuple

Recursive function that returns the number of possible combinations

Recursive function on sorted array returns undefined

Recursive function that returns array with random X values

JavaScript: Function that adds index number to my array elements keeps applying index numbers

Recursive function only works for first level of array

recursive function that finds the first error in the array

Counting numbers greater than a certain number in an array in MongoDb

Get the index of the first element in an array with value greater than x

Recursive function that returns sum of all odd numbers within a range

My C function to reverse an array fills the first and last index with random numbers after consecutive runs. Why is this?

(Javascript) Recursive function that works on first call but not on second When Oscillating numbers

JS Function that checks a dynamic number of index positions and returns array element if it satisfies the condition

My find-biggest-number Recursive function returns a discremented value

Recursive factorial function program returns 0 for large number input in C

Is there a function for finding the first member in an array which greater then a threshold

Display first index number of array for particular value

String match function returns an array, but if converted to number, returns the matched number

function that returns first object of a given array

Recursive function in a character array for finding a consonant in the first 3 elements in the array

Returns in a recursive function

Recursive function that returns the remainder

Is there a NumPy function to return the first index of something in an array?

A function that collects an array of numbers as a parameter and returns an array of percentages?