到 N 的最大数字集,使得 i 或 i/2 存在
给定一个正整数N ,任务是找到可以生成的最大集合的长度,使得如果i存在,则i/2将不存在并且1<=i<=N 。
Note: For multiple solutions, print anyone satisfying the condition.
例子:
Input: N = 2
Output: 1
Explanation: There are two possible values 1 and 2. If 2 is present, 1 can not be present since 2/2=1. So only possibilities are 1 or 2. Both 1 and 2 separately can be the answers.
Input: N = 10
Output: 1 3 4 5 7 9
Explanation: In the output, there are no i for which i/2 exists.
方法:创建一个地图以便于存储和搜索,并创建一个向量来存储最终集。运行从1到N的循环(比如i )如果i是奇数,则将i添加到答案向量并作为地图中的键。否则,如果i是偶数,则在地图中搜索i/2作为键。如果地图中不存在i/2 ,则将i添加到答案向量并作为地图中的键。请按照以下步骤解决问题:
- 初始化 map
mp[]。 - 初始化 vector
ans[]以存储结果。 - 使用变量i遍历范围[, N]并执行以下任务:
- 如果i是奇数,则将i推入向量ans[]并将{i, 1}插入映射mp[]。
- 否则,如果映射mp[]中不存在i/2 ,则将i推入向量ans[]并将{i, 1}插入映射mp[]。
- 执行上述步骤后,打印向量ans[]的值作为答案。
下面是上述方法的实现。
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to get the largest set
vector reqsubarray(int& n)
{
// Initialize the map
unordered_map mp;
// Vector to store the answer
vector ans;
int i;
// Traverse the range
for (i = 1; i <= n; i++) {
if (i & 1) {
ans.push_back(i);
mp.insert({ i, 1 });
}
else if (!mp[i/2]) {
ans.push_back(i);
mp.insert({ i, 1 });
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
int n = 10, i;
vector ans;
ans = reqsubarray(n);
for (i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.HashMap;
class GFG
{
// Function to get the largest set
static ArrayList reqsubarray(int n)
{
// Initialize the map
HashMap mp = new HashMap();
// Vector to store the answer
ArrayList ans = new ArrayList();
int i;
// Traverse the range
for (i = 1; i <= n; i++) {
if ((i & 1) == 1) {
ans.add(i);
mp.put(i, 1);
}
else if (!mp.containsKey(i / 2)) {
ans.add(i);
mp.put(i, 1);
}
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String args[])
{
int n = 10, i;
ArrayList ans = reqsubarray(n);
for (i = 0; i < ans.size(); i++)
System.out.print(ans.get(i) + " ");
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python 3 program for the above approach
# Function to get the largest set
def reqsubarray(n):
# Initialize the map
mp = {}
# Vector to store the answer
ans = []
# Traverse the range
for i in range(1, n + 1):
if (i & 1):
ans.append(i)
mp[i]= 1
elif ((i // 2) not in mp):
ans.append(i)
mp[i] = 1
# Return the answer
return ans
# Driver Code
if __name__ == "__main__":
n = 10
ans = reqsubarray(n)
for i in range(len(ans)):
print(ans[i], end=" ")
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to get the largest set
static ArrayList reqsubarray(int n)
{
// Initialize the map
Dictionary mp =
new Dictionary();
// Vector to store the answer
ArrayList ans = new ArrayList();
int i;
// Traverse the range
for (i = 1; i <= n; i++) {
if ((i & 1) == 1) {
ans.Add(i);
mp.Add(i, 1);
}
else if (!mp.ContainsKey(i / 2)) {
ans.Add(i);
mp.Add(i, 1);
}
}
// Return the answer
return ans;
}
// Driver Code
public static void Main()
{
int n = 10, i;
ArrayList ans = reqsubarray(n);
for (i = 0; i < ans.Count; i++)
Console.Write(ans[i] + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
1 3 4 5 7 9
时间复杂度: O(N)
辅助空间: O(N)