Connell Sequence | Practice | GeeksforGeeks

Connell Sequence is the sequence formed with the first odd number, i.e 1 as its first term. The subsequent terms of the sequence are made up of the first two even numbers, i.e 2 and 4, followed by the next three odd numbers, i.e 5, 7 and 9, followed by the next four even numbers, i.e 10, 12, 14 and 16 and so on .... 
Given an integer n, generate the first n terms of the Connell Sequence.

Example 1:
Input: n = 6
Output: 1 2 4 5 7 9
Explaination: First one odd number. Then 
two even numbers. Then three odd numbers.
 
Example 2:
Input: n = 2
Output: 1 2
Explaination: The sequence consists of first 
odd number then one even number.
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function connellSequence() which takes n as input parmeters and returns a list of integers containing the first n numbers of the Connell sequence.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
 
Constraints:
1 ≤ n ≤ 105  
0
vik10 months ago
vik
import mathclass Solution:    def connellSequence(self, n):        if n == 0:            return 0        else:            arr = []            for i in range(1,n+1):                n = 2 * i - math.floor((1 + math.sqrt(8 * i - 7))/2)                arr.append(n)            return arr
0
Abhay Singh1 year ago
Abhay Singh
what is wrong with this code: #include<iostream>using namespace std;int main() {//code
int t=0;cin>>t;
while(t--){    int n=0;    cin>>n;    int m=0;    int i=1;int s=1;   while(m<=n)   {     for(int k=0;k<s;k++) {="" cout<<i<<"="" ";="" i+="2;" }="" i-="2;" s+="1;" m+="s;" i++;="" }="" cout<<m-s+1<<"="" "<<i;="" int="" h="n-(m-s+1);" while(h--)="" {="" cout<<i<<"="" ";="" i+="2;" }="" cout<<"\n";="" }="" return="" 0;="" }="" i="" m="" getting="" output="" limit="" exceeded="">
0
Geeky Geek2 years ago
Geeky Geek
0.1 sec solution using Vector of sets:Can also be done without storing each element of the sequence with just an equation. If you wanna know, do comment. I used vector of sets just for better presentation./* HINT: The problem is of the form of 1 odd, 2 even, 3 odd, 4 even ...Whatever n is given, we can find a number k such that 1+2+3..upto k is less than or equal to n. If the sum (k*(k+1)/2) is equal to n then there will be k sets of odd and even numbers, or else if (k*(k+1)/2) is lesser, then k+1 sets. Each set will have the same no. of elements as its index.*/#include<bits stdc++.h="">using namespace std;
 int main() {int t; cin>>t; while(t--){     int n;     cin>>n;     vector<set<int> >v;     set<int> s;     set<int> ::iterator it;     int k;    for(int i=1;i<=(n+1)/2;i++){        if((i*(i+1))/2<=n) k=i;        if((i*(i+1))/2>n) break;    }    if((k*(k+1))/2<n) k="k+1;" int="" count="0;" int="" j="0;" for(int="" i="1;i&lt;=k;i++){" int="" p="i;" if(p%2!="0){" s.clear();="" while(p="">=1){              if(count+1>n)               break;              count++;              s.insert(1+j);              j=j+2;              p=p-1;          }               v.push_back(s);        }      else{           s.clear();          while(p>=1){              if(count+1>n)              break;              else count++;              s.insert(j);              j=j+2;              p--;          }j=j-2;            v.push_back(s);}}for(int i=0;i<v.size();i++){ for(it="v[i].begin();it!=v[i].end();++it){" cout<<*it<<"="" ";}}="" cout<<endl;}="" return="" 0;}="">
0
venkat2 years ago
venkat
for t in range(int(input())):    n=int(input())    import math    for i in range(1,n+1):        x=2*i-math.floor((1+math.sqrt(8*i-7))/2)        print(x,end=" ")what's wrong in this code.can anyone please help
0
Abhishek Pandey2 years ago
Abhishek Pandey
#include<iostream>using namespace std;int main() {int t;cin>>t;while(t--){    int n;    cin>>n;  int c=0,j=0,m=0;  for(int i=1;i<=n;i++)  {      cout<<i+c<<" ";="" m++;="" if(c<j)="" {="" c++;="" continue;="" }="" else="" if(c="=j)" {="" c="j;" j="c+m;" m="0;" }="" }="" cout<<endl;="" }="" return="" 0;="" }="">
0
Marius Dragoi4 years ago
Marius Dragoi
When I run test case 100000 as "custom input" it works fine, but when I push SUBMIT I do not receive any answer:
Wrong Answer. !!!Wrong AnswerPossibly your code doesn't work correctly for multiple test-cases (TCs).The first test case where your code failed:
Input:100000
Its Correct output is:1 2 4 5 7 9 10 12 ........And Your Code's output is:Need Help ?
0
Mayank Bhandari4 years ago
Mayank Bhandari
https://ide.geeksforgeeks.o...
segmentation fault error. can anyone help?
0
Balaji Ijjapwar4 years ago
Balaji Ijjapwar
when i run the code with any custom input it gives correct answer ,but when i submitted the code it says wrong answer for input 6 expected output:1 2 4 5 7 11your output:1 2 4 5
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.