GFG Find Last Digit Of a^b for Large Numbers Problem

Problem link:
https://practice.geeksforgeeks.org/problems/find-last-digit-of-ab-for-large-numbers1936/1

My code :


Can u plz help I am getting TLE

hello @joshisanjay8467

a) see they are saying that numbers can be large that means u cant store it in primitive data type like int or long long.
so ur logic will work only for stirng length <=18 ( long long can hold atmax 18 digits)

b) note all they are asking is last digit . so can u find any pattern like.
if last digit of a is 1 then no matter what b is it last digit of final answer will remain 1.
if last digit of a is 2 then last digit of final answer can be 2 or 4 6 8
similarly for 3 last digit of final answer can be 3 , 9 , 7 ,1

write first 10 powers of all digits [0…9 ] and observe the pattern.

1 Like

thankyou Aman bhaiya !! SOLVED