Skip to main content

Adjacency List in a Graph

   from collections import defaultdict graph=defaultdict(list) v,e=map(int,input().split()) for i in range(e):     u,v=map(str,input().split())     graph[u].append(v)     graph[v].append(u)      for i in graph:     print(i,"->",graph[i]) Input: 7 9 A B A C A F C E C F C D D E D G G F Output: F -> ['A', 'C', 'G'] C -> ['A', 'E', 'F', 'D'] G -> ['D', 'F'] D -> ['C', 'E', 'G'] A -> ['B', 'C', 'F'] E -> ['C', 'D'] B -> ['A']       

Repeat and Missing Number Array Asked in: Amazon

 You are given a read-only array of n integers from 1 to n.

Each integer appears exactly once except A which appears twice and B which is missing.

Return A and B.

Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Note that in your output A should precede B.

Example:

Input:[3 1 2 5 3] 

Output:[3, 4] 

A = 3, B = 4
Solution:

class Solution:
    # @param A : tuple of integers
    # @return a list of integers
    def repeatedNumber(self, A):
        d={}
        
        for i in A:
            if i in d:
                d[i]+=1
            else:
                d[i]=1
        for i in range(1,len(A)+1):
            if i not in d:
                d[i]=0
                
        return [max(d,key=lambda x:d[x]),min(d,key=lambda x:d[x])]
        
     
        

  Screenshot :

  


 



Comments

Popular posts from this blog

Trouble with the Number System

  Barua has developed his own Operating System known as "Barua OS" ( BOS ). One day while booting up his system he runs into a bug. You want to impress Barua so you jump in and offer to solve the bug yourself. Barua doesn't like binary numbers very much and his operating system uses a new number system called BNS ( Barua Number System ). The following are the properties of a number represented in BNS form : 1.       The number can only be made up of 2 distinct digits, one or zero. 2.       The number cannot start with zero. 3.       The number can have any number of zeroes, but only one instance of the digit one. For example 100, 1000, 10000 are Barua Numbers whereas 101, 502, 625 are not Barua Numbers. Unfortunately one decimal number has crept into a list of Barua Numbers and Barua OS cannot find its product. Can you? Input Format: First line contains an integer N, total number of elements in the list. Next N lines contains a number a[i] <= 10^18. N

Residue Arithmetic

  For operators with large integers, residue arithmetic is used. A residue of a number n modulo a prime p is defined as the remainder obtained when we divide a by p. For example, if the residue of 100 modulo 3 is 1. Two large primes are chosen and all numbers are expressed as modulo those primes. For example, if 7 and 3 were chosen as the primes, 25 would be represented as 4,1 in this representation, 3 would be represented as 3,3 and 30 would be represented as 2,0. Now if we do 25 * 3 - 30 in this model, we do the operation and compute the residue of the result with each of the primes in turn. SO with respect to prime 7, the result is 4 * 3 - 2 or 10 which has a residue of 3 modulo 7. With respect to 13, the operation is 12 * 3 - 4 = 32 whose residue is 6 modulo 13. Hence the result is 3,6 in the residue notation. There is only one number less than 91 which has this representation, and that is 45, which is what we expect. In this problem, we will be given 3 numbers a,b and c in residue

Perfect Peak of Array

Problem Description Given an integer array  A  of size  N . You need to check that whether there exists an element which is  strictly greater than all the elements on the left  of it and  strictly smaller than all the elements  on the right of it. If it exists return  1  else return  0 . NOTE: Do not consider the corner elements i.e  A[0] and A[N-1]  as the answer. Problem Constraints 3 <= N <= 10 5 1 <= A[i] <= 10 9 Input Format First and only argument is an integer array  A  containing  N  integers. Output Format Return  1  if there exist a element that is  strictly greater than all the elements on left  of it and  strictly smaller than all the elements  on right of it else return  0. Example Input Input 1: A = [5, 1, 4, 3, 6, 8, 10, 7, 9] Input 2: A = [5, 1, 4, 4] Example Output Output 1: 1 Output 2: 0 Example Explanation Explanation 1: A[4] = 6 is the element we are looking for. All elements on left of A[4] are smaller than it and all elements on right are greater