Tuesday, October 10, 2017

Interlace odd-even from A to B

Two numbers A and B are passed as input. The program must print the odd numbers from A to B (inclusive of A and B) interlaced with the even numbers from B to A.

Input Format:
The first line denotes the value of A. The second line denotes the value of B.

Output Format:
The odd and even numbers interlaced, each separated by a space.

Boundary Conditions: 1 <= A <= 9999999 A < B <= 9999999

Example Input/Output 1:
Input:
5
11

Output:
5 10 7 8 9 6 11

Explanation:
The odd numbers from 5 to 11 are 5 7 9 11 The even numbers from 11 to 5 (that is in reverse direction) are 10 8 6 So these numbers are interlaced to produce 5 10 7 8 9 6 11

Solution:
#include <iostream>
using namespace std;
int main()
{
 int n,m,i,j;
 cin>>n>>m;
 for(i=n,j=m;i<=m;i++,j--)
  {
      if(i%2==1)
       cout<<i<<" ";
      if(j%2==0)
       cout<<j<<" ";
  }
}
Share: