All possible values of floor(N/K) for all values of K
Given a function f(K) = floor(N/K) (N>0 and K>0), the task is to find all possible values of f(K) for a given N where K takes all values in the range [1, Inf].
Examples:
Input: N = 5
Output: 0 1 2 5
Explanation:
5 divide 1 = 5
5 divide 2 = 2
5 divide 3 = 1
5 divide 4 = 1
5 divide 5 = 1
5 divide 6 = 0
5 divide 7 = 0
So all possible distinct values of f(k) are {0, 1, 2, 5}.
Input: N = 11
Output: 0 1 2 3 5 11
Explanation:
11 divide 1 = 11
11 divide 2 = 5
11 divide 3 = 3
11 divide 4 = 2
11 divide 5 = 2
11 divide 6 = 1
11 divide 7 = 1
…
…
11 divided 11 = 1
11 divides 12 = 0
So all possible distinct values of f(k) are {0, 1, 2, 3, 5, 11}.
Naive Approach:
The simplest approach to iterate over [1, N+1] and store in a set, all values of (N/i) ( 1 ? i ? N + 1) to avoid duplication.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void allQuotients( int N)
{
set< int > s;
for ( int k = 1; k <= N + 1; k++) {
s.insert(N / k);
}
for ( auto it : s)
cout << it << " " ;
}
int main()
{
int N = 5;
allQuotients(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void allQuotients( int N)
{
HashSet<Integer> s = new HashSet<Integer>();
for ( int k = 1 ; k <= N + 1 ; k++)
{
s.add(N / k);
}
for ( int it : s)
System.out.print(it + " " );
}
public static void main(String[] args)
{
int N = 5 ;
allQuotients(N);
}
}
|
Python3
def allQuotients(N):
s = set ()
for k in range ( 1 , N + 2 ):
s.add(N / / k)
for it in s:
print (it, end = ' ' )
if __name__ = = '__main__' :
N = 5
allQuotients(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void allQuotients( int N)
{
SortedSet< int > s = new SortedSet< int >();
for ( int k = 1; k <= N + 1; k++)
{
s.Add(N / k);
}
foreach ( int it in s)
{
Console.Write(it + " " );
}
}
static void Main()
{
int N = 5;
allQuotients(N);
}
}
|
Javascript
<script>
function allQuotients(N)
{
var s = new Set();
for ( var k = 1; k <= N + 1; k++) {
s.add(parseInt(N / k));
}
var ls = Array.from(s).reverse();
ls.forEach(v => document.write(v+ " " ))
}
var N = 5;
allQuotients(N);
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Efficient Approach:
An optimized solution is to iterate over [1, ?N] and insert values K and (N/K) into the set.
C++
#include <bits/stdc++.h>
using namespace std;
void allQuotients( int N)
{
set< int > s;
s.insert(0);
for ( int k = 1; k <= sqrt (N); k++) {
s.insert(k);
s.insert(N / k);
}
for ( auto it : s)
cout << it << " " ;
}
int main()
{
int N = 5;
allQuotients(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void allQuotients( int N)
{
HashSet<Integer> s = new HashSet<Integer>();
s.add( 0 );
for ( int k = 1 ; k <= Math.sqrt(N); k++)
{
s.add(k);
s.add(N / k);
}
for ( int it : s)
System.out.print(it + " " );
}
public static void main(String[] args)
{
int N = 5 ;
allQuotients(N);
}
}
|
Python3
from math import *
def allQuotients(N):
s = set ()
s.add( 0 )
for k in range ( 1 , int (sqrt(N)) + 1 ):
s.add(k)
s.add(N / / k)
for it in s:
print (it, end = ' ' )
if __name__ = = '__main__' :
N = 5
allQuotients(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void allQuotients( int N)
{
SortedSet< int > s = new SortedSet< int >();
s.Add(0);
for ( int k = 1; k <= Math.Sqrt(N); k++)
{
s.Add(k);
s.Add(N / k);
}
foreach ( int it in s)
{
Console.Write(it + " " );
}
}
static void Main()
{
int N = 5;
allQuotients(N);
}
}
|
Javascript
<script>
function allQuotients(N)
{
var s = new Set();
s.add(0);
for ( var k = 1; k <= parseInt(Math.sqrt(N)); k++) {
s.add(k);
s.add(parseInt(N / k));
}
var tmp = [...s];
tmp.sort((a,b)=>a-b)
tmp.forEach(it => {
document.write( it + " " );
});
}
var N = 5;
allQuotients(N);
</script>
|
Time Complexity: O(sqrt(n)*logn)
Auxiliary Space: O(n)
Last Updated :
24 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...