Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Super Pow problem solution

YASH PAL, 31 July 2024

In this Leetcode Super Pow problem solution, Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Leetcode Super Pow problem solution

Problem solution in Python.

class Solution:
    def superPow(self, a: int, b: List[int]) -> int:
        return pow(a,(int("".join([str(i) for i in b]))),1337)

Problem solution in Java.

public class Solution {
    public int superPow(int a, int[] b) {
        if(b==null || b.length==0) return 0;
        
        int len = b.length;

        int[] remainders = new int[len];
        int first = a % 1337;
        for(int i=len-1; i>=0; i--) {
            int[] nums = new int[11];
            nums[0] = 1;
            for(int j=1; j<11; j++) {
                nums[j] = (nums[j-1]*first) % 1337;
            }
            
            remainders[i] = nums[b[i]];
            first = nums[10];
        }
        int remainder = 1;
        for(int i=0; i<len; i++) {
            remainder = (remainder*remainders[i]) % 1337;
        }
        
        return remainder;
    }
}

Problem solution in C++.

int superPow(int a, vector<int>& b) {
        int result = 1;
        a %= 1337;
        for(int i = 0;i<b.size();i++){
            for(int j = 0;j<b[b.size()-i-1];j++){
                result=(result*a)%1337;
            }
            int a2 = (a*a)%1337;
            int a4 = (a2*a2)%1337;
            int a8 = (a4*a4)%1337; 
            a = (a8*a2)%1337;
        }
        return result;
    }

Problem solution in C.

int func(int a,int b){
    int ret=1;
    for(int i=0;i<b;i++){
        ret=ret*a%1337;
    }
    return ret;
}
int myPow(int a,int b,int zeroNum){
    int ret=func(a,b);
    for(int i=0;i<zeroNum;i++){
        ret=func(ret,10);
    }
    return ret;
}
int superPow(int a, int* b, int bSize){
    int ret=1;
    a%=1337;
    for(int i=0;i<bSize;i++){
        ret=ret*myPow(a,b[i],bSize-i-1)%1337;
    }
    return ret;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes