Q:

Recursively finding the longest streak and initial position of 1's in a 1d array

If given an array. How do you keep track of the position during recursion. Also how would you keep track of the longest streak.
Ex: Given an array of size 16 {1,0,1,1,1,0,0,0,1,1,0,0,1,1,0,1}
The answer is a streak of length 3 at the position of index 2.
This is the iterative solution I have made. It works perfectly.
int* iterative(int arr[],int length)
{
    int temppos = 0;
    int pos = 0;
    int templen = 0;
    int len = 0;
    for(int i=0;i<length;i++)
    {
        if(arr[i]==1)
        {
            templen++;
            if(templen>len)
            {
                len=templen;
                pos=temppos;
            }
        }
        else
        {
            templen=0;
            temppos=i+1;;
        }
    }
    static int x[2]= {pos,len};
    return x;
}

This is my pseudo-code attempt at the recursive solution.
int[] recursive(int[] array, int position, int streak)
{
    if(array length is equal to 1 && arr[0]==1)
     return result(position,streak)
    else
     leftsubarray(array/2)
     rightsubarray(array/2+1)
}

Given the iterative solution. What should my base case and recursive step be.

A:

This is the iterative solution in C++ of your problem.A structure ans is used to store the index i and streak length streak.
#include <iostream>
using namespace std;

typedef struct {
    int i;
    int streak;
} ans;

ans *a= new ans;

void find_streak(int arr[],int i,int s)
{
        if(arr[i]==1)
        {
            s++;
        }
        if(arr[i]==0 || i==15) //checking for end of streak or array
        {
            if(s>a->streak) //checking if the current streak is greater than max
            {
                a->streak=s;
                a->i=i-s;
            }
            s=0; // initializing s to 0 for new streak
        }
        if(i+1<16)
            find_streak(arr,i+1,s);
        else
            return;
}

int main()
{
    int arr[]={1,0,1,1,1,0,0,0,1,1,0,0,1,1,1,1};
    a->i=0;a->streak=0;
    find_streak(arr,0,0);
    cout<<"Index: "<<a->i<<" streak: "<<a->streak<<endl;
}

