Saturday, October 21, 2017

Adamant Kid

An adamant kid keeps on repeating the stuff he wants. Like if the kid wants chocolate he keeps repeating "chocolate". Given the stuff the child is demanding the program must print if the character in two given positions X, Y is same or not by printing YES or NO.

Input Format:
First line will contain the name of the stuff as a string value S. Second line will contain two integer values X, Y denoting the position of the characters. The values of X and Y are separated by a space.

Output Format:
First line will contain YES or NO Boundary Conditions: Length of S is from 2 to 100.

Sample Input/Output 1:
Input:
icecream
4 10
Output:
YES
Explanation:
As the kid keeps on repeating icecream, the 10th position character will also be c in icecreamicecream. As the characters in 4th and 10th position are same (that is c) the output is YES

Sample Input/Output 2:
Input:
cake
2 12
Output:
NO
Explanation:
When cake is repeated like cakecakecakecake, the character in 12th position is e which is NOT equal to the character a in 2nd position. Hence the output is NO.

Solution:
s=input().strip()
x,y=[int(i) for i in input().split()]
if s[x-1]==s[(y-1)%len(s)]:
    print('YES')
else:
    print('NO')
Share:

Sort Numbers - Factor Count

The program must accept N positive integers as the input and sort them based on the factor count (lowest to highest factor count). If two numbers have the same factor count, order based on the value of the numbers in the ascending order.

Input Format:
The first line will contain N. The second line will contain N positive integers separated by a space.

Output Format:
The first line will contain the N positive integers (separated by a space) ordered by their factor count.

Boundary Conditions: 2 <= N <= 10000

Example Input/Output:
Input:
5
18 23 100 1200 45
Output:
23 18 45 100 1200

Solution 1:
import math
n,x,y=int(input()),[int(i) for i in input().split()],[]
for i in x:
    s=math.ceil(i**0.5)
    c=1 if s*s==i else 0
    for j in range(1,s):
        if i%j==0:
            c+=2
    y.append(c)
f=sorted(list(zip(y,x)))
print(*[j for i,j in f])

Solution 2:
#include <bits/stdc++.h>
using namespace std;
int cnt(int a)
{
    int c,i,s=sqrt(a);
    s*s==a?c=1:c=0;
    for(i=1;i<sqrt(a);i++)
     if(a%i==0)
       c+=2;
     return c;
}
bool fc(int x,int y)
{
    int a=cnt(x),b=cnt(y);
    return a==b?x<y:a<b;
}
int main()
{
 int i,n;
 cin>>n;
 int a[n];
 for(i=0;i<n;i++)
  cin>>a[i];
 sort(a,a+n,fc);
 for(i=0;i<n;i++)
  cout<<a[i]<<" ";
}
Share:

Thursday, October 19, 2017

Sean's Encryption Quest!

One-day Sean's friend John wanted him to encrypt a few messages that were to be sent via a TELEGRAM group, so that everyone could see the message, but only those special people who knew decryption can actually understand it. Help Sean write a code that take input values of the type of encryption, a key, and a message to be encrypted that had something to do with the key.

Input Format:
First line containing the type of encryption X. Second line containing the key K. Third line containing the message M.

Output Format:
The first line contains the encrypted message.

Note:
The encryption process is based on the simple logic of ADDITION, that is simply adding the value of the key (modified key) to the characters of the message to be encrypted. ONLY UPPER-CASE alphabets need to be considered.
X is a number -1 or 0 or 1,
a) -1 denotes the decreasing value of the key,
b) 0 denotes the constant value of the key,
c) 1 denotes the increasing value of the key
d) In case of invalid input value of X simply display “Invalid Input”.
If the input value of K is less than or equal to 0, make the initial value of K as 1

Boundary Conditions:
The maximum length of the message M is 200 and its minimum length is 1

Example Input/Output 1:
Input:
1
2
SUMMER
Output:
UXQRKY
Explanation:
Every time the key is used on a character its value is incremented by 1

Example Input/Output 2:
Input:
-1
6
HOLIDAY
Output:
NTPLFBY
Explanation:
Every time the key is used on a character its value is decremented by 1

Example Input/Output 3:
Input:
0
2
ENTERTAINMENT
Output:
GPVGTVCKPOGPV

Solution:
#include <iostream>
using namespace std;
int main()
{
  int i,x,k;
  string m;
  cin>>x>>k>>m;
  if(x<-1||x>1)
   cout<<"Invalid Input";
  else
  {
      k<=0?k=1:1;
      for(i=0;i<m.length();i++)
      {
          m[i]=k>0?(m[i]+k-'A')%26+'A':m[i]+(k%26);
          x==1?k++:x==-1?k--:1;
      }
      cout<<m;
  }
}
Share:

Reverse & Print Common Characters

Two string values S1 and S2 are passed as the input. Reverse string S2 and print the letters which are common in a given index. The letters in S1 and S2 will be in lower case.

Input Format:
The first line contains S1. The second line contains S2.

Output Format:
The first line contains the letters which are common after S2 is reversed.

Boundary Conditions: 2 <= Length of S1 <= 500 2 <= Length of S2 <= 500

Example Input/Output 1:
Input:
energy
genuine
Output:
en
Explanation:
After reversing S2, we get energy, eniuneg
The letters common comparing indices are en (in the first two positions)

Solution:
s,x=input(),input()
i,j=0,len(x)-1
while i<len(s) and j>=0:
    if s[i]==x[j]:
        print(s[i],end='')
    j-=1
    i+=1

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 string s,x;
 cin>>s>>x;
  for(int i=0,j=x.length()-1;i<s.length()&&j>=0;i++,j--)
   if(s[i]==x[j])
    cout<<s[i];
}
Share:

Jersey Number - Highest in Team

In a school there are N students standing in a straight line. Their Jersey numbers from left to right are passed as the input. The students are to be divided into teams of size T starting from left. First T students form Team 1, next T students form Team 2 and so on. If N is not divisible by T and say the remainder is R, the R students are distributed among the teams formed so far in a round robin fashion (starting from the first team) until the R students are assigned to one of the teams. Once the teams are formed, the program must print the highest jersey number in each team.

Input Format:
The first line contains N. The second line contains the jersey numbers of N students separated by a space. The third line contains T.

Output Format:
The highest jersey number in each team separated by a space.

Boundary Conditions: 1 <= N <= 9999999

Example Input/Output:
Input:
10
1 5 8 2 4 11 3 6 9 7
4
Output:
9 11

Solution:
n=int(input())
jersey=[int(x) for x in input().split()]
t=int(input())
teamc=n//t
if teamc==0:
    teamc=1
maxlist=[]
for i in range(teamc):
    maxlist.append(max(jersey[i*t:i*t+t]))
ctr=0
for i in range(t*teamc,n):
    maxlist[ctr]=max(maxlist[ctr],jersey[i])
    ctr+=1
    if ctr==len(maxlist):
        ctr=0
print(*maxlist)
Share:

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: