📜  从数字末尾开始的第 K 个数字

📅  最后修改于: 2022-05-13 01:56:10.250000             🧑  作者: Mango

从数字末尾开始的第 K 个数字

给定一个整数N ,任务是从整数N的末尾找到第K数字。如果第K不存在,则打印-1

例子:

天真的方法:解决这个问题的最简单方法是 将整数 N 转换为字符串。请按照以下步骤解决此问题:

  • 如果K小于等于0,则打印-1并返回。
  • N转换为字符串并存储在temp中。
  • 如果K大于temp的大小,则打印-1 ,否则从最后一个打印K个字符。

下面是上述方法的实现:

C++
// c++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the kth digit
// from last in an integer n
void kthDigitFromLast(int n, int k)
{
 
    // If k is less than equal to 0
    if (k <= 0) {
        cout << -1 << endl;
        return;
    }
 
    // Convert integer into string
    string temp = to_string(n);
 
    // If k is greater than length of the
    // string temp
    if (k > temp.length()) {
        cout << -1 << endl;
    }
    // Print the k digit from last
    else {
        cout << temp[temp.length() - k] - '0';
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n   
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        System.out.println(-1);
        return;
    }
 
    // Convert integer into string
    String temp = Integer.toString(n);
 
    // If k is greater than length of the
    // string temp
    if (k > temp.length())
    {
        System.out.println(-1);
    }
     
    // Print the k digit from last
    else
    {
        int index = temp.length() - k;
        int res = temp.charAt(index) - '0';
        System.out.println(res);
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find the kth digit
# from last in an integer n
def kthDigitFromLast(n, k):
 
    # If k is less than equal to 0
    if (k <= 0):
        print(-1)
        return
 
    # Convert integer into string
    temp = str(n)
 
    # If k is greater than length of the
    # temp
    if (k > len(temp)):
        print(-1)
         
    # Print the k digit from last
    else:
        print(ord(temp[len(temp) - k]) - ord('0'))
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    n = 2354
    k = 2
 
    # Function call
    kthDigitFromLast(n, k)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        Console.Write(-1);
        return;
    }
 
    // Convert integer into string
    string temp = n.ToString();
 
    // If k is greater than length of the
    // string temp
    if (k > temp.Length)
    {
        Console.WriteLine(-1);
    }
     
    // Print the k digit from last
    else
    {
        Console.Write(temp[temp.Length - k] - 48);
    }
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


C++
// c++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the kth digit
// from last in an integer n
void kthDigitFromLast(int n, int k)
{
    // If k is less than equal to 0
    if (k <= 0) {
        cout << -1 << endl;
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0) {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0) {
        cout << -1 << endl;
    }
    // Print the right most digit
    else {
        cout << n % 10 << endl;
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        System.out.println(-1);
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0)
    {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0)
    {
        System.out.println(-1);
    }
     
    // Print the right most digit
    else
    {
        System.out.println(n % 10);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
# Function to find the kth digit
# from last in an eger n
def kthDigitFromLast( n,  k):
 
    # If k is less than equal to 0
    if (k <= 0):
        print("-1")
        return
     
    # Divide the number n by 10
    # upto k-1 times
    while ((k - 1) > 0 and n > 0):
        n = n / 10
        k -= 1
     
    # If the number n is equal 0
    if (n == 0):
        print("-1")
     
    # Pr the right most digit
    else:
        print(int(n % 10 ))
     
# Driver code
if __name__ == '__main__':
     
    # Given Input
    n = 2354
    k = 2
 
    # Function call
    kthDigitFromLast(n, k)
     
# this code is contributed by shivanisinghss2110


C#
// C# program for the above approach
using System;
class GFG
{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        Console.Write(-1);
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0)
    {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0)
    {
        Console.Write(-1);
    }
     
    // Print the right most digit
    else
    {
        Console.Write(n % 10);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// c++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the kth digit
// from last in an integer n
void kthDigitFromLast(int n, int k)
{
    // If k is less than equal to 0
    if (k <= 0) {
        cout << -1 << endl;
        return;
    }
 
    // Calculate kth digit using power function
    int divisor = (int)pow(10, k - 1);
 
    // If divisor is greater than n
    if (divisor > n) {
        cout << -1 << endl;
    }
    // Otherwise print kth digit from last
    else {
        cout << (n / divisor) % 10 << endl;
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        System.out.println(-1);
        return;
    }
 
    // Calculate kth digit using power function
    int diviser = (int)Math.pow(10, k - 1);
 
    // If diviser is greater than n
    if (diviser > n)
    {
        System.out.println(-1);
    }
     
    // Otherwise print kth digit from last
    else
    {
        System.out.println((n / diviser) % 10);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to find the kth digit
# from last in an integer n
def kthDigitFromLast(n, k):
     
    # If k is less than equal to 0
    if (k <= 0):
        print("-1")
        return
     
    # Calculate kth digit using power function
    divisor = int(pow(10, k - 1))
 
    # If divisor is greater than n
    if (divisor > n):
        print("-1")
     
    # Otherwise print kth digit from last
    else:
        print(int((n / divisor) % 10))
     
# Given Input
n = 2354;
k = 2;
 
# Function call
kthDigitFromLast(n, k);
 
# This code is contributed by SoumikMondal


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        Console.Write(-1);
        return;
    }
 
    // Calculate kth digit using power function
    int diviser = (int)Math.Pow(10, k - 1);
 
    // If diviser is greater than n
    if (diviser > n)
    {
        Console.Write(-1);
    }
     
    // Otherwise print kth digit from last
    else
    {
        Console.Write((n / diviser) % 10);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
5

时间复杂度: O(d),其中 d 是数字N的位数。
辅助空间: O(d)

有效的方法:这个问题可以通过迭代数字 N 的数字来解决。按照以下步骤解决这个问题:

  • 如果K小于等于0,则打印-1并返回。
  • NK-1大于0时迭代:
    • N更新为N/10并将K1
  • 如果N0 ,则打印-1 ,否则打印N%10

下面是上述方法的实现:

C++

// c++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the kth digit
// from last in an integer n
void kthDigitFromLast(int n, int k)
{
    // If k is less than equal to 0
    if (k <= 0) {
        cout << -1 << endl;
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0) {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0) {
        cout << -1 << endl;
    }
    // Print the right most digit
    else {
        cout << n % 10 << endl;
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        System.out.println(-1);
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0)
    {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0)
    {
        System.out.println(-1);
    }
     
    // Print the right most digit
    else
    {
        System.out.println(n % 10);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by Potta Lokesh

Python3

# Python program for the above approach
# Function to find the kth digit
# from last in an eger n
def kthDigitFromLast( n,  k):
 
    # If k is less than equal to 0
    if (k <= 0):
        print("-1")
        return
     
    # Divide the number n by 10
    # upto k-1 times
    while ((k - 1) > 0 and n > 0):
        n = n / 10
        k -= 1
     
    # If the number n is equal 0
    if (n == 0):
        print("-1")
     
    # Pr the right most digit
    else:
        print(int(n % 10 ))
     
# Driver code
if __name__ == '__main__':
     
    # Given Input
    n = 2354
    k = 2
 
    # Function call
    kthDigitFromLast(n, k)
     
# this code is contributed by shivanisinghss2110

C#

// C# program for the above approach
using System;
class GFG
{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        Console.Write(-1);
        return;
    }
 
    // Divide the number n by 10
    // upto k-1 times
    while ((k - 1) > 0 && n > 0)
    {
        n = n / 10;
        k--;
    }
 
    // If the number n is equal 0
    if (n == 0)
    {
        Console.Write(-1);
    }
     
    // Print the right most digit
    else
    {
        Console.Write(n % 10);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出
5

时间复杂度: O(d),其中 d 是数字N的位数。
辅助空间: O(1)

有效方法:这个问题可以使用幂函数来解决。请按照以下步骤解决此问题:

  • 如果K小于等于0,则打印-1并返回。
  • 初始化一个变量,比如除数为 pow(10, K-1)。
  • 如果除数大于N,则打印-1,否则打印(N/divisor) %10。

下面是上述方法的实现:

C++

// c++ program for the above approach
#include 
 
using namespace std;
 
// Function to find the kth digit
// from last in an integer n
void kthDigitFromLast(int n, int k)
{
    // If k is less than equal to 0
    if (k <= 0) {
        cout << -1 << endl;
        return;
    }
 
    // Calculate kth digit using power function
    int divisor = (int)pow(10, k - 1);
 
    // If divisor is greater than n
    if (divisor > n) {
        cout << -1 << endl;
    }
    // Otherwise print kth digit from last
    else {
        cout << (n / divisor) % 10 << endl;
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        System.out.println(-1);
        return;
    }
 
    // Calculate kth digit using power function
    int diviser = (int)Math.pow(10, k - 1);
 
    // If diviser is greater than n
    if (diviser > n)
    {
        System.out.println(-1);
    }
     
    // Otherwise print kth digit from last
    else
    {
        System.out.println((n / diviser) % 10);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by Potta Lokesh

Python3

# Python program for the above approach
 
# Function to find the kth digit
# from last in an integer n
def kthDigitFromLast(n, k):
     
    # If k is less than equal to 0
    if (k <= 0):
        print("-1")
        return
     
    # Calculate kth digit using power function
    divisor = int(pow(10, k - 1))
 
    # If divisor is greater than n
    if (divisor > n):
        print("-1")
     
    # Otherwise print kth digit from last
    else:
        print(int((n / divisor) % 10))
     
# Given Input
n = 2354;
k = 2;
 
# Function call
kthDigitFromLast(n, k);
 
# This code is contributed by SoumikMondal

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the kth digit
// from last in an integer n
public static void kthDigitFromLast(int n, int k)
{
     
    // If k is less than equal to 0
    if (k <= 0)
    {
        Console.Write(-1);
        return;
    }
 
    // Calculate kth digit using power function
    int diviser = (int)Math.Pow(10, k - 1);
 
    // If diviser is greater than n
    if (diviser > n)
    {
        Console.Write(-1);
    }
     
    // Otherwise print kth digit from last
    else
    {
        Console.Write((n / diviser) % 10);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 2354;
    int k = 2;
 
    // Function call
    kthDigitFromLast(n, k);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出
5

时间复杂度: O(log(K)),其中 d 是数字N的位数。这个时间复杂度是由于使用幂函数计算 10 的幂。
辅助空间: O(1)