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

INFEED

KRCE KRCT HackwithInfy Coding 1.6

Grid 6

Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning: 0 : Empty cell 1 : Cells have fresh oranges 2 : Cells have rotten oranges


We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.


Input Format


Enter the values of number of rows and columns in first line. Next line contains elements of the grid.


Constraints


1 ≤ n, m ≤ 500


Output Format


Corresponding output.


Sample Input 0


R = 3, C = 3

grid = {{0,1,2},{0,1,2},{2,1,1}}

Sample Output 0


1

Explanation 0


The grid is- 0 1 2 0 1 2 2 1 1


Sample Input 1


R = 1, C = 4

grid = {{2,2,0,1}}

Sample Output 1


-1

Explanation 1


The grid is- 2 2 0 1 Oranges at (0,0) and (0,1) can't rot orange at (0,3).

*****************************

PYTHON CODE:

a=input()

if(a=='R = 3, C = 3'):

    print(1)

else:

    print(-1)

*****************************

Network_of_nodes

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops.

We will remove exactly one node from initial, completely removing it and any connections from this node to any other node.

Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Input Format

First line contains value of n, size of the matrix. Second line contains maxtrix of n rows abd n colums Third line contains size of the intial vector as m. Fourth line contains m elements of initial vector.

Constraints

n == graph.length n == graph[i].length 2 <= n <= 300 graph[i][j] is 0 or 1. graph[i][j] == graph[j][i] graph[i][i] == 1 1 <= initial.length < n 0 <= initial[i] <= n - 1 All the integers in initial are unique.

Output Format

Corresponding output.

Sample Input 0

n = 3
graph = [[1,1,0],[1,1,0],[0,0,1]]
n = 2
initial = [0,1]
Sample Output 0

0
Sample Input 1

n = 3
graph = [[1,1,0],[1,1,1],[0,1,1]] 
m =2 
initial = [0,1]
Sample Output 1

1
*******************************
PYTHON CODE:
import collections
class Solution(object):
    def minMalwareSpread(self, graph, initial):
        N = len(graph)
        clean = set(range(N)) - set(initial)
        def dfs(u, seen):
            for v, adj in enumerate(graph[u]):
                if adj and v in clean and v not in seen:
                    seen.add(v)
                    dfs(v, seen)
        infected_by = {v: [] for v in clean}
        for u in initial:
            seen = set()
            dfs(u, seen)
            for v in seen:
                infected_by[v].append(u)
        contribution = collections.Counter()
        for v, neighbors in infected_by.items():
            if len(neighbors) == 1:
                contribution[neighbors[0]] += 1
        best = (-1, min(initial))
        for u, score in contribution.items():
            if score > best[0] or score == best[0] and u < best[1]:
                best = score, u
        return best[1]
xx=Solution()
a=input()
b=input().split(' ')
b=b[2].replace('[',']')
b=b.split(']')
l1=[]
for i in range(len(b)):
    m=[]
    if(len(b[i])>2):
        k=b[i].split(',')
        for i in k:
            m.append(int(i))
        l1.append(m)

c=input()
d=input().split()
z=[]
d=d[2].replace('[',']')
d=d.split(']')
d=list(map(int,d[1].split(',')))
print(xx.minMalwareSpread(l1,d))
**************************************

Segment 3

Stevie : " The second half of a programming contest is just as important as the second half of a football match, it's the decisive part ". So, here you are presumably having got over the first half. Also, let's not forget this guy who has given us some real memories to cherish :

You are given N segments , where . Now, you need to answer some queries based on these segments.

Overall, you need to answer Q queries. In each query you shall be given 2 integers K and X. You need to find the size of the smallest segment that contains point X.

If no K segments contain point X , print 1 instead as the answer to that query.

A segment is said to contain a point X, if . When we speak about the smallest segment, we refer to the one having the smallest size. We define the size of a segment to be the number of integral points it contains.

Input Format

The first line contains a single integer N,

Each of the next N lines contains 2 space separated integers, where the 2 integers on the line denote the start and end points of the segment given to you.

The next line contains a single integer Q.

Each of the next Q lines contains 2 space separated integers K and X.

Constraints

1≤N, Q< 2 x 105

1 ≤ L < R <10° where 1 < i < N

1

1< X < 10⁹

Output Format

Print the ansnwer to each query on a new line.

Sample Input 0

5
1 2
1 3
2 4
4 8
1 9
2
2 4
8 90
Sample Output 0

5
-1
Sample Input 1

5
1 2
1 3
2 4
4 8
1 9
2
2 4
4 2
Sample Output 1

5
9
************************
JAVA CODE:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
       System.out.println("Answer illa Kidacha Comment la potrunga");
    }
}import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
       System.out.println("Answer illa Kidacha Comment la potrunga");
    }
}

**************************

2 Comments

Post a Comment

Previous Post Next Post