Find all possible triangles with XOR of sides zero

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find all possible 
// triangles with XOR of sides zero
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to find all possible 
// triangles with XOR of sides zero
  
// function to find all triples which
// satisfy the necessary condition
function find_all_possible_sides($n) {
  
// selects first side
for ($x = $n; $x > 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
?>

chevron_right



Output:

10 9 3
6 5 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Improved By : vt_m



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.