Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

PBT | DAILY TEST | 17 JULY | Programming

 

 1.

Problem statement
Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose
sum is equal to K.
Input Format
First line of input contains two space separate integers 'n' and 'k'. Next line of input contains n integers
- the elements of the array.
Constraints
1 <= N <= 105 1 <= K <= 108 1 <= Arr[i] <= 106
Output Format
Print the number of pairs that have sum K.

PYTHON:

def getPairsCount(arr, n, sum):
    count = 0
    for i in range(0, n):
        for j in range(i + 1, n):
            if arr[i] + arr[j] == sum:
                count += 1
    return count
n,s=map(int,input().split())
arr = list(map(int,input().split()))
print(getPairsCount(arr, n, s))

 

2.

Problem statement
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer
number x. For example, if x=4 and there were 3 keyboards in the store, then the devices had indices 4,
5 and 6, and if x=10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
Input Format
The first line contains single integer n — the number of keyboards in the store that remained after the
heist.
The second line contains n distinct integers a1,a2,…,an — the indices of the remaining keyboards. The
integers ai are given in arbitrary order and are pairwise distinct. After the heist, only n keyboards
remain, and they have indices a1,a2,…,an. Calculate the minimum possible number of keyboards that
have been stolen. The staff remember neither x nor the number of keyboards in the store before the
heist.
Constraints
(1≤n≤1000)
(1≤ai≤10^9)
Output Format
Print the minimum possible number of keyboards that have been stolen if the staff remember neither x
nor the number of keyboards in the store before the heist

PYTHON:

n=int(input())
a=[*map(int,input().split())]
print(max(a)-min(a)-n+1)

 

3.

Problem statement
During the lunch break all n Berland State University students lined up in the food court. However, it
turned out that the food court, too, has a lunch break and it temporarily stopped working.
Standing in a queue that isn't being served is so boring! So, each of the students wrote down the
number of the student ID of the student that stands in line directly in front of him, and the student that
stands in line directly behind him. If no one stands before or after a student (that is, he is the first one
or the last one), then he writes down number 0 instead (in Berland State University student IDs are
numerated from 1).
After that, all the students went about their business. When they returned, they found out that
restoring the queue is not such an easy task.
Help the students to restore the state of the queue by the numbers of the student ID's of their
neighbors in the queue.
Input Format
The first line contains integer n— the number of students in the queue.
Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number
of a person in front of a student and bi is the ID number of a person behind a student. The lines are
given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't
exist.
The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue
where all the students stand in some order.
Constraints
(2 ≤ n ≤ 2·10^5)
Output Format
Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the orde 

PYTHON:

n=int(input())
d,d1={},{}
for i in range(n):
  a,b=map(int,input().split())
  d[a],d1[b]=b,a
r = [list(filter(lambda f:d1.get(f)==None, d.keys()))[0], d.get(0)]
for i in range(2,n):
  r.append(d.get(r[i-2]))
print(*r)

Post a Comment

Previous Post Next Post