- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

n the article, first, we have to draw a colored triangle. We need to take an uncolored triangle and divide the triangle into four small equilaterals. Triangles with the same area and keep doing it till the nth step and find the number of equilateral triangles present in the figure.

There are Two Approaches for this Solution and they are −

We can observe that the number of triangles keeps increasing by some number (increasing by 3*previous_number + 2) after every step. So we can run a loop till n and calculate the number of triangles.

#include <iostream> using namespace std; int main() { int n = 2; // number of operations we made int count = 1; // at first we have only one triangle for(int i = 0; i < n; i++) { // looping till n count = 3 * count + 2; // as the triangle count is increasing by 3*prev + 2 } cout <<count << "\n"; }

17

The time complexity of the program above is O(N), where N is the number of operations performed. Now we can further improve its time complexity which will be very helpful when we deal with higher constraints.

In this approach, we will make up a formula that will calculate our answer for us.

#include <bits/stdc++.h> using namespace std; int main() { int n = 2; // number of operations we made int count; count = 2 * (pow(3, n)) - 1; // the total number of triangles after nth move cout << count << "\n"; }

17

The above code has a time complexity of O(log(N)), where N is the number of moves we performed.

In the given program, we are simply making up a formula to solve our given procedure and putting the values required in the formula, and printing the result.

This article finds the number of triangles after N moves by applying some observations and some maths. We also learned the C++ program for this problem and the complete approach (Normal and efficient ) by which we solved this problem.

We can write the same program in other languages such as C, java, python, and other languages. we hope you find this article helpful.

- Related Questions & Answers
- Find number of unique triangles among given N triangles in C++
- Count number of 1s in the array after N moves in C
- Find the probability of reaching all points after N moves from point N in C++
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++\n
- Find the Number of Solutions of n = x + n x using C++
- Count the number of possible triangles in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- Isosceles triangles with nearest perimeter using JavaScript\n
- Minimum number of moves to make all elements equal using C++.
- Find the number of consecutive zero at the end after multiplying n numbers in Python
- Finding number of open water taps after n chances using JavaScript
- Find the Number of Trailing Zeroes in Base 16 Representation of N! using C++
- Find the Number of Trailing Zeroes in base B Representation of N! using C++
- Find the Number of Ways to Traverse an N-ary Tree using C++

Advertisements