Cut Down & Plant - Trees
Coding Problem Keys
Cut Down & Plant - Trees
Problem Statement
In a garden, there are N trees from left to right. The height of each tree is passed as input to the program. The gardener wants to cut down T trees completely from left to right, but if he cuts a tree of height H, then he must plant certain number of trees based on the following conditions.
The value of T is also passed as the input to the program. The program must print the number of trees remaining in the garden after cutting down T trees as the output.
Boundary Condition(s)
Input Format
Output Format
Example Input/Output 1
Input
Output
Explanation
Example Input/Output 2
Input
Output
Note: Max Execution Time Limit: 50 millisecs
Solution
Programming Language: Python 3 Language
n=int(input()) l=list(map(int,input().split())) t=int(input()) for i in range(t): a=l[0] l=l[1:] if a%2: l+=list(range(1,a+1)) else: l+=list(range(a//2,0,-1)) print(len(l))
#Published By PKJCODERSAlter
n,l=int(input()),[*map(int,input().split())] t=int(input()) for i in range(t): h=l.pop(0) if h&1: l+=[*range(1,h+1)] else: l+=[*range(h//2,0,-1)] print(len(l))
#Published By PKJCODERSProgramming Language: C Language
//Published By PKJCODERS#include<stdio.h> #include<stdlib.h> int main(){ int n,t,a[100000],i,j,k; scanf("%d\n",&n); for(i=0;i<n;i++) scanf("%d ",&a[i]); scanf("%d",&t); for(i=0;i<t;i++){ if(a[i]%2!=0) for(k=1;k<=a[i];k++) a[n++]=k; else for(k=a[i]/2;k>=1;k--) a[n++]=k; }printf("%d",n-t); }
Programming Language: C++ Language
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv){ int i,j,n,t,a[100001]; cin>>n; for(i=0;n>i;i=i+1) cin>>a[i]; cin>>t; for(i=0;i<t;i=i+1){ if(0!=a[i]%2) for(j=1;j<=a[i];j=j+1) a[n++]=j; else for(j=a[i]/2;j>=1;j=j-1) a[n++]=j; }cout<<n-t; }
//Published By PKJCODERSProgramming Language: Java Language
import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); List<Integer>li=new ArrayList<Integer>(); for(int i=0;i<n;i++) li.add(sc.nextInt()); int t=sc.nextInt(); while(t!=0){ int val=li.get(0); li.remove(0); if(val%2!=0){ for(int i=1;i<=val;i++) li.add(i); }else{ for(int i=val/2;i>=1;i--) li.add(i); }t--; }System.out.print(li.size()); } }
//Published By PKJCODERSAlter
import java.util.*; public class Hello { public static void main(String[] args){ Scanner scan=new Scanner(System.in); int n=scan.nextInt(); ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<n;i++) ar.add(scan.nextInt()); int t=scan.nextInt(); for(int j=0;j<t;j++){ int h=ar.get(0); if(h%2!=0){ for(int i=1;i<=h;i++) ar.add(i); ar.remove(0); }else{ for(int i=h/2;i>=1;i--) ar.add(i); ar.remove(0); } }System.out.print(ar.size()); } }
//Published By PKJCODERS(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments