Array reversal in Recurrsion

Array reversal in Recurrsion

This problem can be performed in o(n) with normal iterative approach.
Here is vanilla flavor of its implementation in recursion

#include 

/**
 * Recursion version of array swap
 *
 * Kaleb Woldearegay
 */

void reverse(int*arr, int i, int size);
void print(int*arr, int size);

void main()
{
    int nums[] = {4,5,2,13,0}, i=0, size=5;
    i = 0;
    print(nums, 5);
    reverse(nums, i, size-1);
    print(nums, 5);
}
void reverse(int*stk, int i, int size)
{
    if (i>=size)
    {
        return;
    }
    else
    {
        int tmp = stk[i];
        reverse(stk, i+1, size-1);
        stk[i] = stk[size];
        stk[size] = tmp;
    }
}

void print(int*nums, int size)
{
    int i=0;
    while (i<size)
    {
        printf("%in", nums[i]);
        i++;
    }
}

See how you would solve these known algorithm problems

Find the pairs that makes K Complementary in the given array java solution

binary tree problems with solution

Find K Complementary numbers from array Java implementation

Check if there are three numbers a, b, c giving a total T from array A

Find the first occurence of number in the sorted array

Finding missing numbers from billion sequential number list file

testing k complementary pairs algorithm with junit

Find longest palindrom from sequence of characters

String Ordered Permutation Algorithm Problem

Implementing tokenizer and adding tokens to the linked list

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*