Pages

Saturday, March 29, 2025

patterns - lecture 1 (one mistake to avoid!!)

 #include <iostream>

#include <cmath>
using namespace std;

int main() {
    int n = 4;
    int i;
    // outer loop
    for(i = 1; i <= n; i++){ // mistake int i was present, so 5 was not printing
due to this reason
        int val = i;
        // inner loop
        for(int j = 1; j <= n; j++){
            //work
            cout << val << " ";
        }
        cout << endl;
    }
    cout << i << endl;

    return 0;
}


OUTPUT:-
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5

No comments:

Post a Comment

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...