
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to find tree height after n days
Suppose we have an array A with n elements. A has elements either 0 or 1. There is a tree. In consecutive n days, if A[i] is 0 it is not watered, if it is 1 then it is watered, the flower grows in the following manner −
If the tree is not watered for consecutive two days, it dies
If the tree is watered on ith day, it grows 1 cm
If the tree is watered on ith and (i+1)th day consecutively, it grows 5 cm instead of 1 cm.
If it is not watered on ith day, it will not grow.
At the beginning, the tree is 1cm long. We have to find the height of the tree after n days. If it dies, return -1.
So, if the input is like A = [0, 1, 1], then the output will be 7, because on the first day, it will not grow, so height is 1, after second day, the height will be 2, then after third day it will be 2 + 5 = 7.
Steps
To solve this, we will follow these steps −
r := 1 y := 0 n := size of A for initialize i := 0, when i < n, update (increase i by 1), do: x := A[i] if r is same as -1, then: Ignore following part, skip to the next iteration if x is non-zero and y is non-zero, then: r := r + 5 otherwise when x is non-zero, then: (increase r by 1) otherwise when not x is non-zero and not y is non-zero and i > 0, then: r := -1 y := x return r
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int r = 1; int y = 0; int n = A.size(); for (int i = 0; i < n; ++i){ int x = A[i]; if (r == -1) continue; if (x && y) r += 5; else if (x) ++r; else if (!x && !y && i > 0) r = -1; y = x; } return r; } int main(){ vector<int> A = { 0, 1, 1 }; cout << solve(A) << endl; }
Input
{ 0, 1, 1 }
Output
7
- Related Articles
- Prison Cells After N Days in C++
- C++ code to find minimal tiredness after meeting
- C++ code to find xth element after removing numbers
- C++ code to find minimum stones after all operations
- C++ code to find position of students after coding contest
- C++ code to find money after buying and selling shares
- C++ code to find corrected text after double vowel removal
- C++ code to count days to complete reading book
- How to find the date after a number of days in R?
- Program to find state of prison cells after k days in python
- C++ code to find final number after min max removal game
- C++ code to find screen size with n pixels
- C++ code to find minimum different digits to represent n
- C++ code to find three numbers whose sum is n
- C++ code to find composite numbers whose difference is n
