Find all possible triangles with XOR of sides zero
Last Updated :
20 Feb, 2023
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 Complexity = 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++
#include <bits/stdc++.h>
using namespace std;
void find_all_possible_sides( int n) {
for ( int x = n; x > 0; x--) {
for ( int y = x - 1; y >= 0; y--) {
int z = x ^ y;
if (z < x && z < y) {
int max_side = max(x, max(y, z));
if (x + y + z - max_side > max_side) {
cout << x << " " << y << " "
<< z << endl;
}
}
}
}
}
int main() {
int n = 10;
find_all_possible_sides(n);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static void find_all_possible_sides( int n) {
for ( int x = n; x > 0 ; x--) {
for ( int y = x - 1 ; y >= 0 ; y--) {
int z = x ^ y;
if (z < x && z < y) {
int max_side = Math.max(x, Math.max(y, z));
if (x + y + z - max_side > max_side) {
System.out.println(x + " " + y + " " + z);
}
}
}
}
}
public static void main(String[] args) {
int n = 10 ;
find_all_possible_sides(n);
}
}
|
Python3
def find_all_possible_sides(n):
for x in range (n, 0 , - 1 ):
for y in range (x - 1 , - 1 , - 1 ):
z = x ^ y
if (z < x and z < y):
max_side = max (x, max (y, z))
if (x + y + z - max_side > max_side):
print (x , " " , y , " " ,
z)
n = 10
find_all_possible_sides(n)
|
C#
using System;
class GFG {
static void find_all_possible_sides( int n) {
for ( int x = n; x > 0; x--) {
for ( int y = x - 1; y >= 0; y--) {
int z = x ^ y;
if (z < x && z < y) {
int max_side = Math.Max(x,
Math.Max(y, z));
if (x + y + z - max_side >
max_side) {
Console.WriteLine(x + " "
+ y + " " + z);
}
}
}
}
}
public static void Main() {
int n = 10;
find_all_possible_sides(n);
}
}
|
PHP
<?php
function find_all_possible_sides( $n ) {
for ( $x = $n ; $x > 0; $x --) {
for ( $y = $x - 1; $y >= 0; $y --) {
$z = $x ^ $y ;
if ( $z < $x && $z < $y ) {
$max_side = max( $x , max( $y , $z ));
if ( $x + $y + $z - $max_side > $max_side )
{
echo $x , " " , $y , " " ,
$z , "\n" ;
}
}
}
}
}
$n = 10;
find_all_possible_sides( $n );
?>
|
Javascript
<script>
function find_all_possible_sides(n) {
for (let x = n; x > 0; x--) {
for (let y = x - 1; y >= 0; y--) {
let z = x ^ y;
if (z < x && z < y) {
let max_side = Math.max(x, Math.max(y, z));
if (x + y + z - max_side > max_side) {
document.write(x + " " + y + " "
+ z + "<br>" );
}
}
}
}
}
let n = 10;
find_all_possible_sides(n);
</script>
|
Output:
10 9 3
6 5 3
Time complexity: O(n^2) for given input n
Auxiliary space: O(1) it is using constant space
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...