Monday, November 13, 2017

Train Journey - Initial Boarding Count

In a train journey, there are N stations between station A and B (The train starts from station A). The number of passengers who alighted in station B is passed as the input. The number of passengers who alighted and boarded in the N stations in between is also passed as the input. The program must print the number of passengers who boarded the train at station A.

Input Format:
The first line contains N and the number of passengers who alighted at station B (which is the last station), separated by a space. N lines containing the number of passengers who alighted and boarded the train in the N stations in between A and B, with the values separated by a space.

Output Format:
The first line contains the number of passengers who boarded the train at station A.

Boundary Conditions: 1 <= N <= 50

Example Input/Output 1:
Input:
3 50
30 40
40 10
60 30
Output:
100
Explanation:
There are 3 stations in between A and B. 100 passengers boarded the train at station A. In the 2nd station (first after A), 30 got down and 40 boarded the train. So now passengers in the train is 100-30+40 = 110 In the 3rd station (second after A), 40 got down and 10 boarded. So passengers left in the train = 110-40+10 = 80 In the 4th station, passengers left in the train = 80-60+30 = 50. These 50 passengers will alight in the last station which is station B.

Solution:
n,b=[int(i) for i in input().split()]
for i in range(n):
    x,y=[int(i) for i in input().split()]
    a=b+x-y
    b=a
print(a)
Share:

Remove characters occurring multiple times

A string S is passed as the input. The program must remove all characters which appear more than once. If all the characters in a string are occurring more than once, then print -1 as output.

Input Format:
The first line contains the value of S.

Boundary Conditions: 2 <= Length of S <= 100

Output Format:
The string with the characters occurring more than once removed.

Example Input/Output 1:
Input:
level
Output:
v
Explanation: l and e occur more than once. Hence they are removed.

Example Input/Output 2:
Input:
ababab
Output:
-1

Solution:
s,c=input(),0
for i in s:
    if s.count(i)==1:
        print(i,end="")
    else:
        c+=1
if count==len(s): print(-1)
Share:

Matrix Pattern Printing - First/Last Higher

Given an integer N as the input, print the pattern as given in the Example Input/Output section.

Input Format:
The first line contains N.

Output Format:
N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
3
Output:
1 1 1 2
3 2 2 2
3 3 3 4

Solution:
#include <stdio.h>
void printPattern(int n)
{
 for(int i=1;i<=n;i++)
 {
     for(int j=0;j<=n;j++)
      if(i%2==1&&j==n||i%2==0&&j==0)
       printf("%d ",i+1);
      else
       printf("%d ",i);
      printf("\n");
 }
}
int main()
{
int N;
scanf("%d",&N);
printPattern(N);
}
Share:

Inner Reduce Pattern Printing

Given a number N, the program must print the pattern as described below.

Input Format:
The first line contains the value of the N which represent the number N.

Boundary Conditions: 2 <= N <= 9

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

Example Input/Output 1:
Input:
4
Output:
4444444
4333334
4322234
4321234
4322234
4333334
4444444

Solution 1:
n=int(input())
a=[[0 for _ in range(2*n-1)]for _ in range(2*n-1)]
for i in range(n,-1,-1):
    y=n-i
    for x in range(y,2*n-1-y):
        a[y][x]=i
        a[x][y]=i
    y=2*n-2-y
    for x in range(n-i,y+1):
        a[y][x]=i
        a[x][y]=i
for i in a:
    print(''.join(str(j) for j in i))

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int i,x,y,n;
 cin>>n;
 int a[2*n-1][2*n-1];
 for(i=n;i>=0;i--)
 {
     y=n-i;
     for(x=y;x<(2*n-1-y);x++)
      {
          a[y][x]=i;
          a[x][y]=i;
      }
    y=2*n-2-y;
    for(x=n-i;x<=y;x++)
    {
        a[y][x]=i;
        a[x][y]=i;
    }
 }
 for(x=0;x<(2*n-1);x++)
 {
    for(y=0;y<(2*n-1);y++)
     cout<<a[x][y];
    cout<<endl;
 }
}

Solution 3:
#include<iostream>
using namespace std;
 int main()
{
  int n;
  cin>>n;
  for(int i=0;i<2*n-1;i++)
  {
     int k=i<n?i:2*(n-1)-i;
     for(int j=0;j<k;j++)
       cout<<n-j;
     for(int j=0;j<2*(n-k)-1;j++)
       cout<<n-k;
     for(int j=k-1;j>=0;j--)
       cout<<n-j;
     cout<<endl;
  }
  return 0;
}

Share:

Left Number Twice Right

A set of N numbers (separated by one or more spaces) is passed as input to the program. The program must identify the count of numbers where the number on the left is twice the number on the right.

Input Format:
The first line will contain the N numbers separated by one or more spaces.

Boundary Conditions: 3 <= N <= 50 The value of the numbers can be from -99999999 to 99999999

Output Format:
The count of numbers where the sum of the numbers on the LHS is twice that of the sum of numbers on the RHS.

Example Input/Output 1:
Input:
10 20 5 40 15 -2 -30 -1 60
Output:
2
Explanation:
The numbers meeting the criteria are 20, -30

Solution:
a,c=[int(i) for i in input().split()],0
for i in range(1,len(a)-1):
    if a[i-1]==2*a[i+1]:
        c+=1
print(c)
Share:

Diagonal Sum - 1

A set of numbers forming a matrix N*N is passed as input. The program has to print the sum of numbers along the diagonals.

Input Format:
The first line will contain the value of N. The next N lines will contain N numbers each separated by one or more spaces.

Boundary Conditions: 2 <= N <= 10 Value of a given number in the matrix is from 0 to 99999999.

Output Format:
The sum of the numbers along the diagonals.

Example Input/Output 1:
Input:
3
5 6 7
8 9 1
2 3 4
Output:
27
Explanation:
The sum of the numbers along the diagonal = 5+9+4+7+2 = 27. Please pay attention in not counting the overlapping number 9 (which is in the middle) twice.

Solution 1:
n=int(input())
a=[[int(i) for i in input().split()]for i in range(n)]
x=sum(a[i][i]+a[i][n-1-i] for i in range(n))
print(x) if n%2==0 else print(x-a[n//2][n//2])

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

Friend requests in social network

In a social network, a person can invite friends of his/her friend. John wants to invite and add new friends. Complete the program below so that it prints the names of the persons to whom John can send a friend request.

Input Format: 
The first line contains the value of the N which represent the number of friends. Next N lines contain the name of the friend F followed by the number of friends of F and finally their names separated by space. 

Boundary Conditions: 2 <= N <= 10 

Output Format: 
The names to which John can send a friend request. 

Example Input/Output 1: 
Input: 
3 
Mani 3 Ram Raj Guna 
Ram 2 Kumar Kishore 
Mughil 3 Praveen Naveen Ramesh 

Output: 
Raj Guna Kumar Kishore Praveen Naveen Ramesh 

Explanation: 
Ram is not present in the output as Ram is already John's friend. 

Solution:
n = int(input())
already_friend, be_friend, soln = [], [], []
for _ in range(n):
    x, y, *z = input().strip().split()
    already_friend.append(x)
    for j in z:
        be_friend.append(j)
for i in be_friend:
    if i not in already_friend:
        soln.append(i)
print(*soln)
Share:

Collatz Sequence

A number N is passed as the input. The program must print the number of steps required for the program to reach Collatz sequence (that is reach 1). Collatz Sequence: Suppose we select some initial number N and then build the sequence of values by the following rules: if N is even then Nnext = N / 2 else Nnext = 3 * N + 1 For example, with N = 15 we have sequence: 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 After the sequence reaches 1 it enters the loop 1 4 2 1 4 2 1 and so on .... The program must print how many steps are necessary to come to 1.

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

Output Format:
The first line contains the number of steps required for term in the series to reach 1 (ie Collatz Sequence)

Boundary Conditions: 3 <= N <= 9999999

Example Input/Output:
Input:
5
Output:
5
Explanation:
The series is 5 16 8 4 2 1

Solution:
n,s=int(input()),0
while n!=1:
    n = n//2 if n%2==0 else 3*n+1
    s+=1
print(s)
Share:

Identical Toys Distribution

Given the number of T identical toys, find the number of ways W to distribute them among N children.

Input Format:
The first line will contain T and N separated by a space.

Output Format:
The first line will contain W. As W can be large, print the answer as modulo 1000000007

Boundary Conditions: 1 <= T, N <= 1000

Example Input/Output 1:
Input:
2 2
Output:
3
Explanation:
The three ways to disribute 2 toys among 2 children are (2,0) (0,2) (1,1)

Solution:
from math import factorial as fc
x,y=[int(i) for i in input().split()]
print((fc(x+y-1)//(fc(x)*fc(y-1)))%1000000007)
Share: