
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of Triangles After N Moves using C++
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.
Approach to find The Solution
There are Two Approaches for this Solution and they are −
Brute Force Approach
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.
Example
#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"; }
Output
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.
Efficient Approach
In this approach, we will make up a formula that will calculate our answer for us.
Example
#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"; }
Output
17
The above code has a time complexity of O(log(N)), where N is the number of moves we performed.
Explanation of the above code
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.
Conclusion
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 Articles
- Count number of 1s in the array after N moves in C
- Find number of unique triangles among given N triangles 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 area of the following triangles."\n
- Minimum number of given moves required to make N divisible by 25 using C++.
- Finding number of open water taps after n chances using JavaScript
- Find the Number of Solutions of n = x + n x using C++
- Find the index of the left pointer after possible moves in the array in C++
- Find the number of consecutive zero at the end after multiplying n numbers in Python
- Isosceles triangles with nearest perimeter using JavaScript\n
- Program to find number of items left after selling n items in python
- Program to find remainder after dividing n number of 1s by m in Python
- Triangles ABC and DEF are similar.If $AC\ =\ 19\ cm$ and $DF\ =\ 8\ cm$, find the ratio of the area of two triangles. "\n
- Triangles ABC and DEF are similar.If $AB\ =\ 1.2\ cm$ and $DE\ =\ 1.4\ cm$, find the ratio of the area of two triangles."\n
