Highest power of a number that divides other number
Given two numbers N and M, the task is to find the highest power of M that divides N.
Note: M > 1
Examples:
Input: N = 48, M = 4
Output: 2
48 % (4^2) = 0
Input: N = 32, M = 20
Output: 0
32 % (20^0) = 0
Brute Force Approach: The idea to solve this problem is to iterate from 1 to m and for each number, check if it is a factor of both n and m. If it is a factor of both, then divide n and m by the number until it’s no longer divisible by it. Keep a count of how many times the number is divided by both n and m and take the minimum count as the answer for that number. Finally, return the maximum count obtained from all the numbers.
- Define a function named getMaximumPower() that takes two integers n and m as inputs and returns an integer value.
- Initialize a variable named maxi to 0, which will store the maximum power of a number that divides both n and m.
- Iterate from 2 to m using a for loop. For each value of i, check if i is a common factor of both n and m. If not, continue with the next iteration.
- If i is a common factor of n and m, initialize two variables cnt1 and cnt2 to 0.
- While n is divisible by i, divide n by i and increment cnt1.
- While m is divisible by i, divide m by i and increment cnt2.
- If cnt2 is greater than cnt1, the power of i that divides n and m is 0. Continue with the next iteration of the for loop.
- Otherwise, calculate the power of i that divides n and m as cnt1 / cnt2 and update maxi if it is greater than the current value.
- After the loop completes, return maxi as the maximum power of a number that divides both n and m.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return the highest power int getMaximumPower( int n, int m) { int maxi = 0; // Iterate from 2 to m to check the factors for ( int i = 2; i <= m; i++) { // If i is a common factor of n and m if (n % i == 0 && m % i == 0) { int cnt1 = 0, cnt2 = 0; // Count the number of times i divides n while (n % i == 0) { n /= i; cnt1++; } // Count the number of times i divides m while (m % i == 0) { m /= i; cnt2++; } // Find the maximum power of i maxi = max(maxi, cnt1 / cnt2); } } // Return the maximum power return maxi; } // Drivers code int main() { int n = 48, m = 4; cout << getMaximumPower(n, m); return 0; } |
2
Time Complexity: O(sqrt(n) + sqrt(m))
Auxiliary Space: O(max(n, m))
Approach: Initially prime factorize both the numbers N and M and store the count of prime factors in freq1[] and freq2[] respectively for N and M. For every prime factor of M, check if its freq2[num] is greater than freq1[num] or not. If it is for any prime factor of M, then max power will be 0. Else the maximum power will be the minimum of all freq1[num] / freq2[num] for every prime factor of M.
For a number N = 24, the prime factors will 2^3 * 3^1. Hence freq1[2] = 3 and freq1[3] = 1.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to get the prime factors // and its count of times it divides void primeFactors( int n, int freq[]) { int cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( int i = 3; i <= sqrt (n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1; } // Function to return the highest power int getMaximumPower( int n, int m) { // Initialize two arrays int freq1[n + 1], freq2[m + 1]; memset (freq1, 0, sizeof freq1); memset (freq2, 0, sizeof freq2); // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0; // Iterate and find the maximum power for ( int i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue ; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i]) { // get the maximum power maxi = max(maxi, freq1[i] / freq2[i]); } } return maxi; } // Drivers code int main() { int n = 48, m = 4; cout << getMaximumPower(n, m); return 0; } |
Java
// Java program to implement // the above approach class GFG { // Function to get the prime factors // and its count of times it divides static void primeFactors( int n, int freq[]) { int cnt = 0 ; // Count the number of 2s that divide n while (n % 2 == 0 ) { cnt++; n = n / 2 ; } freq[ 2 ] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( int i = 3 ; i <= Math.sqrt(n); i = i + 2 ) { cnt = 0 ; // While i divides n, count i and divide n while (n % i == 0 ) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2 ) freq[n] = 1 ; } // Function to return the highest power static int getMaximumPower( int n, int m) { // Initialize two arrays int freq1[] = new int [n + 1 ], freq2[] = new int [m + 1 ]; // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0 ; // Iterate and find the maximum power for ( int i = 2 ; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0 ) continue ; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0 ; // If i is a prime factor of M if (freq2[i] != 0 ) { // get the maximum power maxi = Math.max(maxi, freq1[i] / freq2[i]); } } return maxi; } // Drivers code public static void main(String[] args) { int n = 48 , m = 4 ; System.out.println(getMaximumPower(n, m)); } } // This code contributed by Rajput-Ji |
Python 3
import math # Python program to implement # the above approach # Function to get the prime factors # and its count of times it divides def primeFactors(n, freq): cnt = 0 # Count the number of 2s that divide n while n % 2 = = 0 : cnt = cnt + 1 n = int (n / / 2 ) freq[ 2 ] = cnt # n must be odd at this point. So we can skip # one element (Note i = i+2) i = 3 while i< = math.sqrt(n): cnt = 0 # While i divides n, count i and divide n while (n % i = = 0 ): cnt = cnt + 1 n = int (n / / i) freq[ int (i)] = cnt i = i + 2 # This condition is to handle the case when n # is a prime number greater than 2 if (n > 2 ): freq[ int (n)] = 1 # Function to return the highest power def getMaximumPower(n, m): # Initialize two arrays freq1 = [ 0 ] * (n + 1 ) freq2 = [ 0 ] * (m + 1 ) # Get the prime factors of n and m primeFactors(n, freq1) primeFactors(m, freq2) maxi = 0 # Iterate and find the maximum power i = 2 while i < = m: # If i not a prime factor of n and m if (freq1[i] = = 0 and freq2[i] = = 0 ): i = i + 1 continue # If i is a prime factor of n and m # If count of i dividing m is more # than i dividing n, then power will be 0 if (freq2[i] > freq1[i]): return 0 # If i is a prime factor of M if (freq2[i]): # get the maximum power maxi = max (maxi, int (freq1[i] / / freq2[i])) i = i + 1 return maxi # Drivers code n = 48 m = 4 print (getMaximumPower(n, m)) # This code is contributed by Shashank_Sharma |
C#
// C# program to implement // the above approach using System; class GFG { // Function to get the prime factors // and its count of times it divides static void primeFactors( int n, int []freq) { int cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( int i = 3; i <= Math.Sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1; } // Function to return the highest power static int getMaximumPower( int n, int m) { // Initialize two arrays int []freq1 = new int [n + 1]; int []freq2 = new int [m + 1]; // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0; // Iterate and find the maximum power for ( int i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue ; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i] != 0) { // get the maximum power maxi = Math.Max(maxi, freq1[i] / freq2[i]); } } return maxi; } // Drivers code public static void Main(String[] args) { int n = 48, m = 4; Console.WriteLine(getMaximumPower(n, m)); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP program to implement // the above approach // Function to get the prime factors // and its count of times it divides function primeFactors( $n , $freq ) { $cnt = 0; // Count the number of 2s that divide n while ( $n % 2 == 0) { $cnt ++; $n = floor ( $n / 2); } $freq [2] = $cnt ; // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2) { $cnt = 0; // While i divides n, count i and divide n while ( $n % $i == 0) { $cnt ++; $n = floor ( $n / $i ); } $freq [ $i ] = $cnt ; } // This condition is to handle the case when n // is a prime number greater than 2 if ( $n > 2) $freq [ $n ] = 1; return $freq ; } // Function to return the highest power function getMaximumPower( $n , $m ) { $freq1 = array_fill (0, $n + 1,0); $freq2 = array_fill (0, $m + 1,0); // Get the prime factors of n and m $freq1 = primeFactors( $n , $freq1 ); $freq2 = primeFactors( $m , $freq2 ); $maxi = 0; // Iterate and find the maximum power for ( $i = 2; $i <= $m ; $i ++) { // If i not a prime factor of n and m if ( $freq1 [ $i ] == 0 && $freq2 [ $i ] == 0) continue ; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if ( $freq2 [ $i ] > $freq1 [ $i ]) return 0; // If i is a prime factor of M if ( $freq2 [ $i ]) { // get the maximum power $maxi = max( $maxi , floor ( $freq1 [ $i ] / $freq2 [ $i ])); } } return $maxi ; } // Drivers code $n = 48; $m = 4; echo getMaximumPower( $n , $m ); // This code is contributed by Ryuga ?> |
Javascript
<script> // Javascript program to implement // the above approach // Function to get the prime factors // and its count of times it divides function primeFactors(n, freq) { var cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; var i; // n must be odd at this point. So we can skip // one element (Note i = i +2) for (i = 3; i <= Math.sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1; } // Function to return the highest power function getMaximumPower(n, m) { // Initialize two arrays var freq1 = new Array(n+1); var freq2 = new Array(m+1); // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); var maxi = 0; // Iterate and find the maximum power for (i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue ; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i]) { // get the maximum power maxi = Math.max(maxi, freq1[i]/freq2[i]); } } return maxi; } // Drivers code var n = 48, m = 4; document.write(getMaximumPower(n, m)); </script> |
2
Time Complexity: O(n log log n)
Auxiliary Space: O(max(m,n))
Please Login to comment...