K-th digit from the end of a number
Last Updated :
28 Jan, 2022
Given an integer N, the task is to find the Kth digit from the end of an integer N. If the Kth digit is not present, then print -1.
Examples:
Input: N = 2354, K = 2
Output: 5
Input: N = 1234, K = 1
Output: 4
Naive Approach: This simplest approach to solve this problem is converting the integer N to string. Follow the steps below to solve this problem:
- If K is less than equal to 0, then print -1 and return.
- Convert N into string and store in temp.
- If K is greater than size of temp, then print -1, otherwise print K character from last.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void kthDigitFromLast( int n, int k)
{
if (k <= 0) {
cout << -1 << endl;
return ;
}
string temp = to_string(n);
if (k > temp.length()) {
cout << -1 << endl;
}
else {
cout << temp[temp.length() - k] - '0' ;
}
}
int main()
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
|
Java
import java.io.*;
class GFG{
public static void kthDigitFromLast( int n, int k)
{
if (k <= 0 )
{
System.out.println(- 1 );
return ;
}
String temp = Integer.toString(n);
if (k > temp.length())
{
System.out.println(- 1 );
}
else
{
int index = temp.length() - k;
int res = temp.charAt(index) - '0' ;
System.out.println(res);
}
}
public static void main(String[] args)
{
int n = 2354 ;
int k = 2 ;
kthDigitFromLast(n, k);
}
}
|
Python3
def kthDigitFromLast(n, k):
if (k < = 0 ):
print ( - 1 )
return
temp = str (n)
if (k > len (temp)):
print ( - 1 )
else :
print ( ord (temp[ len (temp) - k]) - ord ( '0' ))
if __name__ = = '__main__' :
n = 2354
k = 2
kthDigitFromLast(n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void kthDigitFromLast( int n, int k)
{
if (k <= 0)
{
Console.Write(-1);
return ;
}
string temp = n.ToString();
if (k > temp.Length)
{
Console.WriteLine(-1);
}
else
{
Console.Write(temp[temp.Length - k] - 48);
}
}
public static void Main()
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
}
|
Javascript
<script>
function kthDigitFromLast(n, k)
{
if (k <= 0)
{
document.write(-1);
return ;
}
var temp = String(n);
if (k > temp.length)
{
document.write(-1);
}
else
{
var req = temp.charAt(temp.length - k)
document.write(Number(req));
}
}
var n = 2354;
var k = 2;
kthDigitFromLast(n, k);
</script>
|
Time complexity: O(d), where d is the number of digits in number N.
Auxiliary Space: O(d)
Efficient Approach: This problem can be solved by iterating over the digits of the number N. Follow the steps below to solve this problem:
- If K is less than equal to 0, then print -1 and return.
- Iterate while N and K-1 are greater than 0 :
- Update N as N/10 and decrement K by 1.
- If N is 0, then print -1, otherwise print N%10.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void kthDigitFromLast( int n, int k)
{
if (k <= 0) {
cout << -1 << endl;
return ;
}
while ((k - 1) > 0 && n > 0) {
n = n / 10;
k--;
}
if (n == 0) {
cout << -1 << endl;
}
else {
cout << n % 10 << endl;
}
}
int main()
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
|
Java
import java.io.*;
class GFG{
public static void kthDigitFromLast( int n, int k)
{
if (k <= 0 )
{
System.out.println(- 1 );
return ;
}
while ((k - 1 ) > 0 && n > 0 )
{
n = n / 10 ;
k--;
}
if (n == 0 )
{
System.out.println(- 1 );
}
else
{
System.out.println(n % 10 );
}
}
public static void main(String[] args)
{
int n = 2354 ;
int k = 2 ;
kthDigitFromLast(n, k);
}
}
|
Python3
def kthDigitFromLast( n, k):
if (k < = 0 ):
print ( "-1" )
return
while ((k - 1 ) > 0 and n > 0 ):
n = n / 10
k - = 1
if (n = = 0 ):
print ( "-1" )
else :
print ( int (n % 10 ))
if __name__ = = '__main__' :
n = 2354
k = 2
kthDigitFromLast(n, k)
|
C#
using System;
class GFG
{
public static void kthDigitFromLast( int n, int k)
{
if (k <= 0)
{
Console.Write(-1);
return ;
}
while ((k - 1) > 0 && n > 0)
{
n = n / 10;
k--;
}
if (n == 0)
{
Console.Write(-1);
}
else
{
Console.Write(n % 10);
}
}
public static void Main(String[] args)
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
}
|
Javascript
<script>
function kthDigitFromLast(n, k)
{
if (k <= 0)
{
document.write(-1);
return ;
}
while ((k - 1) > 0 && n > 0)
{
n = n / 10;
k--;
}
if (n == 0)
{
document.write(-1);
}
else
{
document.write(parseInt(n % 10));
}
}
var n = 2354;
var k = 2;
kthDigitFromLast(n, k);
</script>
|
Time complexity: O(d), where d is the number of digits in number N.
Auxiliary Space: O(1)
Efficient Approach: This problem can be solved using power function. Follow the steps below to solve this problem:
- If K is less than equal to 0, then print -1 and return.
- Initialize a variable say, divisor as pow(10, K-1).
- If divisor is greater than N, then print -1, otherwise print (N/divisor) %10.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void kthDigitFromLast( int n, int k)
{
if (k <= 0) {
cout << -1 << endl;
return ;
}
int divisor = ( int ) pow (10, k - 1);
if (divisor > n) {
cout << -1 << endl;
}
else {
cout << (n / divisor) % 10 << endl;
}
}
int main()
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
|
Java
import java.io.*;
class GFG{
public static void kthDigitFromLast( int n, int k)
{
if (k <= 0 )
{
System.out.println(- 1 );
return ;
}
int diviser = ( int )Math.pow( 10 , k - 1 );
if (diviser > n)
{
System.out.println(- 1 );
}
else
{
System.out.println((n / diviser) % 10 );
}
}
public static void main(String[] args)
{
int n = 2354 ;
int k = 2 ;
kthDigitFromLast(n, k);
}
}
|
Python3
def kthDigitFromLast(n, k):
if (k < = 0 ):
print ( "-1" )
return
divisor = int ( pow ( 10 , k - 1 ))
if (divisor > n):
print ( "-1" )
else :
print ( int ((n / divisor) % 10 ))
n = 2354 ;
k = 2 ;
kthDigitFromLast(n, k);
|
C#
using System;
class GFG{
public static void kthDigitFromLast( int n, int k)
{
if (k <= 0)
{
Console.Write(-1);
return ;
}
int diviser = ( int )Math.Pow(10, k - 1);
if (diviser > n)
{
Console.Write(-1);
}
else
{
Console.Write((n / diviser) % 10);
}
}
public static void Main(String[] args)
{
int n = 2354;
int k = 2;
kthDigitFromLast(n, k);
}
}
|
Javascript
<script>
function kthDigitFromLast(n, k)
{
if (k <= 0)
{
document.write(-1);
return ;
}
var diviser = parseInt(Math.pow(10, k - 1));
if (diviser > n)
{
document.write(-1);
}
else
{
document.write(parseInt((n / diviser) % 10));
}
}
var n = 2354;
var k = 2;
kthDigitFromLast(n, k);
</script>
|
Time complexity: O(log(K)), where d is the number of digits in number N. This time complexity is due to the calculation of power of 10 using power function.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...