Find largest factor of N such that N / F is less than K | C ++ program for the above approach ; Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function Call
#include <iostream> NEW_LINE using namespace std ; int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = max ( ans , j ) ; } if ( n / j <= k ) { ans = max ( ans , n / j ) ; } } } return ans ; } int main ( ) { int N = 8 , K = 7 ; cout << ( N / solve ( N , K ) ) ; return 0 ; }