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']       

Max Non Negative SubArray

 Problem Description

Given an array of integers, A of length N, find out the maximum sum sub-array of non negative numbers from A.

The sub-array should be contiguous i.e., a sub-array created by choosing the second and fourth element and skipping the third element is invalid.

Maximum sub-array is defined in terms of the sum of the elements in the sub-array.

Find and return the required subarray.

NOTE:

  1. If there is a tie, then compare with segment's length and return segment which has maximum length.
  2. If there is still a tie, then return the segment with minimum starting index.



Problem Constraints

1 <= N <= 105
-109 <= A[i] <= 109



Input Format

The first and the only argument of input contains an integer array A, of length N.



Output Format

Return an array of integers, that is a subarray of A that satisfies the given conditions.



Example Input

Input 1:

 A = [1, 2, 5, -7, 2, 3]

Input 2:

 A = [10, -1, 2, 3, -4, 100]


Example Output

Output 1:

 [1, 2, 5]

Output 2:

 [100]


Example Explanation

Explanation 1:

 The two sub-arrays are [1, 2, 5] [2, 3].
 The answer is [1, 2, 5] as its sum is larger than [2, 3].

Explanation 2:

 The three sub-arrays are [10], [2, 3], [100].
 The answer is [100] as its sum is larger than the other two.


Solution:


class Solution:

    # @param A : list of integers

    # @return a list of integers

    def maxset(self, A):

        if len(A)==1:

            return A

        k=[]

        s=0

        d={}

        for i in range(len(A)):

            if A[i]>=0:

                k.append(A[i])

                s+=A[i]

                

            else:

                d[s]=k

                k=[]

                s=0

        for i,j in d.items():

            print(i,j)

        #k=d[max(d,key=d.get)]

        return []

                

            

                

        



 

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