Related Articles
Find all possible triangles with XOR of sides zero
• Last Updated : 28 Apr, 2021

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

```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)

## C++

 `// C++ implementation to find all possible``// triangles with XOR of sides zero``#include ``using` `namespace` `std;` `// function to find all triples which``// satisfy the necessary condition``void` `find_all_possible_sides(``int` `n) {` `  ``// selects first side``  ``for` `(``int` `x = n; x > 0; x--) {` `    ``// select second side``    ``for` `(``int` `y = x - 1; y >= 0; y--) {` `      ``// third side is equal to xor of``      ``// first and second side``      ``int` `z = x ^ y;``      ``if` `(z < x && z < y) {` `        ``// find longest side``        ``int` `max_side = max(x, max(y, z));` `        ``// check if it can make a triangle``        ``if` `(x + y + z - max_side > max_side) {``           ``cout << x << ``" "` `<< y << ``" "``                ``<< z << endl;``        ``}``      ``}``    ``}``}` `}` `// Driver Program``int` `main() {``  ``int` `n = 10;``  ``find_all_possible_sides(n);``  ``return` `0;``}`

## Java

 `// Java implementation to find all possible``// triangles with XOR of sides zero``import` `java.lang.*;` `class` `GFG {``    ` `// function to find all triples which``// satisfy the necessary condition``static` `void` `find_all_possible_sides(``int` `n) {``    ` `    ``// selects first side``    ``for` `(``int` `x = n; x > ``0``; x--) {` `    ``// select second side``    ``for` `(``int` `y = x - ``1``; y >= ``0``; y--) {` `        ``// third side is equal to xor of``        ``// first and second side``        ``int` `z = x ^ y;``        ``if` `(z < x && z < y) {` `        ``// find longest side``        ``int` `max_side = Math.max(x, Math.max(y, z));` `        ``// check if it can make a triangle``        ``if` `(x + y + z - max_side > max_side) {``            ``System.out.println(x + ``" "` `+ y + ``" "` `+ z);``        ``}``        ``}``    ``}``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args) {``    ` `    ``int` `n = ``10``;``    ``find_all_possible_sides(n);``}``}` `// This code is contributed by Anant Agarwal.`

## Python3

 `# function to find``# all triples which``# satisfy the necessary condition``def` `find_all_possible_sides(n):``    ` `    ``# selects first side``    ``for` `x ``in` `range``(n,``0``,``-``1``):` `        ``# select second side``        ``for` `y ``in` `range``(x ``-` `1``,``-``1``,``-``1``):``    ` `            ``# third side is equal to xor of``            ``# first and second side``            ``z ``=` `x ^ y``            ``if` `(z < x ``and` `z < y):``           ` `                ``# find longest side``                ``max_side ``=``max``(x,``max``(y, z))` `                ``# check if it can make a triangle``                ``if` `(x ``+` `y ``+` `z ``-` `max_side > max_side):``        ` `                    ``print``(x , ``" "` `, y , ``" "``,``                                ``z)``                                ` `# driver code                               ` `n ``=` `10``find_all_possible_sides(n)` `# This code is contributed``# by Anant Agarwal.`

## C#

 `// C# implementation to find all possible``// triangles with XOR of sides zero``using` `System;` `class` `GFG {``    ` `    ``// function to find all triples which``    ``// satisfy the necessary condition``    ``static` `void` `find_all_possible_sides(``int` `n) {``        ` `        ``// selects first side``        ``for` `(``int` `x = n; x > 0; x--) {``    ` `            ``// select second side``            ``for` `(``int` `y = x - 1; y >= 0; y--) {``        ` `                ``// third side is equal to xor of``                ``// first and second side``                ``int` `z = x ^ y;``                ``if` `(z < x && z < y) {``        ` `                    ``// find longest side``                    ``int` `max_side = Math.Max(x,``                                 ``Math.Max(y, z));``            ` `                    ``// check if it can make a``                    ``// triangle``                    ``if` `(x + y + z - max_side >``                                     ``max_side) {``                                         ` `                        ``Console.WriteLine(x + ``" "``                                  ``+ y + ``" "` `+ z);``                    ``}``                ``}``            ``}``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main() {``        ` `        ``int` `n = 10;``        ` `        ``find_all_possible_sides(n);``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ` 0; ``\$x``--) {` `    ``// select second side``    ``for` `(``\$y` `= ``\$x` `- 1; ``\$y` `>= 0; ``\$y``--) {` `    ``// third side is equal to xor of``    ``// first and second side``    ``\$z` `= ``\$x` `^ ``\$y``;``    ``if` `(``\$z` `< ``\$x` `&& ``\$z` `< ``\$y``) {` `        ``// find longest side``        ``\$max_side` `= max(``\$x``, max(``\$y``, ``\$z``));` `        ``// check if it can make a triangle``        ``if` `(``\$x` `+ ``\$y` `+ ``\$z` `- ``\$max_side` `> ``\$max_side``)``        ``{``              ``echo` `\$x` `, ``" "` `,``\$y` `, ``" "``,``                   ``\$z` `,``"\n"` `;``        ``}``    ``}``    ``}``}` `}` `// Driver Code``\$n` `= 10;``find_all_possible_sides(``\$n``);` `// This code is contributed by anuj_67``?>`

## Javascript

 ``

Output:

```10 9 3
6 5 3```

My Personal Notes arrow_drop_up