i/p : getting n terms
o/p : see below example
eg:
i/p : 1 2 3 4 5 6 7 8
o/p: 1 3 5 7 2 4 6 8
o/p : see below example
eg:
i/p : 1 2 3 4 5 6 7 8
o/p: 1 3 5 7 2 4 6 8
#include<stdio.h> int main(){ int i,a[30],n,odd=0,even; scanf("%d",&n); //slight neat trick to find where even number start even = (n%2==0)?n/2:n/2+1; for(i=0;i<n;i++){ //loop to seperate the odd index and even index if(i%2==0){ scanf("%d",&a[odd++]); }else{ scanf("%d",&a[even++]); } } for(i=0;i<n;i++)//neat mission accomplished victory printf("%d ",a[i]); return 0; }
On seeing the question, was thinking in the perspective of getting the input, and then processing it as per the need. But your solution does the job as it gets input. Superb implementation! 👌🏻
ReplyDeleteThis way you can save half the space by only storing the numbers in either odd or even places and directly printing the others. :D
ReplyDeleteCould prove useful when the input numbers don't need to be stored.
#include
#include
using std::vector;
using std::cout;
using std::cin;
int main() {
vector array;
bool index = 0;
int n, temp;
cin>>n;
for(int i=0 ; i>temp;
if(index){
array.push_back(temp);
} else {
cout<<temp<<' ';
}
index = !index;
}
for(int i=0; i<array.size() ; i++){
cout<<array.at(i)<<' ';
}
return 0;
}