Max occurring divisor in an interval
Given an interval [x, y]. Find the divisor that occurs maximum number of times except ‘1’ in the divisors of numbers in the range [x, y], both inclusive.
Examples :
Input : [2, 5] Output : 2 Explanation : Divisors of 2, 3, 4, 5 except 1 are: 2 -> 2 3 -> 3 4 -> 2, 4 5 -> 5 Among all the divisors 2 occurs maximum time, i.e two times. Input : [3, 3] Output : 3
Time Complexity: O(n*sqrt(n)), where n is total number of numbers between interval [x, y].
Efficient Approach: An efficient approach will be to observe carefully that every even number will have 2 as a divisor. And in the interval [x, y] there is atleast floor((y-x)/2) + 1 numbers of 2. That is, half of the total numbers in the interval will have divisor as 2. Clearly this is the maximum number of occurrences of a divisor in the interval. If (x == y), then answer will be x or y.
Below is implementation of above idea:
C++
// Efficient C++ program to // find maximum occurring // factor in an interval #include <iostream> using namespace std; // function to find max // occurring divisor // interval [x, y] int findDivisor( int x, int y) { // if there is only // one number in the // in the interval, // return that number if (x == y) return y; // otherwise, 2 is the // max occurring // divisor return 2; } // Driver code int main() { int x = 3, y = 16; cout << findDivisor(x, y); return 0; } |
Java
// Efficient Java program to // find maximum occurring // factor in an interval import java.io.*; class GFG { // function to find max // occurring divisor // interval [x, y] static int findDivisor( int x, int y) { // if there is only // one number in the // in the interval, // return that number if (x == y) return y; // otherwise, 2 is the max // occurring divisor return 2 ; } /* Driver program */ public static void main(String[] args) { int x = 3 , y = 16 ; System.out.println(findDivisor(x, y)); } } // This code is contributed by Gitanjali. |
Python3
# Efficient python 3 program # to find maximum occurring # factor in an interval # function to find max # occurring divisor # interval [x, y] def findDivisor(x, y): # if there is only # one number in the # in the interval, # return that number if (x = = y): return y # otherwise, 2 is # max occurring # divisor return 2 # Driver code x = 3 y = 16 print (findDivisor(x, y)) # This code is contributed by # Smitha Dinesh Semwal |
C#
// Efficient C# program to // find maximum occurring // factor in an interval using System; class GFG { // function to find max // occurring divisor // interval [x, y] static int findDivisor( int x, int y) { // if there is only // one number in the // in the interval, // return that number if (x == y) return y; // otherwise, 2 is the max // occurring divisor return 2; } // Driver Code public static void Main() { int x = 3, y = 16; Console.Write(findDivisor(x, y)); } } // This code is contributed by nitin mittal. |
PHP
<?php // Efficient PHP program to // find maximum occurring // factor in an interval // function to find max // occurring divisor // interval [x, y] function findDivisor( $x , $y ) { // if there is only // one number in the // in the interval, // return that number if ( $x == $y ) return $y ; // otherwise, 2 is the // max occurring // divisor return 2; } // Driver code $x = 3; $y = 16; echo findDivisor( $x , $y ); // This code is contributed by mits. ?> |
Javascript
<script> // Efficient javascript program to // find maximum occurring // factor in an interval // function to find max // occurring divisor // interval [x, y] function findDivisor(x , y) { // if there is only // one number in the // in the interval, // return that number if (x == y) return y; // otherwise, 2 is the max // occurring divisor return 2; } /* Driver program */ var x = 3, y = 16; document.write(findDivisor(x, y)); // This code is contributed by 29AjayKumar </script> |
Output:
2
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...