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.

    - If H is odd, then he must plant H trees of height ranging from 1 to H on the rightmost end.
    - If H is even, then he must plant H/2 trees of height ranging from H/2 to 1 on the rightmost end.

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) 

1 <= N <= 100.
1 <= Height of each tree <= 50.
1 <= T <= 1000.

 Input Format 

The first line contains N.
The second line contains N integer values separated by a space.
The third line contains T.

 Output Format 

The first line contains the number of trees remaining in the garden after cutting down T trees.

 Example Input/Output 1 

 Input 

4
4 3 5 2
6

 Output 

11

 Explanation 

After cutting down the 1st tree, the trees in the garden becomes 3 5 2 2 1.
After cutting down the 2nd tree, the trees in the garden becomes 5 2 2 1 1 2 3.
After cutting down the 3rd tree, the trees in the garden becomes 2 2 1 1 2 3 1 2 3 4 5.
After cutting down the 4th tree, the trees in the garden becomes 2 1 1 2 3 1 2 3 4 5 1.
After cutting down the 5th tree, the trees in the garden becomes 1 1 2 3 1 2 3 4 5 1 1.
After cutting down the 6th tree, the trees in the garden becomes 1 2 3 1 2 3 4 5 1 1 1.
Now 11 trees remaining in the garden, so 11 is printed as the output.

 Example Input/Output 2 

 Input 

7
6 4 2 2 5 13 4
10

 Output 

29

 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 PKJCODERS

 Alter 

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 PKJCODERS

 Programming Language: C Language 

#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);
}
//Published By PKJCODERS

 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 PKJCODERS

 Programming 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 PKJCODERS

 Alter 

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

Popular Posts

Shooting Targets

Four Strings Rectangle

String Pattern - Inverted U

Swap and Reverse the String

Same Unit Digit Adjacent Numbers

Archery Game