Given two integers a and b in the form of strings. Return the last digit of ab.
Examples:
Input: a = "3", b = "10"
Output: 9
Explanation: 310 = 59049. The last digit is 9.Input: a = "6", b = "2"
Output: 6
Explanation: 62 = 36. The last digit is 6.
Try It Yourself
Using Cyclicity of Last Digit (Mod 10) - O(m) Time O(1) Space
The last digit of powers follows a repeating cycle (maximum length = 4). Instead of computing large powers, we reduce the exponent using modulo 4 and use only the last digit of the base.
Here are few examples numbers and last digits of their powers
- 1: 1, 1, 1, 1, 1, 1, .... [Cycle Length is 1]
- 2: 4, 8, 6, 2, 4, 8, 6, 2,... [Cycle Length is 4]
- 3: 9, 7, 1, 3, 9, 7, 1, 3,... [Cycle Length is 4]
- 4: 6, 4, 6, 4, 6, 4, 6, 4, ...... [Cycle Length is 2]
- 5: 5, 5, 5, 5, 5, ... [Cycle Length is 1]
- 6: 6, 6, 6, 6, 6...... [Cycle Length is 1]
- 7: 9, 3, 1, 7, 9, 3, 1, 7,...... [Cycle Length is 4]
- 8: 4, 2, 6, 8, 4, 2, 6, 8, ..... [Cycle Length is 4]
- 9: 1, 9, 1, 9, 1, 9...... [Cycle Length is 2]
Algorithm:
- Extract the last digit of base a.
- Compute b % 4 using string-based modulo (since b is large).
- If b % 4 == 0, take the exponent as 4, otherwise b % 4.
- Compute power using reduced exponent.
- Return the last digit of the result (% 10).
Why does this work?
- Any number’s last digit depends o (a^b) mod 10. The possible last digits are 0–9. When you keep multiplying, results must eventually repeat (finite possibilities)
- If the base 'a' is coprime with 10 (i.e., last digit is 1, 3, 7, or 9), then (a^4) mod 10 is 1. After every 4 powers, the pattern resets. so last digits repeat every 4.
- If the base is not coprime with 10 (i.e., last digit is 2, 4, 5, 6 or 8), then we can find the pattern case by case basis as there are finite possibilities. For example for 2, 5 or 6, it is always the same digit.
#include <bits/stdc++.h>
using namespace std;
int modulo(int d, string &b)
{
int mod = 0;
// calculate (b mod d) to make
// b in range 0 <= b < d
for (int i = 0; i < b.length(); i++)
mod = (mod * 10 + b[i] - '0') % d;
return mod;
}
int getLastDigit(string &a, string &b)
{
int n = a.length(), m = b.length();
// exponent is 0
if (m == 1 && b[0] == '0')
return 1;
// base is 0
if (n == 1 && a[0] == '0')
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
int exp = (modulo(4, b) == 0) ? 4 : modulo(4, b);
int res = pow(a[n - 1] - '0', exp);
return res % 10;
}
int main()
{
string a = "3", b = "10";
cout << getLastDigit(a, b);
return 0;
}
import java.util.*;
public class GFG {
int modulo(int d, String b)
{
int mod = 0;
// calculate (b mod d) to make
// b in range 0 <= b < d
for (int i = 0; i < b.length(); i++)
mod = (mod * 10 + b.charAt(i) - '0') % d;
return mod;
}
int getLastDigit(String a, String b)
{
int n = a.length(), m = b.length();
// exponent is 0
if (m == 1 && b.charAt(0) == '0')
return 1;
// base is 0
if (n == 1 && a.charAt(0) == '0')
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
int exp = (modulo(4, b) == 0) ? 4 : modulo(4, b);
int res = (int)Math.pow(a.charAt(n - 1) - '0', exp);
return res % 10;
}
public static void main(String[] args)
{
GfG m = new GfG();
String a = "3", b = "10";
System.out.println(m.getLastDigit(a, b));
}
}
def modulo(d, b):
mod = 0
# calculate (b mod d) to make
# b in range 0 <= b < d
for i in range(len(b)):
mod = (mod * 10 + int(b[i])) % d
return mod
def getLastDigit(a, b):
n = len(a)
m = len(b)
# exponent is 0
if m == 1 and b[0] == '0':
return 1
# base is 0
if n == 1 and a[0] == '0':
return 0
# if exponent is divisible by 4 that means last
# digit will be pow(a, 4) % 10
# otherwise last digit will be pow(a, b%4) % 10
exp = 4 if modulo(4, b) == 0 else modulo(4, b)
res = pow(int(a[n - 1]), exp)
return res % 10
if __name__ == "__main__":
a = "3"
b = "10"
print(getLastDigit(a, b))
public class GFG {
public int modulo(int d, string b)
{
int mod = 0;
// calculate (b mod d) to make
// b in range 0 <= b < d
for (int i = 0; i < b.Length; i++)
mod = (mod * 10 + b[i] - '0') % d;
return mod;
}
public int getLastDigit(string a, string b)
{
int n = a.Length, m = b.Length;
// exponent is 0
if (m == 1 && b[0] == '0')
return 1;
// base is 0
if (n == 1 && a[0] == '0')
return 0;
int mod = modulo(4, b);
int exp = (mod == 0) ? 4 : mod;
int res = (int)Math.Pow(a[n - 1] - '0', exp);
return res % 10;
}
public static void Main()
{
string a = "3", b = "10";
Console.WriteLine(getLastDigit(a, b));
}
}
function modulo(d, b)
{
let mod = 0;
// calculate (b mod d) to make
// b in range 0 <= b < d
for (let i = 0; i < b.length; i++) {
mod = (mod * 10 + parseInt(b[i])) % d;
}
return mod;
}
function getLastDigit(a, b)
{
let n = a.length, m = b.length;
// exponent is 0
if (m == 1 && b[0] == "0")
return 1;
// base is 0
if (n == 1 && a[0] == "0")
return 0;
// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
let exp = (modulo(4, b) == 0) ? 4 : modulo(4, b);
let res = Math.pow(parseInt(a[n - 1]), exp);
return Math.floor(res % 10);
}
// Driver code
let a = "3", b = "10";
console.log(getLastDigit(a, b));
Output
9