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

Check if two strings are anagrams or not

Flatten nested javascript array

binary tree problems with solution

Implement Queue Using two Stacks – JavaScript algorithm

Changing decimal number to its binary equivalent

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

Implementing tokenizer and adding tokens to the linked list

Find longest palindrom from sequence of characters

Find the first occurence of number in the sorted array

find longest word in the sentence

Leave a Reply

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

*
*