Write a program to add one to a given number. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc are not allowed.
Examples:
Input: 12
Output: 13
Input: 6
Output: 7
Method: Adding 1 to a given number by importing add function and without using +,- etc.
C++
#include <iostream>
#include <functional>
using namespace std;
int main() {
int n = 6;
std::function< int ( int )> addOne = []( int x) -> int { return x + 1; };
cout << addOne(n) << endl;
return 0;
}
|
Java
import java.util.function.IntUnaryOperator;
public class AddOne {
public static void main(String[] args) {
int n = 6 ;
IntUnaryOperator addOne = x -> x + 1 ;
System.out.println(addOne.applyAsInt(n));
}
}
|
Python3
import operator
n = 6
print (operator.add(n, 1 ))
|
C#
using System;
public class AddOne
{
public static void Main( string [] args)
{
int n = 6;
System.Func< int , int > addOne = x => x + 1;
System.Console.WriteLine(addOne(n));
}
}
|
Javascript
const n = 6;
const addOne = (x) => x + 1;
console.log(addOne(n));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 1
To add 1 to a number x (say 0011000111), flip all the bits after the rightmost 0 bit (we get 0011000000). Finally, flip the rightmost 0 bit also (we get 0011001000) to get the answer.
C++
#include <bits/stdc++.h>
using namespace std;
int addOne( int x)
{
int m = 1;
while ( x & m )
{
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
int main()
{
cout<<addOne(13);
return 0;
}
|
C
#include <stdio.h>
int addOne( int x)
{
int m = 1;
while ( x & m )
{
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
int main()
{
printf ( "%d" , addOne(13));
getchar ();
return 0;
}
|
Java
import java.io.*;
class GFG {
static int addOne( int x)
{
int m = 1 ;
while ( ( int )(x & m) >= 1 )
{
x = x ^ m;
m <<= 1 ;
}
x = x ^ m;
return x;
}
public static void main(String[] args)
{
System.out.println(addOne( 13 ));
}
}
|
Python3
def addOne(x) :
m = 1 ;
while (x & m):
x = x ^ m
m << = 1
x = x ^ m
return x
n = 13
print (addOne(n))
|
C#
using System;
class GFG {
static int addOne( int x)
{
int m = 1;
while ( ( int )(x & m) == 1)
{
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
public static void Main()
{
Console.WriteLine(addOne(13));
}
}
|
PHP
<?php
function addOne( $x )
{
$m = 1;
while ( $x & $m )
{
$x = $x ^ $m ;
$m <<= 1;
}
$x = $x ^ $m ;
return $x ;
}
echo addOne(13);
?>
|
Javascript
<script>
function addOne( x) {
let m = 1;
while ( x & m ) {
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
document.write(addOne(13));
</script>
|
Time Complexity: O(log2n)
Auxiliary Space: O(1)
Method 2
We know that the negative number is represented in 2’s complement form on most of the architectures. We have the following lemma hold for 2’s complement representation of signed numbers.
Say, x is numerical value of a number, then
~x = -(x+1) [ ~ is for bitwise complement ]
(x + 1) is due to the addition of 1 in 2’s complement conversion
To get (x + 1) apply negation once again. So, the final expression becomes (-(~x)).
C++
#include <bits/stdc++.h>
using namespace std;
int addOne( int x)
{
return (-(~x));
}
int main()
{
cout<<addOne(13);
return 0;
}
|
C
#include<stdio.h>
int addOne( int x)
{
return (-(~x));
}
int main()
{
printf ( "%d" , addOne(13));
getchar ();
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int addOne( int x)
{
return (-(~x));
}
public static void main(String[] args)
{
System.out.printf( "%d" , addOne( 13 ));
}
}
|
Python3
def addOne(x):
return ( - (~x));
print (addOne( 13 ))
|
C#
using System;
class GFG
{
static int addOne( int x)
{
return (-(~x));
}
public static void Main()
{
Console.WriteLine(addOne(13));
}
}
|
PHP
<?php
function addOne( $x )
{
return (-(~ $x ));
}
echo addOne(13);
?>
|
Javascript
<script>
function addOne(x)
{
return (-(~x));
}
document.write(addOne(13));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Example :
Assume the machine word length is one *nibble* for simplicity.
And x = 2 (0010),
~x = ~2 = 1101 (13 numerical)
-~x = -1101
Interpreting bits 1101 in 2’s complement form yields numerical value as -(2^4 – 13) = -3. Applying ‘-‘ on the result leaves 3. The same analogy holds for decrement. Note that this method works only if the numbers are stored in 2’s complement form.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!