Given an integer N, we need to find three integers(X, Y, Z) which can form a triangle with the following conditions:
- Lengths of sides are integers not exceeding N.
- XOR of three sides is 0, i.e., X ^ Y ^ Z = 0
- Area of triangle is greater than 0.
Find all the possible triples which satisfy the above conditions.
Input: 6 Output: 6 5 3 Input: 10 Output: 10 9 3 6 5 3
Naive Approach: Select the first side by iterating from N to 1 and then select the second side by iterating from the first side to 1 and then select third side by iterating from second side to 1. Now check if the three sides can make a triangle(sum of the two smaller sides must be greater than the longest side) and the xor-sum of lengths is equal to 0.
Time Compelxity = O(n^3).
Efficient Approach: In this method we select the first two sides as we did in the first approach, the third side will be equal to the xor of the first two sides(this will make the xor-sum of lengths is equal to 0)and this side must be smaller than the first two sides. Now check if these sides can make a triangle.
Time Complexity = O(n^2)
10 9 3 6 5 3
- Area of the circumcircle of any triangles with sides given
- Find other two sides of a right angle triangle
- Find other two sides and angles of a right angle triangle
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Check whether triangle is valid or not if sides are given
- Check if given four integers (or sides) make rectangle
- Number of triangles after N moves
- Count the number of possible triangles
- Check whether right angled triangle is valid or not for large sides
- Number of triangles that can be formed with given N points
- Program to check congruency of two triangles
- Number of Triangles in an Undirected Graph
- Program to check similarity of given two triangles
- Counting Triangles in a Rectangular space using BIT
- Count number of unique Triangles using STL | Set 1 (Using set)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.