Monday, November 27, 2017

Pattern - Two Integers X & Y

Given two positive integers value X & Y as input, print the pattern as in the Example Input/Output section.

Input Format:
The first line contains X and Y separated by a space.

Output Format:
The pattern as described in the Example Input/Output section.

Boundary Conditions: 2 <= X, Y <= 50

Example Input/Output 1:
Input:
10 20
Output:
11 19 12 18 13 17 14 16 15

Example Input/Output 2:
Input:
9 22
Output:
10 21 11 20 12 19 13 18 14 17 15 16

Solution 1:
#include <iostream>
using namespace std;
int main()
{
 int x,y;
 cin>>x>>y;
 while(++x<=--y)
  x!=y?cout<<x<<" "<<y<<" ":cout<<x<<" ";
}

Solution 2:
x,y=[int(i) for i in input().split()]
x,y=x+1,y-1
while x<=y:
    print(x,y,end=' ') if x!=y else print(x,end=' ')
    x,y=x+1,y-1
Share: