Thursday, October 31, 2019

Common Divisor

You are given a sequence of integers. You want to find out if all the numbers have a common divisor.
If there exists such a number, print "YES", otherwise print "NO".
Input Format:
The first line of the input contains an integer N, denoting the number of elements in the array.
The second line contains N space-separated integers.
Output Format:
Print "YES" or "NO" on a single line(without quotes) according to the above condition.

Example:
Input:
3
2 3 4
Output:
NO
Input:
5
5 10 15 20 25
Output:
YES

CODE::
n=int(input())
a=list(map(int,input().split()))
b=min(a)
divi=1
count=0
for i in range(2,b+1):
    count=0
    for j in range(n):
        if a[j]%i==0:
            count+=1
    if count==n:
        divi=i
if divi==1:
    print("NO")
else:
    print("YES")

        

Tuesday, October 29, 2019

Maximum Weaving Number

Given two numbers A=a1a2...an and B= b1b2...Bm, we can weave these two numbers to produce the following numbers.
a1b1a2b2...anbnb(n+1)...bm,, if m>n
a1b1a2b2...ambma(m+1)...an if m<n
a1b1a2b2...anbn if m=n.
Similarly, we can also get b1a1b2a2.... as above.
We can also start weaving from the end. By weaving from the end, we can form the words: anbnb(n-1)a(n-1)... , bnanb(n-1)a(n-1)...
Thus, by weaving A and B, four new numbers are formed: weaving from the beginning, starting with A, weaving from the beginning, starting with B, weaving from the end, starting with A, weaving from the end, starting with B. While weaving two numbers A and B, if all the digits of A are weaved and some more digits are there in B, the remaining digits of B are just appended at the end. For example, if A = 27 and B = 54 then there are four numbers that can be woven
2574
5247
7425
4752
and maximum out of these is 7425
Given two numbers A and B, write a code to compute the numbers woven by A &B and find the maximum woven number.
Input Format:
First line contains the number A
Next line contains the number B
Output Format:
Print the maximum woven number

Note: Input and Output may be as large as 10^10 choose appropriate data type

Minimum Multiple

Given a collection C1 of ‘n’ positive integers and a number ‘m’ write a C program to find the minimum multiple of m in C1. If no such multiple exist in C1 then print ‘No multiple found’
For example, if there are seven elements 23, 24, 25, 12, 6, 7, 11 and m is 3 then the output should be 6.
Input Format
First line contains the number of elements in the collection C1, n
Next ‘n’ lines contain the elements in the collection C1
Next line contains the value of m
Output Format
Print the minimum multiple of ‘m’ present in C1 or ‘No multiple found’

CODE::

n=int(input())
c=[]
for i in range(n):
    a=int(input())
    c.append(a)
m=int(input())
c.sort()
count=0
for i in c:
    if i%m==0:
        print(i)
        count=1
        break
if count==0:

    print("No multiple found")

Wednesday, October 16, 2019


Win in a Board Game!!
In a board game, a pXp board is given and the cells of the board are numbered as follows.

3,0
3,1
3,2
3,3
2,0
2,1
2,2
2,3
1,0
1,1
1,2
1,3
0,0
0,1
0,2
0,3
The movement of the coin in the board allowed are left, right, up and down. The coin movements are defined in the below table:
Movement
Current Position
Position after move
Left
m, n
m, n-1 if n>0
m, n otherwise
Right
m, n
m, n+1 if n<p-1
m, n otherwise
Up
m, n
m+1, n if m<p-1
m, n otherwise
Down
m, n
m-1, n if m>0
m, n otherwise
Given a size the board, p, initial cell of the coin in the board, a list of movements and a winning cell in the board, write a code to check if the coin reaches the winning cell from initial cell by these movements. If the winning position is reached then print Win and print Loss otherwise.
Input Format
Dimension of the board, p
Initial cell of the coin m,n separated by a space
Number of moves, num1
Next num1 lines contain the movements l-left, r-right, u-up and d-down
Winning cell of the coin m1, n1 separated by a space
Output Format
Print Win or Loss

CODE::
p=int(input())
y,x=map(int,input().split())
n=int(input())
for i in range(n):
    ch=input()
    if ch=='l':
        if x>0:
            x-=1
    elif ch=='r':
        if x<p-1:
            x+=1
    elif ch=='u':
        if y<p-1:
            y+=1
    elif ch=='d':
        if y>0:
            y-=1
y1,x1=map(int,input().split())
if y==y1 and x==x1:
    print("Win")
else:
    print("Loss")

Wednesday, September 18, 2019

15-09-2019


Reverse Length Divisible


Given a number N,  check if it is reverse length divisible. A number is said to be reverse length divisible if the first i digits of the number is divisible by (l-i-1) where l is the number of digits in N and 0 < i <= l. 
For example, 52267 is reverse length divisible because:
5 is divisible by 5
52 is divisible by 4
522 is divisible by 3
5226 is divisible by 2
52267 is divisible by 1.
43268 is not reverse length divisible. Print Yes if the number is reverse length divisible and no otherwise.
Note:
  • It is easy to solve this problem by using strings, but if you want some challenge, solve this problem without strings or any library functions.
  • Be cautious about choosing the data types and the boundary conditions
Boundary Conditions
0<n<10000000
Input Format
The first line contains the number N
Output Format
Print "Yes" if the number is reverse length divisible otherwise print "No" (without quotes).



CODE::

a=input()
b=len(a)
count=0
for i in range(1,b+1):
    if int(a[0:i])%(b-i+1)==0:
        count+=1
if count==b:
    print("Yes")
else:
    print("No")

    

18-09-2019

Secondary Factorial

Factorial of an integer n,  denoted as n! is defined as the product of the first n natural numbers
n! = 1*2*3....*n
1! = 1 and 0! = 1
We define a secondary factorial of a number n, denoted by SF(n), as follows:
SF(n) = 1*3*5*....*n, if n is odd and
SF(n) = 2*4*6*....*n if n is even
If n is an odd number, SF(n) is defined as the product of all the odd numbers, starting from 1, till the number n. SF(5)= 1*3*5= 15.
If n is an even number, SF(n) is defined as the product of all the even numbers, starting from 2, till the number n. SF(6)=2*4*6=48.
Given a number k, write a code to compute SF(n), where k = n!.  For the given number k, If there is no number n such that  n! = k then, your code should print -1.
Illstration
Given k = 24 then 24 = 4! and
SF(4) = 2* 4 = 8.
Given k=25, there is no number n such that 25 = n!, then the out put should be -1.
Given k=6, 6=3!. SF(3)=1*3=3
Input Format
First line contains an integer, k
Ouput Format
Print SF(n) if there exists a number  n, such that k = n! and -1 otherwise

CODE::
k=int(input())
import sys
f=1
num=-1
ans=1
for i in range(1,k):
    f*=i
    if f==k:
        num=i
        break
if num==-1:
    print(-1)
    sys.exit()
elif num%2==0:
    for i in range(2,num+1,2):
        ans*=i
    print(ans)
else:
    for i in range(1,num+1,2):
        ans*=i
    print(ans)