Thursday, October 19, 2017

Immediate Previous Larger Number

N numbers are passed as input to the program. The program must print the immediate previous larger number. If there is no such larger number print 0 for that specific number. Note: As N can be as high as 100000, optimize your algorithm to avoid timeout.

Input Format:
The first line contains N. The second line contains N numbers separated by a space.

Output Format:
The first line contains N numbers which denote the immediate previous larger number.

Boundary Conditions: 2 <= N <= 100000

Example Input/Output 1:
Input:
11
455 346 76 304 488 374 385 433 350 9 1000
Output:
0 455 346 346 0 488 488 488 433 350 0

Solution 1:
n= int(input())
a,s=[int(i) for i in input().split()],[]
for i in range(n):
    while len(s)!=0 and s[-1]<=a[i]:
        s.pop()
    if len(s)!=0:
        print(s[-1],end=' ')
    else:
        print('0',end=' ')
    s.append(a[i])

Solution 2:
#include <iostream>
#include <stack>
using namespace std;
int main(){
 int i,a,n;
 stack<int> s;
 cin>>n;
 for(i=0;i<n;i++){
      cin>>a;
      while(!s.empty()&&s.top()<=a)
      s.pop();
      if(!s.empty())
       cout<<s.top();
      else
       cout<<"0";
      s.push(a);
      if(i<n-1)
       cout<<" ";
  }
}


Share:

Pattern Printing - Half Pyramid Numbers

The number of rows N is passed as the input. The program must print the half pyramid using the numbers from 1 to N.

Input Format:
The first line contains N.

Output Format:
N lines representing the half pyramid pattern using the numbers from 1 to N. (A single space is used to separate the numbers)

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
5
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 
Solution:
n=int(input())
for i in range(1,n+1):
    print(*(j for j in range(1,i+1)))
Share:

Punctuality Incentive

The CEO of company ABC Inc wanted to encourage the employees coming on time to the office. So, he announced that for every consecutive day an employee comes on time in a week (starting from Monday to Saturday), he will be awarded Rs.200 more than the previous day as "Punctuality Incentive". The incentive I for the starting day (i.e. on Monday) is passed as the input to the program. The number of days N an employee came on time consecutively starting from Monday is also passed as the input. The program must calculate and print the "Punctuality Incentive" P of the employee.

Input Format:
The first line denotes the value of I. The second line denotes the value of N.

Output Format:
The first line denotes the value of P.

Example Input/Output:
Input:
500
3
Output:
2100
Explanation:
On Monday the employee receives Rs.500, on Tuesday Rs.700, on Wednesday Rs.900 So total = Rs.2100

Solution 1:
x,y=int(input()),int(input())
print("%0.0f"%((x*y)+(200*((y-1)*(y)/2))))

Solution 2:
#include <iostream>
using namespace std;
int main()
{
   int x,y;
   cin>>x>>y;
   cout<<((x*y)+(200*((y-1)*(y)/2)));
}
Share:

Saturday, October 14, 2017

Meeting Late Comers

A certain number of people attended a meeting which was to begin at 10:00 am on a given day. The arrival time in HH:MM format of those who attended the meeting is passed as the input in a single line, with each arrival time by a space. The program must print the count of people who came late (after 10:00 am) to the meeting.

Input Format:
The first line contains the arrival time separated by a space.

Output Format:
The first line contains the count of late comers.

Boundary Conditions: The length of the input string is between 4 to 10000. The time HH:MM will be in 24 hour format (HH is hours and MM is minutes).

Example Input/Output 1:
Input:
10:00 9:55 10:02 9:45 11:00
Output:
 2
Explanation:
The 2 people were those who came at 10:02 and 11:00

Solution 1:
#include<stdio.h>
int main()
{
 int x,y,s=0;
 char c=' ';
 while(c==' ')
 {
     scanf("%d:%d%c",&x,&y,&c);
     if(x>10||x==10&&y>0)
     s++;
 }
printf("%d",s);
}

Solution 2:
#include <cstdio>
using namespace std;
int main()
{
 int x,y,s=0;
 char c=' ';
 while(c==' ')
 {
     scanf("%d:%d%c",&x,&y,&c);
     if(x>10||x==10&&y>0)
      s++;
 }
 printf("%d",s);
}

Solution 3:
s,c=[i.split(':') for i in input().split()],0
for i in s:
    if int(i[0])>10 or int(i[0])==10 and int(i[1])>0:
        c+=1
print(c)
Share:

Thursday, October 12, 2017

Digit In Tenth Position

The program must accept a positive integer N and print the digit in the tenth position.

Input Format:
The first line denotes the value of N.

Output Format:
The first line contains the value of N.

Boundary Conditions: 10 <= N <= 9999999

Example Input/Output 1:
Input:
20
Output:
2

Example Input/Output 2:
Input:
37843
Output:
4

Solution 1:
print(int(int(input())/10)%10)

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int n;
 cin>>n;
 cout<<(n/10)%10;
}
Share:

Wednesday, October 11, 2017

First Five Values Divisible

Three numbers A, B and C are passed as input. The program must print the first five values that are divisible by A, B and C.

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

Output Format:
The first line contains the first five numbers divisible by A, B and C (with each value separated by a space).

Boundary Conditions: 1 <= A <= 9999 1 <= B <= 9999 1 <= C <= 9999

Example Input/Output 1:
Input:
2
3
4
Output:
12 24 36 48 60

Example Input/Output 2:
Input:
4
6
8
Output:
24 48 72 96 120

Solution 1:
import fractions
x,y,z=int(input()),int(input()),int(input())
l=int(x*y/fractions.gcd(x,y))
l=int(l*z/fractions.gcd(l,z))
for i in range(1,6):
    print(i*l,end=' ')

Solution 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int i,x,y,z,l;
 cin>>x>>y>>z;
 l=x*y/__gcd(x,y);
 l=l*z/__gcd(l,z);
 for(i=1;i<6;i++)
 cout<<i*l<<" ";
}
Share:

Tuesday, October 10, 2017

Toggle Alphabet Case

The program must accept an alphabet X and toggle the case (convert upper case to lower case and vice versa).

Input Format:
The first line denotes the value of character X.

Output Format:
The first line contains the characters X and the toggled case character separated by a hyphen.

Boundary Conditions: X is from a to z

Example Input/Output:
Input:
d
Output:
d-D

Solution:
s=input()
print(s+'-'+s.swapcase())
Share:

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:

Print Calendar Month in Words

The program must accept an integer value N and print the corresponding calendar month in words. 1 - January, 2 - February, .... , 11 - November, 12 - December If any value apart from 1 to 12 is passed as input, the program must print "Invalid Input".

Input Format:
The first line denotes the value of N.

Output Format:
The first line contains the output as per the description. (The output is CASE SENSITIVE).

Boundary Conditions:
1 <= N <= 12

Example Input/Output 1:
Input:
5
Output:
May

Example Input/Output 2:
Input:
105
Output:
Invalid Input

Solution 1:
import calendar
n=int(input())
print(calendar.month_name[n]) if n<13 else print('Invalid Input')

Solution 2:
d={1:'January',2:'February',3:'March',4:'April',5:'May',6:'June',7:'July',8:'August',9:'September',10:'October',11:'November',12:'December'}
n=int(input())
print(d[n]) if n<13 else print('Invalid Input')

Solution 3:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
    switch(n)
    {
        case 1:
            printf("January");
            break;
        case 2:
            printf("February");
            break;
        case 3:
            printf("March");
            break;
        case 4:
            printf("April");
            break;
        case 5:
            printf("May");
            break;
        case 6:
            printf("June");
            break;
        case 7:
            printf("July");
            break;
        case 8:
            printf("August");
            break;
        case 9:
            printf("September");
            break;
        case 10:
            printf("October");
            break;
        case 11:
            printf("November");
            break;
        case 12:
            printf("December");
            break;
        default:
            printf("Invalid Input");
    }
}
Share:

String Concatenation

Two string values S1 and S2 are passed as input. The program must print the output as S1 Technologies S2.

Input Format:
The first line denotes the value of S1. The second line denotes the value of S2.

Output Format:
The first line contains the output as per the description. There must be exactly one space between the words.

Boundary Conditions: Length of S1 is from 1 to 100. Length of S2 is from 1 to 100.

Example Input/Output 1:
Input:
Wipro
Bangalore
Output:
Wipro Technologies Bangalore

Solution:
x,y=input().strip(),input().strip()
print(x+' Technologies '+y)
Share: