< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Q1: Head-Recursion
  • Q2: Tail-Recursion
  • Q3: Fibonacci Number
  • Q4: Climbing Stairs --> TLE Error
  • Q5: SayDigits
  • Q6: isSorted-Array
  • Q7: Check if Array Is Sorted and Rotated > Pending <
  • Working Approach
  • Q8: Linear Search
  • Day-04 Recursion
  • Q9: Reverse String
  • Q10: Find Exponent in logarithmic time

Was this helpful?

Recursion

Q1: Head-Recursion

void headRecursion(int count) {
    
    // Base-Condition
    if (count == 0)
        return;

    // Recursive Relation
    headRecursion(count - 1);
    
    // Processing
    cout << count << " ";
}

// o/p = 1 2 3 4 5

Q2: Tail-Recursion


void tailRecursion(int count) {
    // Base-Condition
    if (count == 0)
        return;
   
    // Processing
    cout << count << " ";
    
    // Recursive Relation
    tailRecursion(count - 1); 
}

// o/p = 5 4 3 2 1
int fib(int n) {
    if(n < 2)
        return n;
    return fib(n-1) + fib(n-2);
}
int climbStairs(int n) {
    if(n < 0) 
        return 0;
    if(n == 0) 
        return 1;
            
    return climbStairs(n-1) + climbStairs(n-2);
}

Q5: SayDigits

#include <iostream>
using namespace std;

void sayDigits(int x, string* arr) {
    if (x == 0) return ;

    int digit = x%10;
    x /= 10;
    sayDigits(x, arr);

    cout << arr[digit] << " ";
}

int main() {
    string arr[] =  {"Zero", "One", "Two", "Three", "Four", 
                        "Five", "Six", "Seven", "Eight", "Nine"};
    sayDigits(4129192, arr);
}

Q6: isSorted-Array

#include <iostream>
using namespace std;

bool isSorted(int *arr, int size, int i = 0) {
    if(i == size-1) return true;

    if(arr[i] > arr[i+1]) return false;

    isSorted(arr, size, ++i);
}

int main() {
    int arr[4] = {5, 7, 7, 10};
    cout << boolalpha << isSorted(arr, 4);
}


bool isSorted(int arr[], int size);
bool isRotated(int arr[], int size);

bool check(int nums[], int size) {

    return isSorted(nums, size);
}

bool isSorted(int arr[], int size) {
    if(size == 0 || size == 1) 
        return true;

    if(arr[0] > arr[1])
        return isRotated(arr + 1, size);
    
    return isSorted(arr + 1, size - 1);
}

bool isRotated(int arr[], int size) {
    if(size == 0 || size == 1) 
        return true;

    if(arr[0] > arr[1])
        return false;
    
    return isRotated(arr + 1, size - 1);
}

int main() {
    int arr[5] = {3,4,5,1,2};

    if(check(arr, 5))
        cout << "Given Array is Sorted or Rotated";
    else
        cout << "Given Array isn't Sorted or Rotated";
}

Q8: Linear Search

void print(int arr[], int size) {
    cout << "Size: " << size << endl;

    for (int i = 0; i < size; i++){
        cout << arr[i] << " ";
    } cout << endl;
}

bool linearSearch(int arr[], int size, int k) {
    print(arr, size);
    
    if(size == 0)
        return false;
    if(arr[0] == k)
        return true;
    
    return linearSearch(arr + 1, size - 1, k);
}

Day-04 Recursion

#include <iostream>
#include <cstring>
using namespace std;

void reverseString(char* str, int e, int s = 0) {
    if(s > e) return ;
    
    swap(str[s], str[e]);

    reverseString(str, --e, ++s);
}

int main() {
    char str[] = "nafees";
    reverseString(str, strlen(str)-1);
    for (auto c : str) {
        cout << c << " ";
    }
}

Q10: Find Exponent in logarithmic time

TC: log n (log base 2 of n)
#include "iostream"
using namespace std;

int findExpo(int a, int pow, int ans = 0) {
     
     if(pow == 0) return 1;
     
     else if(pow == 1) return a;
     
     
     else if(pow % 2 == 0) {
     // a^(n/2).a^(n/2)
          ans = findExpo(a, pow/2, ans);
          return ans * ans;
     }
     else {
     // a^[(n-1)/2].a^[(n-1)/2].a
          ans = findExpo(a, (pow-1)/2, ans);
          return ans*ans*a;
     }
}

int main() {
     int a = 2,pow = 7;

     cout <<  findExpo(a, pow);
}
PreviousBasic Math - CodechefNextStrings Functions

Last updated 1 year ago

Was this helpful?

Q3:

Q4: --> TLE Error

Q7: > Pending <

Q9:

In this program, the recursiveFunc() function is called recursively until the base case is reached. The base case is when s > e. Therefore, the number of recursive calls made by the function is equal to half the length of the string. .

The space complexity of your program depends on the maximum depth of recursion. In your program, the maximum depth of recursion is equal to half the length of the string. .

Fibonacci Number
Climbing Stairs
Check if Array Is Sorted and Rotated
Reverse String
Hence, the time complexity of your program is O(n/2) or O(n)
1
Therefore, the space complexity of your program is O(n/2) or O(n)
2
☠️
Working Approach
Page cover image