Given an integer N, The task is to find the absolute value of the given integer.
Examples:
Input: N = -6
Output: 6
Explanation: The absolute value of -6 is 6 which is non negative
Input: N = 12
Output: 12
Naive Approach: To solve the problem follow the below idea:
The absolute value of any number is always positive. For any positive number, the absolute value is the number itself and for any negative number, the absolute value is (-1) multiplied by the negative number
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findAbsolute( int N)
{
if (N < 0)
{
N = (-1) * N;
}
cout << " " << N;
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
C
#include <stdio.h>
void findAbsolute( int N)
{
if (N < 0) {
N = (-1) * N;
}
printf ( "%d " , N);
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
Java
class GFG{
static void findAbsolute( int N)
{
if (N < 0 )
{
N = (- 1 ) * N;
}
System.out.printf( "%d " , N);
}
public static void main(String[] args)
{
int N = - 12 ;
findAbsolute(N);
}
}
|
Python3
def findAbsolute(N):
if (N < 0 ):
N = ( - 1 ) * N;
print (N);
if __name__ = = '__main__' :
N = - 12 ;
findAbsolute(N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findAbsolute( int N)
{
if (N < 0)
{
N = (-1) * N;
}
Console.Write( "{0} " , N);
}
public static void Main(String[] args)
{
int N = -12;
findAbsolute(N);
}
}
|
Javascript
<script>
function findAbsolute(N)
{
if (N < 0)
{
N = (-1) * N;
}
document.write( " " + N);
}
let N = -12;
findAbsolute(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Find the absolute value of a given number Using Bitmasking:
To solve the problem follow the below idea:
Negative numbers are stored in the form of 2s complement, to get the absolute value we have to toggle bits of the number and add 1 to the result.
Follow the steps below to implement the idea:
- Set the mask as right shift of the integer by 31 (assuming integers are stored using 32 bits) mask = n >> 31
- For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. Add the mask to the given number i.e. mask + n
- XOR of mask + n and mask gives the absolute value i.e.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findAbsolute( int N)
{
int mask = N >> ( sizeof ( int ) * CHAR_BIT - 1);
cout << ((mask + N) ^ mask);
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
C
#include <stdio.h>
#define CHAR_BIT 8
void findAbsolute( int N)
{
int mask
= N
>> ( sizeof ( int ) * CHAR_BIT - 1);
printf ( "%d " , (mask + N) ^ mask);
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
Java
class GFG{
static final int CHAR_BIT = 8 ;
static void findAbsolute( int N)
{
int mask = N >> (Integer.SIZE / 8 *
CHAR_BIT - 1 );
System.out.printf( "%d " , (mask + N) ^ mask);
}
public static void main(String[] args)
{
int N = - 12 ;
findAbsolute(N);
}
}
|
Python3
import sys
CHAR_BIT = 8 ;
def findAbsolute(N):
mask = N >> (sys.getsizeof( int ()) / / 8 *
CHAR_BIT - 1 );
print ((mask + N) ^ mask);
if __name__ = = '__main__' :
N = - 12 ;
findAbsolute(N);
|
C#
using System;
class GFG{
static readonly int CHAR_BIT = 8;
static void findAbsolute( int N)
{
int mask = N >> ( sizeof ( int ) / 8 *
CHAR_BIT - 1);
Console.Write((mask + N) ^ mask);
}
public static void Main(String[] args)
{
int N = -12;
findAbsolute(N);
}
}
|
Javascript
<script>
let CHAR_BIT = 8;
function findAbsolute(N)
{
let mask = N >> (4 / 8 * CHAR_BIT - 1);
document.write((mask + N) ^ mask);
}
let N = -12;
findAbsolute(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Find the absolute value of a given number Using the inbuilt abs() function:
To solve the problem follow the below idea:
The inbuilt function abs() in stdlib.h library finds the absolute value of any number. This can be used to find absolute value of any integer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findAbsolute( int N)
{
int X = abs (N);
cout << X;
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
void findAbsolute( int N)
{
int X = abs (N);
printf ( "%d " , X);
}
int main()
{
int N = -12;
findAbsolute(N);
return 0;
}
|
Java
class GFG{
static void findAbsolute( int N)
{
int X = Math.abs(N);
System.out.printf( "%d" , X);
}
public static void main(String[] args)
{
int N = - 12 ;
findAbsolute(N);
}
}
|
Python3
import math
def findAbsolute(N):
X = abs (N);
print (X);
N = - 12 ;
findAbsolute(N);
|
C#
using System;
class GFG{
static void findAbsolute( int N)
{
int X = Math.Abs(N);
Console.Write( "{0}" , X);
}
public static void Main(String[] args)
{
int N = -12;
findAbsolute(N);
}
}
|
Javascript
<script>
function findAbsolute(N)
{
let X = Math.abs(N);
document.write(X);
}
let N = -12;
findAbsolute(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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!
Last Updated :
14 Sep, 2022
Like Article
Save Article