Pages

Tuesday, May 27, 2025

Tiling problem using Recursion in C++

 #include <iostream>

#include <vector>
using namespace std;

int tilingProblem(int n) { // 2xn
    if(n==0 || n==1) {
        return 1;
    }

    //vertical
    int ans1 = tilingProblem(n-1); // 2x(n-1)

    // horizontal
    int ans2 = tilingProblem(n-2); // 2x(n-2)

    return ans1+ans2;
}

int main() {

    int n = 3;

    cout << tilingProblem(4) << endl;

    return 0;
}


OUTPUT:
5

1 comment:

  1. Smaller code than original one...


    #include

    #include
    using namespace std;

    int tilingProblem(int n) { // 2xn
    if(n==0 || n==1) {
    return 1;
    }
    return tilingProblem(n-1)+tilingProblem(n-2);
    }

    int main() {

    int n = 3;

    cout << tilingProblem(5) << endl; // 2x5

    return 0;
    }

    ReplyDelete

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...