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 first occurence of number in the sorted array

Find K Complementary numbers from array Java implementation

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

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

Kadane’s algorithm in C – Dynamic Programming

Changing decimal number to its binary equivalent

Implementing tokenizer and adding tokens to the linked list

Finding missing numbers from billion sequential number list file

Find longest palindrom from sequence of characters

Java solution for checking anagram strings – tell if phrases are anagrams

Leave a Reply

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

*
*