Poisonous Plants

Problem Statement :

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies.

You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plant with more pesticide content than the plant to its left.


p = [ 3, 6, 2, 7, 5  ] // pesticide levels

Use a 1-indexed array. On day  1, plants  2 and 4 die leaving p' = [ 3, 2, 5 ] . On day ,2, plant 3  in p'  dies leaving p^n  = [ 3, 2 ]. There is no plant with a higher concentration of pesticide than the one to its left, so plants stop dying after day 2.

Function Description
Complete the function poisonousPlants in the editor below.

poisonousPlants has the following parameter(s):

int p[n]: the pesticide levels in each plant
- int: the number of days until plants no longer die from pesticide

Input Format

The first line contains an integer n, the size of the array p.
The next line contains n  space-separated integers p[ i ] .

Solution :


                            Solution in C :

In C++ :

#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i,n) for(in i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i)
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long in;
typedef vector<in> VI;
typedef vector<VI> VVI;
in dct=0;
map<in,in> mar;
set<in> td;
void proc(in id){
  auto it=mar.find(id);
  auto it2=it;
  if(it2!=mar.end() && it2!=mar.begin()){
VI otd;
int main(){
  in n;
  in ta;
    if(i>0 && mar[i]>mar[i-1])
  return 0;

In Java :

import java.util.Scanner;

public class Solution {
    private Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Solution().solve();

    private void solve() {
        int n = sc.nextInt();

        int[] p = new int[n];
        for (int i = 0; i < n; ++i) {
            p[i] = sc.nextInt();

        int min = p[0];
        int maxDays = 0;
        for (int i = 1; i < n; ++i) {
            min = Math.min(min, p[i]);
            if (p[i] > p[i - 1]) {
                int last = p[i];
                int k = i + 1;
                int days = 1;
                while (k < n && min < p[k]) {
                    if (p[k] <= last) {
                        last = p[k];

                maxDays = Math.max(maxDays, days);


In C :

int main(){
    long int n,i,j,min=0,locmin;
    long long int *p=(long long int *)malloc(sizeof(long long int)*n);
               if(j<n && p[j]>p[i]){
                              while(j<n && (p[j]>p[i] || p[j]<0)){
                              locmin = p[j];
                              //p[j] = -1;
    printf("%ld ",-min);
    return 0;

In Python3 :

class plant:
    def __init__(self, pest):
        self.pest = pest
        self.prevkiller = self.nextkiller = None
    __slots__ = 'pest,next,prevkiller,nextkiller'.split(',')
nplants = int(input())
plants = [int(pest) for pest in input().split()]
start = current = plant(plants[0])
curkiller = firstkiller = plant(None)
for pest in plants[1:]:
    current.next = newplant = plant(pest)
    if current.pest < newplant.pest:
            curkiller.nextkiller = current
            current.prevkiller = curkiller
            curkiller = current
    current = newplant
last = current.next = plant(-1)
last.prevkiller = curkiller
curkiller.nextkiller = last
day = 0
while last.prevkiller is not firstkiller:
    day += 1
    curkiller = last.prevkiller
    while curkiller is not firstkiller:
        victim = curkiller.next
        if not(hasattr(victim, "next")):
            print(victim.pest, day)
        curkiller.next = victim.next
        if victim.prevkiller == curkiller:
            curkiller.nextkiller = victim.nextkiller
            victim.nextkiller.prevkiller = curkiller
            victim.prevkiller = victim.nextkiller = None
        curkiller = curkiller.prevkiller

    curkiller = last.prevkiller
    while curkiller is not firstkiller:
        prevkiller = curkiller.prevkiller
        if curkiller.pest >= curkiller.next.pest:
            curkiller.nextkiller.prevkiller = prevkiller
            prevkiller.nextkiller = curkiller.nextkiller
            curkiller.prevkiller = curkiller.nextkiller = None
        curkiller = prevkiller

View More Similar Problems

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →