Interlace odd / even from A to B - Spicy Coders

Recent

Sunday, October 08, 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: 
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 

Example Input/Output 2: 
Input: 
14 

Output: 
14 5 12 7 10 9 8 11 6 13 4 

Explanation: The odd numbers from 4 to 14 are 5 7 9 11 13 The even numbers from 14 to 4 (that is in reverse direction) are 14 12 10 8 6 4 So these numbers are interlaced to produce 14 5 12 7 10 9 8 11 6 13 4 (Here as the even numbers count are more than the odd numbers count we start with the even number in the output) 

Example Input/Output 3: 
Input: 
12 

Output: 
3 12 5 10 7 8 9 6 11 4 

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

Source Code:

#include<stdio.h>
int main()
{
int i,n,m,j;
scanf("%d%d",&n,&m);
for(i=n,j=m;i<=m;i++,j--)
{
    if(i%2==1)
    printf("%d\t",i);
    if(j%2==0)
    printf("%d\t",j);
}


}

9 comments:

  1. Replies
    1. Are you an ALIEN!!!!!
      if yes give your mobile no,i will give you program

      Delete
  2. Kavin kumar everything has limit.Dont cross your limit.Do your program by own.dont search in net

    ReplyDelete
  3. dont copy u r name from a cricket player

    ReplyDelete
  4. a,b=map(int,input().split())
    arr=[i for i in range(a,b+1)]
    l=[i for i in range(a,b+1) if(i%2==0)]
    k=sorted(l,reverse=True)
    for i in range(1,len(arr)-len(l)):
    arr[2*i-1]=k[i-1]
    print(arr)

    ReplyDelete
    Replies
    1. Will you please explain the logic behind this....i can't able to understand in python...plzz

      Delete
  5. How to do the same program in python 3

    ReplyDelete