Thursday, August 31, 2017

Complete Cell State After D Days

There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells (neighbours). Each day, for each cell, if it's neighbours are both active or both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day. The two cells at the ends have only single adjacent cell, so the other imaginary adjacent cell can be assumed to be always inactive. On a given day, even after updating the cell state, consider it's previous day's state for updating the state of other cells. Complete the function cellComplete which takes an 8 element array of integer cells representing the current state of 8 cells and an integer D days representing the number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.

Input Format:
The first line contains 8 integer values representing the initial state of the cells. The second line contains D - which represents the number of days.

Output Format:
The first line contains the state of the cells after D days.

Boundary Conditions: 1 <= D <= 1000

Example Input/Output 1:
Input:
0 0 0 0 1 0 0 
3
Output:
0 0 1 1 1 0 1 0

Example Input/Output 2:
Input:
10 0 0 0 1 0 0 
2
Output:
1 0 1 1 0 0 0 1

Solution:
def cellCompete(c,d):
 for _ in range(d):
    for i in range(8):
        n=c[i]
        if i>0 and i<7:
            c[i]=1 if a!=c[i+1] else 0
        elif i==0:
            c[i]=1 if c[i+1]==1  else 0
        else:
            c[i]=1 if a==1 else 0
        a=n
 return c
cells = [int(x) for x in input().split()]
days = int(input())
print(*cellCompete(cells,days))


Share:

Wednesday, August 30, 2017

Square Matrix - Corner Elements Sum

A square matrix of size N×N is passed as the input. The program must calculate and print the sum of the elements in the corners.

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

Output Format:
The first line will contain the integer value denoting the sum of the elements in the corners.

Boundary Conditions: 2 <= N <= 20

Example Input/Output 1:
Input:
3
10 90 1 4 22 5 32 8 66

Output:
109

Explanation:
The sum = 10+1+66+32 = 109

Solution:
n=int(input())
a=[[int(i) for i in input().split()]for _ in range(n)]
print(a[0][0]+a[0][n-1]+a[n-1][0]+a[n-1][n-1])
Share:

Tuesday, August 29, 2017

Rotate String - N Positions

A string S of length L is passed as the input. The program must rotate the string S by N position in forward direction and print the result as the output.

Input Format:
The first line will contain the value of S.
The second line will contain N.

Output Format:
The first line will contain the rotated string value.

Boundary Conditions:
The length L of the string S is from 3 to 100.
0 <= N <= L

Example Input/Output 1:
Input:
cricket
2

Output:
etcrick

Example Input/Output 2:
Input:
truth
5

Output:
truth

Solution:
s,k=input(),int(input())
print(s[-k:]+s[:-k])



Share:

Range Start and End Position

N integers are passed as input. X which is an integer is also passed as the input. The program must print the start S and end E positions of X in these N integers. -1 must be printed as the start and end positions when X is not present in these N numbers.

Input Format:
The first line contains N. The second line contains N integer values separated by a space. The third line contains X.

Output Format:
The first line contains S and E separated by a space.

Boundary Conditions:
2 <= N <= 1000 -9999 <= X <= 9999

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

Example Input/Output 2:
Input:
7
10 20 10 20 30 40 50 60
Output:
-1 -1

Solution:
n,a,m=int(input()),[int(i) for i in input().split()],int(input())
x,y=-1,-1
for i in range(n):
    if m == a[i]:
        x = i+1 if x==-1 else x
        y = i+1

print(x,y)
Share:

Count the primes in a range

Two whole numbers N1 and N2 are passed as input. The program must print the number of primes present between N1 and N2 (the range is inclusive of N1 and N2)

Input Format:
First line will contain the value of the first number N1 Second line will contain the value of the second number N2

Output Format:
First line will contain the count of prime numbers between N1 and N2

Sample Input/Output:

Example 1:
Input:
6142
6200

Output:
6

Explanation:
The prime numbers within the range 6142 to 6200 are 6143, 6151, 6163, 6173, 6197, 6199

Example 2:
Input:
38
70

Output:
7

Explanation:
The prime numbers within the range 38 to 70 are 41, 43, 47, 53, 59, 61, 67

Solution:
x,y,c=int(input()),int(input()),0
for i in range(x,y+1):
    f=0
    for j in range(2,i//2+1):
        if i%j==0:
            f=1
            break
    if f==0 and i!=1:
        c+=1
print(c)
Share:

Saturday, August 26, 2017

Peak Elements Count

N integers are passed as input. The program must print the count of peak elements among the N integers. An element is a peak element if it is greater than it's neighbours (the elements to the left and right). The elements present in the extreme left and right can never be peak elements.

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

Output Format:
The first line contains the count of peak elements.

Boundary Conditions:
2 <= N <= 1000

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

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

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

Monday, August 21, 2017

Shirt - Matching Pairs

A shop to increase sales during a festival has an offer that a customer will get a discount if the customer buys shirts having same size in pairs. Any customer who buys will choose N shirts and the size of the shirt is denoted by S(i) where 1 <= i <=N. Two shirts S(i) and S(j) are matching and form a pair only if S(i) = S(j).
The program must print the number of pairs eligible for the discount.

Input Format:
The first line will contain the value of N The second line will contain the the size of N shirts S(1) to S(N) with each size separated by a space.

Output Format:
The first line will contain the number of matching pairs eligible for the discount.

Constraints:
2 <= N <= 100

Example Input/Output 1:
Input:
9
10 20 20 10 10 30 44 10 20
Output:
3
Explanation:
The matching pairs are (10,10) (20,20) (10,10).

Example Input/Output 2:
Input:
6
42 44 40 42 44 42
Output:
2
Explanation:
The matching pairs are (42,42) (44,44)

Solution-1:
n = int(input())
a,c,s = [int(i) for i in input().split()],[0]*101,0
for i in a:
    c[i]+=1
a = set(a)
for i in a:
    if c[i]>1:
       s+=c[i]//2
print(s)

Solution-2:
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char** argv)
{
 int n,i,j=0,s=0;
 cin>>n;
 int a[n],b;
 map<int,int> c;
 for(i=0;i<n;i++)
 {
    cin>>b;
    c[b]++;
    if(c[b]==1)
     a[j++] = b;
 }
 for(i=0;i<j;i++)
   if(c[a[i]]>1)
    s+=c[a[i]]/2;
 cout<<s;
}
Share:

Sunday, August 20, 2017

Rounding Marks

In a college, each student receives marks M in any of the subjects in the range from 0 to 100.
- If the difference between the marks and the next multiple of 5 is less than 3, then round the marks up to the next multiple of 5.
- If the difference between the marks and the next multiple of 5 is more than or equal to 3, then leave the marks as it is.
- If the marks obtained is less than or equal to 37, then leave the marks as it is.

Input Format:
The first line will contain the value of N which represents the count of the test cases.
Next N lines will contain the marks from M(1) to M(N)

Output Format:
N lines containing the rounded marks value, one line each for the marks from M(1) to M(N)

Constraints:
2 <= N <= 100

Example Input/Output 1:
Input:
4
83
57
48
33
Output:
85
57
50
33

Solution:
n = int(input())
for _ in range(n):
    a = int(input())
    if ((a//5)+1)*5 - a < 3 and a>37:
        print(((a//5)+1)*5)
    else:
        print(a)
Share:

Tuesday, August 15, 2017

Turning Book Pages

A book can be turned either from front or from the back. When we open a book always we have the page 1 always on the right. The book pages must always be turned one by one instead of multiple pages at once. Each page has two sides, front and back. Hence the last page may have only front side depending on the total number of pages N in the book (If N is even, it will have both sides printed else if N is odd only the front side will be printed).

Now Manoj wants to navigate to a page P in the book by turning the least minimum pages either from front or back. Please help Manoj by completing the program as per the given requirement.

Input Format:
The first line will contain the value of N which represents the total number of pages in the book.
The second line will contain the value of P which represents the page to be navigated.

Output Format:
The first line will contain the integer value which is the least minimum pages to be turned either from front or back.

Constraints:
1 <= N <= 10000
1 <= P <= N

Example Input/Output 1:
Input:
8
6
Output:
1
Explanation:
From front, after turn 1, pages 2 & 3 are visible. After turn 2, pages 4 & 5 are visible. In the third turn pages 6 & 7 are visible. So, 3 turns are required from front.
From back the last page back side is page 8. So, after turn 1, pages 6 & 7 are visible. So, 1 turn is required from the back.
The minimum of 3 and 1 (which is 1) is printed as the output.

Solution-1:

n,p=int(input()),int(input())
n = n+1 if n%2==0 else n
if n/2 > p:
    print(p//2)
else:
    print((n-p)//2)

Solution-2:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 int n,p;
 cin>>n>>p;
 n = n%2==0?n+1:n;
 if(n/2>p)
  cout<<p/2;
 else
  cout<<(n-p)/2;
}
Share:

Monday, August 14, 2017

Farmer - Breaking Yield Records

A farmer wishes to track during each harvest if he has broken previous best or worst records. The yield (in certain units) is passed as input to the program and the program must print how many times he broke his best record along with the number of times he broke his worst record.

Input Format:
The first line will contain the value of N which represents the units harvested in a specific yield.
The second line will contain the units harvested in N yields separated by a space.

Output Format:
The first line will contain the number of times he broke his best record followed by the number of times he broke his worst record, each separated by a space.

Constraints:
3 <= N <= 100

Example Input/Output 1:
Input:
10
3 4 21 36 10 28 30 5 24 42
Output:
4 0
Explanation:
The best yield breaking records are for 4, 21, 36, 42. Hence 4 is printed.
The worst yield breaking record is 0 as none of the yields was less than 3 (which happens to be the first yield).

Example Input/Output 1:
Input:
9
10 5 20 20 4 5 2 25 1
Output:
2 4
Explanation:
The best yield breaking records are for 20, 25. Hence 2 is printed.
The worst yield breaking records are for 5, 4, 2, 1. Hence 4 is printed.

Solution:
n=int(input())
a=[int(x) for x in input().split()]
s,e=a[0],a[0]
p,w=0,0
for i in a:
    if s<i:
        s=i
        p+=1
    if e>i:
        e=i
        w+=1
print(p,w)
Share:

Sunday, August 13, 2017

Exact Capacity Booking

Fed up of overbooking in airlines industry, the government has ordered that airlines cannot book more than the capacity C of an aeroplane. To optimize their revenue, the airlines must confirm booking exactly for C passengers only. The ticket may be booked for just a single passenger or for a group of passengers ranging from 2 to N. (Like 2 tickers for a couple going on a honey moon or 4 tickets for a family of husband, wife and two kids). Given the capacity C and N, print the number of ways W to fill the C seats using ticket bookings with single passengers or group booking count of 2 to N.

Input Format:
The first line contains the values of C.
The second line contains the values of N.

Output Format:
The first line contains the number of ways W.

Boundary Conditions:
1 <= C <= 500
2 <= N <= 10

Example Input/Output 1:

Input:
5
4

Output:
6

Explanation:
The ways to fill 5 seats using booking count of 1,2,3,4 are (1,1,1,1,1) (1,1,1,2) (1,2,2) (1,1,3) (1,4) (2,3) Please note that 1,1,3 is same as the combination of 1,3,1 or 3,1,1 and hence not counted as additional ways.

Solution-1:

n,m=int(input()),int(input())
a = [0]*(n+1)
a[0] = 1
for i in range(1,m+1):
    for j in range(i,n+1):
        a[j] += a[j-i]
print(a[n])

Solution-2:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 int i,j,n,m;
 cin>>n>>m;
 int a[n+1] = {0};
 a[0] = 1;
 for(i=1;i<m+1;i++)
  for(j=i;j<n+1;j++)
    a[j] += a[j-i];
  cout<<a[n];
}
Share:

Pet Store Dogs

There is a per store in a city where dogs can be kept when their owners go for a long tour out of the city or the owners travel abroad. The cage in the pet store can accommodate two dogs. But certain dogs are very aggressive and must be kept along in a cage and hence cannot be put in the cage along with another dog. Given the number of dogs N and assuming that a given dog can be either aggressive or passive, the program must print the number of combinations in which can they be put in the cages (Assume the number of cages is always greater than N and hence there is no shortage of cages).

Input Format:
The first line contains N.

Output Format:
The first line contains the number of combinations in which the dogs can be put in the cages.

Boundary Conditions: 1 <= N <= 999

Example Input/Output 1:
Input:
4

Output:
10

Explanation:
4 dogs can be arranged in 10 combinations as shown below. The numbers inside parentheses indicates that the dogs are put in the same cage.
1 2 3 4
(1 2) 3 4
(1 2) (3 4)
(1 3) 2 4
(1 3) (2 4)
(1 4) 2 3
(1 4) (2 3)
1 (2 3) 4
1 3 (2 4)
1 2 (3 4)
Please note that 1 (2 3) 4 is same as 1 4 (2 3) or 4 (2 3) 1 or 4 (3 2) 1.

Solution:
n=int(input())
a=[1,2]
for i in range(2,n):
    a.append(i*a[i-2]+a[i-1])
print(a[n-1])
Share:

Thursday, August 10, 2017

Peanut Packing

A man is running a super market and he observes the pattern of customer buying peanuts in packs of 1 kg, 2 kgs, 3 kgs till N kgs. So based on the buying pattern and his observation he can price the packs of 1 kg, 2 kgs, ... N kgs at rupees P(1), P(2), P(3), ... P(N) respectively. What is the maximum revenue he can earn when selling N kgs of peanuts based on the given pricing?

Input Format:
The first line contains N. The second line contains the integer value denoting the price for 1 kg pack, 2 kgs pack till N kgs pack with each value separated by a space.

Output Format:
The first line contains the maximum revenue he can earn by packing and selling the N kgs peanuts based on the given pricing.

Boundary Conditions: 1 <= N <= 999

Example Input/Output 1:
Input:
4
120 250 360 490

Output:
500

Explanation: While selling 4 kgs of peanuts, the maximum revenue of Rs.500 is obtained when he packs the 4 kgs as 2 kgs + 2kgs and sells them at Rs. 250 each.

Example Input/Output 2:
Input:
4
120 240 360 490

Output:
490

Explanation: While selling 4 kgs of peanuts, the maximum revenue of Rs.490 is obtained when he packs the 4 kgs as a single 4 kgs pack and sells it ar Rs.490

Solution:
n,a = int(input()),[int(i) for i in input().split()]
m = [0]*(n+1)
a = [0]+a
for i in range(1,n+1):
    m[i] = a[i]
    for j in range(i-1,0,-1):
        if m[i]<m[j]+m[i-j]:
            m[i] = m[j]+m[i-j]
print(m[n])
Share:

Tuesday, August 8, 2017

Triangle Pattern - Modified Pascal

Given an integer N, print a triangle as mentioned in the below examples.

Input Format:
The first line contains N.

Output Format:
N lines containing the triangle pattern as shown in the example input/output.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
4

Output:
      1                                                                                                                                                               
    232                                                                                                                                                               
  34543                                                                                                                                                            4567654

Example Input/Output 2:
Input:
6

Output:
          1                                                                                                                                                             
        232                                                                                                                                                             
      34543                                                                                                                                                            
    4567654                                                                                                                                                          
  567898765                                                                                                                                                         
67891219876

Solution:

n = int(input())
k=0
for i in range(n):
    for j in range(2*n-1):
        if j >= n-i-1 and j<=n-1:
            k+=1
            k = 1 if k==10 else k
            print(k,end='')
        elif j>n-i and j<=n+i-1:
            k-=1
            k = 9 if k==0 else k
            print(k,end='')
        else:
            print(end=' ')
    print()
Share: