给定一个函数f(K)= floor(N / K) ( N> 0和K> 0 ),任务是找到给定N的f(K)的所有可能值,其中K取[ 1,Inf] 。
例子:
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}.
天真的方法:
迭代[1,N + 1]并将其存储在集合中的最简单方法是(N / i) (1?i?N +1)的所有值以避免重复。
下面是上述方法的实现:
C++
// C++ Program for the
// above approach
#include
using namespace std;
// Function to print all
// possible values of
// floor(N/K)
void allQuotients(int N)
{
set s;
// loop from 1 to N+1
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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print all
// possible values of
// Math.floor(N/K)
static void allQuotients(int N)
{
HashSet s = new HashSet();
// loop from 1 to N+1
for(int k = 1; k <= N + 1; k++)
{
s.add(N / k);
}
for(int it : s)
System.out.print(it + " ");
}
// Driver code
public static void main(String[] args)
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to print all possible
# values of floor(N/K)
def allQuotients(N):
s = set()
# Iterate from 1 to N+1
for k in range(1, N + 2):
s.add(N // k)
for it in s:
print(it, end = ' ')
# Driver code
if __name__ == '__main__':
N = 5
allQuotients(N)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all possible
// values of Math.floor(N/K)
static void allQuotients(int N)
{
SortedSet s = new SortedSet();
// Loop from 1 to N+1
for(int k = 1; k <= N + 1; k++)
{
s.Add(N / k);
}
foreach(int it in s)
{
Console.Write(it + " ");
}
}
// Driver code
static void Main()
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by divyeshrabadiya07
C++
// C++ Program for the
// above approach
#include
using namespace std;
// Function to print all
// possible values of
// floor(N/K)
void allQuotients(int N)
{
set 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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print all
// possible values of
// Math.floor(N/K)
static void allQuotients(int N)
{
HashSet s = new HashSet();
s.add(0);
// loop from 1 to N+1
for(int k = 1; k <= Math.sqrt(N); k++)
{
s.add(k);
s.add(N / k);
}
for(int it : s)
System.out.print(it + " ");
}
// Driver code
public static void main(String[] args)
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by rock_cool
Python3
# Python3 program for the above approach
from math import *
# Function to print all possible
# values of floor(N/K)
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 = ' ')
# Driver code
if __name__ == '__main__':
N = 5
allQuotients(N)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all possible
// values of Math.floor(N/K)
static void allQuotients(int N)
{
SortedSet s = new SortedSet();
s.Add(0);
// loop from 1 to N+1
for(int k = 1; k <= Math.Sqrt(N); k++)
{
s.Add(k);
s.Add(N / k);
}
foreach(int it in s)
{
Console.Write(it + " ");
}
}
// Driver code
static void Main()
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by divyeshrabadiya07
0 1 2 5
时间复杂度: O(N)
辅助空间: O(N)
高效方法:
一种优化的解决方案是遍历[1,?N]并将值K和(N / K)插入到集合中。
C++
// C++ Program for the
// above approach
#include
using namespace std;
// Function to print all
// possible values of
// floor(N/K)
void allQuotients(int N)
{
set 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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print all
// possible values of
// Math.floor(N/K)
static void allQuotients(int N)
{
HashSet s = new HashSet();
s.add(0);
// loop from 1 to N+1
for(int k = 1; k <= Math.sqrt(N); k++)
{
s.add(k);
s.add(N / k);
}
for(int it : s)
System.out.print(it + " ");
}
// Driver code
public static void main(String[] args)
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by rock_cool
Python3
# Python3 program for the above approach
from math import *
# Function to print all possible
# values of floor(N/K)
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 = ' ')
# Driver code
if __name__ == '__main__':
N = 5
allQuotients(N)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all possible
// values of Math.floor(N/K)
static void allQuotients(int N)
{
SortedSet s = new SortedSet();
s.Add(0);
// loop from 1 to N+1
for(int k = 1; k <= Math.Sqrt(N); k++)
{
s.Add(k);
s.Add(N / k);
}
foreach(int it in s)
{
Console.Write(it + " ");
}
}
// Driver code
static void Main()
{
int N = 5;
allQuotients(N);
}
}
// This code is contributed by divyeshrabadiya07
0 1 2 5
时间复杂度: O(?N)
辅助空间: O(N)