Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Exponentiation With Modulus Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Exponentiation With Modulus | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given three numbers x, y, and z, Calculate (xy) % z.

Naive Approach

We can multiply x with itself y times, and keep taking modulo at every step. Return the result after that.

Analysis

  • Time Complexity: O(y)
  • Space Complexity: O(1)

Implementation

C++
int getModulatedPower(int x, int y, int z) {
   int value = x;
    for(int i = 1; i <= y; i++) {
       x = (x * value) % z;
   }
   return x;
}
Java
class Solution {
   int getModulatedPower(int x, int y, int z) {
       int value = x;
       for(int i = 1; i <= y; i++) {
           x = (x * value) % z;
       }
       return x;
   }
}

Optimal Approach

We can solve this problem in logarithmic time.

Logic:

  • If y = 0, we return one.
  • Else if y is even, we can write the equation as x(y / 2) * 2.
  • Else if y is odd we can write it as x((y  - 1) / 2) * 2 * x.

We can run this algorithm till it converges into the base case. Since y has at the most floor(log y) + 1 bits, the effective complexity of the algorithm will be logarithmic.

Analysis

  • Time Complexity: O(log y)
  • Space Complexity: O(1)

Implementation

C++
int power(int x, int y, int z) {
   int result = 1;
   x %= z;
   if(x == 0) {
       return 0;
   }
   while(y > 0) {
       if(y % 2 != 0) {
           result = (result * x) % z;
       }
       y /= 2;
       x = (x * x) % z;
   }
   return result;
}
int getModulatedPower(int x, int y, int z) {
   return power(x, y, z);
}
Java
class Solution {
   int power(int x, int y, int z) {
       int result = 1;
       x %= z;
       if(x == 0) {
           return 0;
       }
       while(y > 0) {
           if(y % 2 != 0) {
               result = (result * x) % z;
           }
           y /= 2;
           x = (x * x) % z;
       }
       return result;
   }
   int getModulatedPower(int x, int y, int z) {
       return power(x, y, z);
   }
}
Related Content
Excel Column Number
Greatest Common Divisor
Greatest Common Divisor
Matrix Paths
Primes upto N
Trailing Zeroes
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet