John Patrick Amata


my blog for algorithms
Dark/Light Mode:  
Share: 

Day 4

today

Continued the pattern problems

Pattern Problem 3

Link: https://practice.geeksforgeeks.org/problems/triangle-number/1

If n = 5, print:

1
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution {
  public:
    void printTriangle(int n) {
        // code here
        for (int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                cout << j << " ";
            }
            cout << "\n";
        }
    }
};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: kind of like pattern problem 2 in day 3, but this time we’re incrementing the characters, so we initialise the variables i and j to 1, and use <= operator

Pattern Problem 4

Link: https://practice.geeksforgeeks.org/problems/triangle-number-1661428795/1

If n = 5, print:

1
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution {
  public:
    void printTriangle(int n) {
        // code here
        int count = 1;
        for(int i = 0; i < n; i++){
            for(int j = 1; j < count + 1; j++){
                cout << count << " ";
            }
            cout << "\n";
            count++;
        }

    }
};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: alternatively, i can just copy the solution in pattern 3 but instead print the row (i) such as:

cout « i « ” “;

Pattern Problem 5

Link: https://practice.geeksforgeeks.org/problems/triangle-pattern/1

If n = 5, print:

* * * * *
* * * * 
* * * 
* *  
* 
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution{
public:

	void printTriangle(int n) {
	    // code here
	    for(int i = n; i > 0; i--){
	        for(int j = 0; j < i; j++ ){
	            cout << "*" << " ";
	        }
	        cout << "\n";
	    }
	}
};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: alternatively we can do it like this

for(int i = 0; i < n; i++){
	        //inner loop
	        for(int j = n; j > i ;j--){
	            cout << "*" << " ";
	        }
	        cout << "\n";
	    }

Pattern Problem 6

Link: https://practice.geeksforgeeks.org/problems/triangle-number-1661489840/1

If n = 5, print:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution{
public:

	void printTriangle(int n) {
	    // code here
	    for(int i = n; i > 0; i--){
	        for(int j = 1; j <= i ;j++){
	            cout << j << " ";
	        }
	        cout << "\n";
	    }
	}
};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: solution is like the previous problem, only the inner loop has seen a changed

Pattern Problem 7

Link: https://practice.geeksforgeeks.org/problems/triangle-pattern-1661492263/1

If n = 5, print:

    *
   ***  
  *****
 *******
*********
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution {
  public:
    void printTriangle(int n) {
        // code here
        for(int i = 0; i < n; i++){
            for(int j = 0; j<n-i-1;j++){
                cout << " ";
            }
            for(int j = 0; j<2*i+1;j++){
                cout << "*";
            }
            for(int j = 0; j<n-1+1;j++){
                cout << " ";
            }
            cout << "\n";
        }

    }
};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: basically a pyramid of spaces on its edges and stars on the middle; take note of the pattern for the sample input and output. For example for a pyramid with an input of 5, it provides us the following pattern:

i            [space, star, space]
0       *       [4,1,4]
1      ***      [3,3,3]
2     *****     [2,5,2]
3    *******    [1,7,1]
4   *********   [0,9,0]

We can now see a pattern emerging:

Because of this condition of having to print space, star, and space, we need 3 inner loops.

//outer loop for rows
for(int i = 0; i < n; i++) {
    //inner loop for columns
    space loop{}
    star loop{}
    space loop{}
}

Pattern Problem 8

Link: https://practice.geeksforgeeks.org/problems/triangle-pattern-1661493231/1

If n = 5, print:

*********
 *******
  *****
   ***
    *
//{ Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends
class Solution{
public:

	void printTriangle(int n) {
	    // code here
	    for(int i = 0; i < n; i++){
	        for(int j = 0; j<i;j++){
	            cout << " ";
	        }
	        for(int j = 0; j< (2*n - (2*i+1));j++){
	            cout << "*";
	        }
	        for(int j = 0; j<i;j++){
	            cout << " ";
	        }
	        cout << "\n";

	    }

	}

};

//{ Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        Solution ob;
        ob.printTriangle(n);
    }
    return 0;
}
// } Driver Code Ends

Notes: I tried to do it with the outer loop set to n, but i realised that finding the pattern by substraction was just too unintuitive compared to setting the outer loop i to 0.

Anyway, the pattern here is basically: