Absolute difference between the first X and last X Digits of N
Last Updated :
22 Jun, 2022
Given two integers N and X. The task is to print the absolute difference between the first X and last X digits in N. Considering the number of digits is atleast 2*x.
Examples:
Input: N = 21546, X = 2
Output: 25
The first two digit in 21546 is 21.
The last two digit in 21546 is 46.
The absolute difference of 21 and 46 is 25.
Input: N = 351684617, X = 3
Output: 266
Simple Approach:
- Store the last x digits of the number in last.
- Find the number of digits in the number.
- Remove all the digits except the first x.
- Store the first x integers of the number in first.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long digitsCount( long long n)
{
int len = 0;
while (n > 0) {
len++;
n /= 10;
}
return len;
}
long long absoluteFirstLast( long long n, int x)
{
int i = 0, mod = 1;
while (i < x) {
mod *= 10;
i++;
}
int last = n % mod;
long long len = digitsCount(n);
while (len != x) {
n /= 10;
len--;
}
int first = n;
return abs (first - last);
}
int main()
{
long long n = 21546, x = 2;
cout << absoluteFirstLast(n, x);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int digitsCount( int n)
{
int len = 0 ;
while (n > 0 )
{
len++;
n /= 10 ;
}
return len;
}
static int absoluteFirstLast( int n, int x)
{
int i = 0 , mod = 1 ;
while (i < x)
{
mod *= 10 ;
i++;
}
int last = n % mod;
int len = digitsCount(n);
while (len != x)
{
n /= 10 ;
len--;
}
int first = n;
return Math.abs(first - last);
}
public static void main(String args[])
{
int n = 21546 , x = 2 ;
System.out.println(absoluteFirstLast(n, x));
}
}
|
Python3
def digitsCount(n) :
length = 0 ;
while (n > 0 ) :
length + = 1 ;
n / / = 10 ;
return length;
def absoluteFirstLast(n, x) :
i = 0 ;
mod = 1 ;
while (i < x) :
mod * = 10 ;
i + = 1 ;
last = n % mod;
length = digitsCount(n);
while (length ! = x) :
n / / = 10 ;
length - = 1 ;
first = n;
return abs (first - last);
if __name__ = = "__main__" :
n = 21546 ;
x = 2 ;
print (absoluteFirstLast(n, x));
|
C#
using System;
class GFG
{
static int digitsCount( int n)
{
int len = 0;
while (n > 0)
{
len++;
n /= 10;
}
return len;
}
static int absoluteFirstLast( int n, int x)
{
int i = 0, mod = 1;
while (i < x)
{
mod *= 10;
i++;
}
int last = n % mod;
int len = digitsCount(n);
while (len != x)
{
n /= 10;
len--;
}
int first = n;
return Math.Abs(first - last);
}
public static void Main(String []args)
{
int n = 21546, x = 2;
Console.Write(absoluteFirstLast(n, x));
}
}
|
PHP
<?php
function digitsCount( $n )
{
$len = 0;
while ( $n > 0)
{
$len ++;
$n = (int)( $n / 10);
}
return $len ;
}
function absoluteFirstLast( $n , $x )
{
$i = 0;
$mod = 1;
while ( $i < $x )
{
$mod *= 10;
$i ++;
}
$last = $n % $mod ;
$len = digitsCount( $n );
while ( $len != $x )
{
$n = (int)( $n / 10);
$len --;
}
$first = $n ;
return abs ( $first - $last );
}
$n = 21546;
$x = 2;
echo absoluteFirstLast( $n , $x );
?>
|
Javascript
<script>
function digitsCount(n)
{
let len = 0;
while (n > 0)
{
len++;
n = Math.floor(n / 10);
}
return len;
}
function absoluteFirstLast(n, x)
{
let i = 0, mod = 1;
while (i < x)
{
mod *= 10;
i++;
}
let last = n % mod;
let len = digitsCount(n);
while (len != x)
{
n = Math.floor(n / 10);
len--;
}
let first = n;
return Math.abs(first - last);
}
let n = 21546, x = 2;
document.write(absoluteFirstLast(n, x));
</script>
|
Time Complexity: O(X+k) where X is the given integer and k is the number of digits in n.
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...