Largest set of numbers upto N such that either i or i/2 is present
Last Updated :
18 Jan, 2022
Given a positive integer N, the task is to find the length of largest set that can be generated such that if i is present, then i/2 will not be present and 1<=i<=N.
Note: For multiple solutions, print anyone satisfying the condition.
Examples:
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.
Approach: Create a map for easy storing and searching and a vector to store the final set. Run a loop from 1 to N (say i) If i is odd then add i to the answer vector and as a key in the map. Else if i is even, search i/2 as key in the map. If i/2 is absent in the map then add i to the answer vector and as a key in the map. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
vector< int > reqsubarray( int & n)
{
unordered_map< int , bool > mp;
vector< int > ans;
int i;
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 ans;
}
int main()
{
int n = 10, i;
vector< int > ans;
ans = reqsubarray(n);
for (i = 0; i < ans.size(); i++)
cout << ans[i] << " " ;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.HashMap;
class GFG
{
static ArrayList<Integer> reqsubarray( int n)
{
HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
ArrayList<Integer> ans = new ArrayList<Integer>();
int i;
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 ans;
}
public static void main(String args[])
{
int n = 10 , i;
ArrayList<Integer> ans = reqsubarray(n);
for (i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i) + " " );
}
}
|
Python3
def reqsubarray(n):
mp = {}
ans = []
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 ans
if __name__ = = "__main__" :
n = 10
ans = reqsubarray(n)
for i in range ( len (ans)):
print (ans[i], end = " " )
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static ArrayList reqsubarray( int n)
{
Dictionary< int , int > mp =
new Dictionary< int , int >();
ArrayList ans = new ArrayList();
int i;
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 ans;
}
public static void Main()
{
int n = 10, i;
ArrayList ans = reqsubarray(n);
for (i = 0; i < ans.Count; i++)
Console.Write(ans[i] + " " );
}
}
|
Javascript
<script>
function reqsubarray(n) {
let mp = new Map();
let ans = [];
let i;
for (i = 1; i <= n; i++) {
if (i & 1) {
ans.push(i);
mp.set(i, 1);
}
else if (!mp.has(Math.floor(i / 2))) {
ans.push(i);
mp.set(i, 1);
}
}
return ans;
}
let n = 10;
let ans;
ans = reqsubarray(n);
for (let i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...