Thursday, November 30, 2017

Bank Account Balance

In a given month, N transactions related to a bank account are passed as the input along with the initial balance IB present in the bank account. The program must print the final balance FB in the account after all the N transactions have been completed. Each transaction has a code followed by the amount. The following are the possible codes and a code is classified either as credit or debit. CASH - Cash deposit in the account (CREDIT) ATM - Withdrawal using ATM (DEBIT) CHQ - Cheque deposit (CREDIT) CHQW - Payment made using a cheque to another entity (DEBIT) INT - Interest credited (CREDIT) MNT - Account Maintenance Charges (DEBIT) CARD - Purchase using Debit Card(DEBIT) ET - Electronic Transfer using NEFT/RTGS/IMPS etc If more than L ATM withdrawals are made in a month, then Rs.X is deducted for each withdrawal. If a card purchase is made for more than Rs.Y, then 10% of the purchase amount (floored to the nearest integer value) is credited as incentive for cashless mode.

Input Format:
The first line contains IB, N, L, X, Y each separated by a space. Next N lines contain the details for the N transactions, the code and the amount separated by a space. The amount will always be an integer value.

Output Format:
The first line contains FB

Boundary Conditions:
1 <= N <= 100 0 <= IB, FB <= 99999999 || 1 <= X <= 100 || 10 <= Y <= 10000 || 4 <= L <= 100

Example Input/Output 1:
Input:
10000 10 3 50 1000
CASH 5000
ATM 2000
CARD 2400
ATM 2000
CHQW 2500
ET 20000
CASH 1200
ATM 2500
CARD 579
ATM 5000
Output:
19411
Explanation:
There are 10 transactions made and the initial balance is 10000.
After 1st txn, as 5000 is deposited, the balance is 15000.
After 2nd txn, as 2000 is withdrawn using ATM, the balance is 13000.
After 3rd txn, as a card purchase is made for 2400, the balance is 10600. But as 2400 is more than the incentive limit of 1000, 10% of 2400 = 240 is credited making the balance as 10840.
After 4th txn, as 2000 is withdrawn using ATM, the balance is 8840.
After 5th txn, as 2500 is debited using cheque, the balance is 6340.
After 6th txn, 20000 is credited using electronic transfer and hence the balance is 26340.
After 7th txn, 1200 is deposited using cash and hence the balance is 27540.
After 8th txn, as 2500 is withdrawn using ATM, the balance is 25040.
After 9th txn, a card purchase of 579, the balance is 24461. As the purchase amount via card is less than 1000, no incentive is credited.
After 10th txn, the balance is 19461. But as it is the 4th transaction via ATM, Rs.50 is deducted making the final balance as 19411.

Solution 1:
ib,n,l,x,y=[int(i) for i in input().split()]
c,fb=0,ib
for i in range(n):
    t,a=input().split()
    if t=='CASH' or t=='CHQ' or t=='INT' or t=='ET':
        fb+=int(a)
    elif t=='CHQW' or t=='MNT':
        fb-=int(a)
    elif t=='ATM':
        fb,c=fb-int(a),c+1
        if c>l:
            fb-=x
    elif t=='CARD':
        fb-=int(a)
        if int(a)>y:
            fb+=int(10/100*int(a))
print(fb)

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int i,ib,n,l,x,y,a,c=0;
 string tn;
 cin>>ib>>n>>l>>x>>y;
 for(i=0;i<n;i++)
 {
     cin>>tn>>a;
     if(tn=="CASH"||tn=="CHQ"||tn=="INT"||tn=="ET")
      ib+=a;
     else if(tn=="CHQW"||tn=="MNT")
      ib-=a;
     else if(tn=="ATM")
     {
        ib-=a;
        c++;
        if(c>l) ib-=x;
     }
     else
     {
         ib-=a;
         if(a>y)
          ib+=(a*10/100);
     }
 }
 cout<<ib;
}
Share:

Wednesday, November 29, 2017

Same Unit Digit Adjacent Numbers

A matrix of size R*C (R rows and C columns) containing numbers (positive integer values) is passed as input. The program must print the count C of numbers in a given cell which has at least one adjacent cell (in the same row) containing a number with the same unit digit.

Input Format:
The first line contains R and C separated by a space. Next R lines contains C values separated by a space.

Output Format:
The first line contains C.

Boundary Conditions: 1 <= R, C <= 100

Example Input/Output 1:
Input:
4 3
20 27 67
13 74 100
37 90 97
53 75 44
Output:
 2
Explanation:
The numbers are 27 67 in the first row where both have 7 as their unit digit.

Solution:
#include <iostream>
using namespace std;
int main()
{
 int i,j,r,c,cn=0;
 cin>>r>>c;
 int a[r][c];
 for(i=0;i<r;i++)
  for(j=0;j<c;j++)
   cin>>a[i][j];
  for(i=0;i<r;i++)
  {
      for(j=1;j<c-1;j++)
       if(a[i][j]%10==a[i][j+1]%10||a[i][j]%10==a[i][j-1]%10)
        cn++;
      if(a[i][0]%10==a[i][1]%10) cn++;
      if(a[i][c-1]%10==a[i][c-2]%10) cn++;
  }
 cout<<cn;
}
Share:

Tuesday, November 28, 2017

Only Fibonacci

N integers are passed as input. The program must print only the integers that are present in the Fibonacci series.

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

Output Format:
Integers that are part of the Fibonacci series in their order of occurrence (separated by a space).

Boundary Conditions: 1 <= N <= 9999999

Example Input/Output 1:
Input:
5
13 2 10 4 8
Output:
13 2 8
Explanation: The Fibonacci series is 0 1 1 2 3 5 8 13 21 and so on. So the input values that are part of the Fibonacci series are 13 2 8 in their order of occurrence.

Solution:
def ps(x):
    return int(x**(0.5))**2==x
n,a=input(),[int(i) for i in input().split()]
print(*[i for i in a if ps(5*i*i+4) or ps(5*i*i-4)])
Share:

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:

Sunday, November 26, 2017

Numbers - First and Last Digits Same

N numbers A(1) to A(N) are passed as input. The program must print only the X numbers which have same first and last digit.

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

Output Format:
The first line contains the X numbers separated by a space.

Boundary Conditions:
2 <= N <= 200
10 <= A(i) <= 9999999
1 <= X <= N

Example Input/Output 1:
Input:
4
102 333 282 500
Output:
333 282

Solution 1:
n=input()
for i in input().split():
    if i[0]==i[-1]: print(i,end=' ')

Solution 2:
#include <iostream>
using namespace std;
int main(){
   int n,i;
   string a;
   cin>>n;
   for(i=0;i<n;i++)
   {
       cin>>a;
       if(a[0]==a[a.size()-1])
       cout<<a<<" ";
   }
}
Share:

LRU Cache - Miss Count

The least recently used (LRU) cache algorithm evicts the element from the cache that was least recently used when the cache is full. After an element is requested from the cache, it should be added to the cache (if not there) and considered the most recently used element in the cache whether it is newly added or was already existing. Initially, the cache is empty. Implement the function lruCountMiss so that the function returns an integer indicating the number of cache misses M using the LRU cache algorithm execution for the given input. Assume that the array pages always have pages numbered from 0 to 50. (A hit means the requested page is already existing in the cache and a miss means the requested page is not found in the cache).

Input Format:
The first line contains the cache size S and the number of page requests N separated by a space. The second line containing the N pages being requested from the cache.

Output Format:
The first line contains the miss count M.

Boundary Conditions: 2 <= S <= N 2 <= N <= 100

Example Input/Output 1:
Input:
3 16
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
Output: 11

Solution 1:
s,n=[int(i) for i in input().split()]
p=[int(i) for i in input().split()]
c,m=[],0
for i in p:
    if i in c:
        c.remove(i)
    else:
        if len(c)>=s:
            c.pop(0)
        m=m+1
    c.append(i)
print(m)

Solution 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
   int i,s,n,a,m=0;
   list<int> c;
   cin>>s>>n;
   for(i=0;i<n;i++)
   {
       cin>>a;
       if(find(c.begin(),c.end(),a)!=c.end())
        c.remove(a);
       else
        {
            if(c.size()>=s)
               c.pop_front();
            m++;
        }
      c.push_back(a);   
   }
   cout<<m;
   return 0;
}

Solution 3:
s,n=[int(i) for i in input().split()]
arr_pages=[int(i) for i in input().split()]
cache=[None]*s
x,miss=0,0
for i in arr_pages:
    if not cache or x<s and not i in cache:
        cache[x]=i
        x+=1
        miss+=1
    else:
        if i not in cache:
            miss+=1
            begin=0
        else:
            begin=cache.index(i)
        for j in range(begin,s):
            cache[j]=cache[j+1] if j<s-1 else i
print(miss)


Share:

Friday, November 24, 2017

Remaining Balloon Numbers

There are N filled balloons each painted with a random number B(i) where i is from 1 to N and the balloons are tied up to a rope in a straight line. M kids who play football arrive and they decide to burst the balloons with the numbers divisible by their jersey numbers J(a) where a is from 1 to M. The program must print the numbers on the balloons remaining after all M kids burst the balloons in the order of their occurrence. If none of the balloons are remaining then the program must print -1.

Input Format:
The first line contains N and M separated by a space. The second line contains N positive integers which denote the numbers on the balloons separated by a space. The third line contains M positive integers which denote the numbers on the jerseys separated by a space.

Output Format:
The first line contains the numbers on the remaining balloons separated by a space (or -1 if no balloons remain)

Boundary Conditions: 1 <= N <= 9999 1 <= M <= 20

Example Input/Output 1:
Input:
11 3
38 40 11 46 44 48 35 14 39 44 23
2 3 11
Output:
35 23
Explanation:
The 1st kid bursts balloons with numbers which are divisible by 2. So the balloons remaining are 11 35 39 23 The 2nd kid bursts balloons with numbers which are divisible by 3. So the balloons remaining are 11 35 23. The 3rd kid bursts balloons with numbers which are divisible by 11. So the balloons remaining are 35 23.

Solution:
n,m=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
jn=[int(i) for i in input().split()]
a=list(filter(lambda x: all(x%j!=0 for j in jn),a))
print(*a) if a else print('-1')
Share:

Wednesday, November 22, 2017

Square of Prime Closest to Sum

Two numbers N1, N2 are passed as input. The program must find the closest prime number to their sum and then print the square of that prime number. If there are multiple prime numbers which are the closest to the sum, then the program must print -1.

Input Format:
The first line will contain the number N1 The second line will contain the number N2

Boundary Conditions: 1 <= N1 <= 99999999 1 <= N2 <= 99999999

Output Format: The square of the prime number as per the conditions mentioned above.

Example Input/Output 1:
Input:
2
1
Output:
9
Explanation: The sum of the numbers is 3. The prime number closest to 3 is 3 itself. Hence square of 3 = 9 is printed as output.

Solution 1:
def prime(x):
    for i in range(2,int(x**(0.5))):
        if x%i==0:
            return 0
    return 1
s,i,j=int(input())+int(input()),0,0
while 1:
    if prime(s-i) and prime(s+j): break
    elif not prime(s-i): i+=1
    else: j+=1
if i==0: print(s*s)
elif i==j: print('-1')
elif i>j: print((s+j)**2)
else: print((s-i)**2)
Share:

Tuesday, November 21, 2017

Robot No Movement Count

As a final year project certain student in a college have designed a Robot which can move front, back, left or right in a given rectangular grid of dimension L*B units (L denotes the length from left to right and B denotes the breadth from top to bottom). Always the robot moves in units which are of integer values. The robot cannot move outside the grid (That is the robot cannot go beyond L and B units). A sequence of N movement instructions are given to the robot to move in the desired direction (F-front or up, B-back or down, L-left, R-right) and the robot moves if the destination falls within the limit of the grid dimensions. Else the robot does not move. Assume the robot always starts at the bottom left of the grid. The program must print the number of movement instructions C for which the robot did not move (as the destination was outside the grid)

Input Format:
The first line contains L and B separated by a space. The second line contains N which denotes the number of instructions. The third line contains N instructions separated by one or more spaces.

Output Format:
The first line contains C which denoted the count of instructions for which the robot did not move.

Boundary Conditions: 1 <= L, B <= 999 1 <= N <= 999 C <= N

Example Input/Output 1:
Input:
6 5
9
3R 2L 11R 4F 4R 2F 3B 5L 4B
Output:
3
Explanation: The robot did not move for the instructions 11R, 2F and 4B as they will make the robot go outside the grid.

Solution 1:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int l,b,n,i,a,s=0,s1=0,count=0;
char c;
scanf("%d %d \n %d",&l,&b,&n);
for(i=0;i<n;i++)
{
    scanf("%d%c",&a,&c);
    if(c=='R'&&s+a<=l)
        s=s+a;
    else if(c=='L'&&s-a>=0)
        s=s-a;
    else if(c=='F'&&s1+a<=b)
        s1=s1+a;
    else if(c=='B'&&s1-a>=0)
        s1=s1-a;
    else
    count++;
}
printf("%d",count);
}

Solution 2:
l,b=[int(i) for i in input().split()]
n,a,x,y,c=int(input()),input().split(),0,0,0
for s in a:
    m=int(s[:len(s)-1])
    if s[-1]=='R' and x+m<=l:
        x+=m
    elif s[-1]=='L' and x-m>=0:
        x-=m
    elif s[-1]=='F' and y+m<=b:
        y+=m
    elif s[-1]== 'B' and y-m>=0:
        y-=m
    else: c+=1
print(c)



Share:

Palindrome Missing Alphabet

String S which is a palindrome is passed as the input. But just one alphabet A is missing in S. The program must print the missing alphabet A. Note: The FIRST alphabet of S will always be present.

Input Format:
The first line contains S.

Output Format:
The first line contains the missing alphabet A.

Boundary Conditions:
The length of the input string S is between 3 to 100. The FIRST alphabet of S will always be present.

Example Input/Output 1:
Input:
malayaam
Output:
l

Solution 1:
#include <iostream>
using namespace std;
int main(){
    string c;
    cin>>c;
    int l=c.size();
    for(int i=0,j=l-1;i<l/2;i++,j--) {
        if(c[i]!=c[j]&& c[i]==c[j-1]&&(i!=j-1))
        {
                cout<<c[j];
                break;
         }
         else if(c[i]!=c[j])
            {
                cout<<c[i];
                break;
            }
        }
}

Solution 2:
s=input()
n=len(s)
for i in range(n):
    if s.count(s[i])&1==1:
       if n&1==0 and i!=n//2 or n&1==1:
           print(s[i])
           break
Share:

Sunday, November 19, 2017

Sliding Window Maximum

Given a list of N positive integers and a sliding window of size W, the program must print the compare the maximum value CURRMAX present in the specific window instance with that of the previous window maximum PREVMAX and print the maximum value of CURRMAX and PREVMAX. The maximum values must be separated by a space.

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

Output Format:
The first line contains the maximum values separated by a space.

Boundary Conditions: 1 <= N <= 10000 W <= N

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

Solution:
n,x=[int(i) for i in input().split()]
a,p,c=[int(i) for i in input().split()],0,0
for i in range(n-x+1):
    c=max(a[i:i+x])
    print(max(c,p),end=' ')
    p=c
Share:

Saturday, November 18, 2017

String - Word Counter

Given a string S as input which consists of multiple words separated by a space, the program must print the count C of the words which are repeated exactly N times. The comparison of the words is case sensitive.

Input Format:
The first line contains S The second line contains N

Output Format:
The first line contains C

Boundary Conditions: 1 <= Length of S <= 10000

Example Input/Output 1:
Input:
one two three four three two five
1
Output:
3
Explanation: The words which are repeated only once are one, four and five. Hence the count is 3.

Solution 1:
a,n=input().split(),int(input())
print(len(set([x for x in a if a.count(x)==n])))

Solution 2:
s,n,c=input().split(),int(input()),0
for i in s:
    if s.count(i)==n:
        c+=1
print(int(c/n))

Solution 3:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,a[1000];
int n=-1,m,i=0,l,c=0;
while((cin>>a[++n])>0);
m=atoi(a[--n].c_str());
sort(a,a+n);
while(i<n)
{
l=count(a+i,a+n,a[i]);
i+=l;
if(l==m)
c++;
}
cout<<c;
}
Share:

Array Maximum Sum Divisible By N

An array of numbers separated by space will be passed as input. A number N is also passed as input. The program has to print the maximum sum of the numbers in the array which is divisible by N. If there is no such maximum sum of the numbers, the program should print -1 as output.

Input Format:
The first line contains the array of numbers separated by space. The second line contains the value of N

Boundary Conditions: The length of the array of numbers will be from 3 to 200. 1 <= N <= 1000

Output Format:
The maximum sum of the numbers in the array that is divisible by N.

Example Input/Output 1:
Input:
10 20 40 70
3
Output:
120
Explanation: The maximum sum of numbers that is divisible by 3 is 120 (10+40+70) and hence it is printed as the output.

Solution 1:
a,n= [int(i) for i in input().split()],int(input())
x=sorted([sum(a)-i for i in a if (sum(a)-i)%n==0])
print(-1 if len(x)==0 else x[-1])

Solution 2:
nm,n,d= [int(i) for i in input().split()],int(input().strip()),[[0]*n]
for i in range(1,len(nm)+1):
    t=[]
    for j in range(n):
        x=(j+n-nm[i-1]%n)%n
        if d[i-1][x]%n!=x and x!=0:
            t.append(max(d[i-1][x],d[i-1][j]))
        else:
            t.append(max(d[i-1][x]+nm[i-1],d[i-1][j]))
    d.append(t)
if d[-1][0]!=0:
    print(d[-1][0])
else:
    print('-1')

Solution 3:
import itertools
a,k,l,m=[int(i) for i in input().split()],int(input()),[],[]
for i in range(1,len(a)+1):
    l.append(list(itertools.combinations(a,i)))
for i in l:
    for j in i:
      s=sum(j)
      if s%k==0:
         m.append(s)
print(max(m)) if len(m)!=0 else print('-1')
Share: