Ascending order of three integers after XOR operation
Last Updated :
19 Sep, 2023
Given three distinct integers X, Y, Z. Find an integer N that after performing XOR of each element, integers must follow that X should be minimum and Z should be maximum between three integers. If possible then print the integer, Otherwise print -1.
Examples:
Input: 4, 5, 6
Output: 0
Explanation: 4 XOR 0 = 4, 5 XOR 0 = 5, 6 XOR 0 = 6, so 4 is minimum and 6 is maximum, .
Input: 12, 9, 8
Output: 15
Approach: To solve the problem follow the below idea:
The approach checks if the three integers are sorted in ascending or descending order. If it is sorted in ascending order, it outputs 0, because we know that Any Number XOR 0 = That Number. If it is sorted in descending order, calculates the next positive integer of the maximum integer which is a power of 2, and prints that number -1. If the array is not sorted, it outputs -1.
- Check if the integers are sorted in ascending order by comparing X with Y and Y with Z. If it is, output 0.
- Check if the array is sorted in descending order by comparing X with Y and Y with Z. If it is, compute the next positive integer of the maximum integer which is a power of 2 minus 1 using the logarithm and power functions, and output this value.
- If the array is not sorted, output -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortThreeInteger( int X, int Y, int Z, int n)
{
if (X < Y && Y < Z) {
cout << 0 << endl;
}
else if (X > Y && Y > Z) {
int d = log2(X);
int u = pow (2, d + 1);
cout << u - 1 << endl;
}
else {
cout << -1 << endl;
}
}
int main()
{
int X = 12, Y = 9, Z = 8;
sortThreeInteger(X, Y, Z, 3);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static void sortThreeInteger( int X, int Y, int Z, int n) {
if (X < Y && Y < Z) {
System.out.println( 0 );
}
else if (X > Y && Y > Z) {
int d = ( int ) (Math.log(X) / Math.log( 2 ));
int u = ( int ) Math.pow( 2 , d + 1 );
System.out.println(u - 1 );
}
else {
System.out.println(- 1 );
}
}
public static void main(String[] args) {
int X = 12 , Y = 9 , Z = 8 ;
sortThreeInteger(X, Y, Z, 3 );
}
}
|
Python3
import math
def sortThreeInteger(X, Y, Z, n):
if X < Y and Y < Z:
print ( 0 )
elif X > Y and Y > Z:
d = math.ceil(math.log2(X))
u = 2 * * d
print (u - 1 )
else :
print ( - 1 )
X = 12
Y = 9
Z = 8
sortThreeInteger(X, Y, Z, 3 )
|
C#
using System;
class Program
{
static void SortThreeInteger( int X, int Y, int Z)
{
if (X < Y && Y < Z)
{
Console.WriteLine(0);
}
else if (X > Y && Y > Z)
{
int d = ( int )Math.Log(X, 2);
int u = ( int )Math.Pow(2, d + 1);
Console.WriteLine(u - 1);
}
else
{
Console.WriteLine(-1);
}
}
static void Main( string [] args)
{
int X = 12, Y = 9, Z = 8;
SortThreeInteger(X, Y, Z);
}
}
|
Javascript
function sortThreeInteger(X, Y, Z, n) {
if (X < Y && Y < Z) {
console.log(0);
}
else if (X > Y && Y > Z) {
let d = Math.ceil(Math.log2(X));
let u = Math.pow(2, d );
console.log(u - 1);
}
else {
console.log(-1);
}
}
let X = 12, Y = 9, Z = 8;
sortThreeInteger(X, Y, Z, 3);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...