Minimum value of N such that xor from 1 to N is equal to K
Given a value K which is the XOR of all the values from 1 to N, the task is to find the minimum value of N such that XOR from 1 to N is equal to K.
Examples:
Input: K = 7
Output: 6
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7
Input: K = 10
Output: Not Possible
Approach: This problem is similar to the Calculate XOR from 1 to n. Below are the conditions to be checked:
- If k = 0, then N = 3.
- If k = 1, then N = 1.
- If k % 4 = 0, then N = k.
- If k % 4 = 3, then N = k-1.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findN( int k)
{
int ans;
if (k == 0)
ans = 3;
if (k == 1)
ans = 1;
else if (k % 4 == 0)
ans = k;
else if (k % 4 == 3)
ans = k - 1;
else
ans = -1;
return ans;
}
int main()
{
int k = 7;
int res = findN(k);
if (res == -1)
cout << "Not possible" ;
else
cout << res;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findN( int k)
{
int ans;
if (k == 0 )
ans = 3 ;
if (k == 1 )
ans = 1 ;
else if (k % 4 == 0 )
ans = k;
else if (k % 4 == 3 )
ans = k - 1 ;
else
ans = - 1 ;
return ans;
}
public static void main (String[] args)
{
int k = 7 ;
int res = findN(k);
if (res == - 1 )
System.out.println( "Not possible" );
else
System.out.println(res);
}
}
|
Python3
def findN(k) :
if (k = = 0 ) :
ans = 3
if (k = = 1 ) :
ans = 1
elif (k % 4 = = 0 ) :
ans = k
elif (k % 4 = = 3 ) :
ans = k - 1
else :
ans = - 1
return ans
k = 7
res = findN(k)
if (res = = - 1 ):
print ( "Not possible" )
else :
print (res)
|
C#
using System;
class GFG
{
static int findN( int k)
{
int ans;
if (k == 0)
ans = 3;
if (k == 1)
ans = 1;
else if (k % 4 == 0)
ans = k;
else if (k % 4 == 3)
ans = k - 1;
else
ans = -1;
return ans;
}
public static void Main ()
{
int k = 7;
int res = findN(k);
if (res == -1)
Console.WriteLine( "Not possible" );
else
Console.WriteLine(res);
}
}
|
PHP
<?php
function findN( $k )
{
$ans ;
if ( $k == 0)
$ans = 3;
if ( $k == 1)
$ans = 1;
else if ( $k % 4 == 0)
$ans = $k ;
else if ( $k % 4 == 3)
$ans = $k - 1;
else
$ans = -1;
return $ans ;
}
$k = 7;
$res = findN( $k );
if ( $res == -1)
echo "Not possible" ;
else
echo $res ;
?>
|
Javascript
<script>
function findN(k)
{
var ans;
if (k == 0)
ans = 3;
if (k == 1)
ans = 1;
else if (k % 4 == 0)
ans = k;
else if (k % 4 == 3)
ans = k - 1;
else
ans = -1;
return ans;
}
var k = 7;
var res = findN(k);
if (res == -1)
document.write( "Not possible" );
else
document.write(res);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
How does this work?
When we do XOR of numbers, we get 0 as XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.
Number Binary-Repr XOR-from-1-to-n
1 1 [0001]
2 10 [0011]
3 11 [0000] <----- We get a 0
4 100 [0100] <----- Equals to n
5 101 [0001]
6 110 [0111]
7 111 [0000] <----- We get 0
8 1000 [1000] <----- Equals to n
9 1001 [0001]
10 1010 [1011]
11 1011 [0000] <------ We get 0
12 1100 [1100] <------ Equals to n
Last Updated :
31 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...