Colorful Flowers Count
Colorful Flowers Count
PROBLEM STATEMENT :
In a garden, there are N flower plants in a row. There are three types of plants which are given below.
Type 1: It gives only a red flower every day.
Type 2: It gives only a green flower every 2 days.
Type 3: It gives only a blue flower every 3 days.
Initially, there are no flowers in the N plants. The program must accept N characters denoting the colors of the N plants and N integers denoting the maximum number of flowers that the plants can give. The program must print the total number of flowers in each color that the gardener can get in T days. The value of T is also passed as the input.
Boundary Condition(s):
1 <= N <= 100
1 <= Each integer value <= 1000
2 <= T <= 1000
Input Format:
The first line contains N.
The second line contains N characters separated by a space.
The third line contains N integer values separated by a space.
The fourth line contains T.
Output Format:
The first line contains the total number of flowers in each color that the gardener can get in T days.
Example Input/Output 1:
Input: ()
4
R G R B
10 5 5 2
7
Output:
12 3 2
Explanation:
Day 1: The gardener gets 2 red flowers.
Day 2: The gardener gets 2 red flowers and 1 green flower.
Day 3: The gardener gets 2 red flowers and 1 blue flower.
Day 4: The gardener gets 2 red flowers and 1 green flower.
Day 5: The gardener gets 2 red flowers.
Day 6: The gardener gets 1 red flower, 1 green flower, and 1 blue flower.
Day 7: The gardener gets 1 red flower.
The total number of red flowers = 12.
The total number of green flowers = 3.
The total number of blue flowers = 2.
Hence the output is 12 3 2
Example Input/Output 2:
Input: ()
7
B G B B B G B
8 2 5 9 1 3 9
10
Output:
0 5 13
Example Input/Output 3:
Input: ()
8
B B B B B B B B
1 7 4 8 8 1 10 9
13
Output:
0 0 26
1) LEARN THRICE
👇
2) THINK TWICE
👇
3) APPLY ONCE
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
scanf("%d",&num);
char l1[num];
for(int i=0;i<num;i++){
scanf(" %c",&l1[i]);
}
int l2[num];
for(int i=0;i<num;i++){
scanf("%d",&l2[i]);
}
int x;
scanf("%d",&x);
int r=0,g=0,b=0;
for(int i=0;i<num;i++){
if (l1[i]=='R'){
if (l2[i]<x) r=r+l2[i];
else r=r+x;
}
else if (l1[i]=='G'){
if(l2[i]<x/2) g=g+l2[i];
else g=g+x/2;
}
else{
if(l2[i]<x/3) b=b+l2[i];
else b=b+x/3;
}
}
printf("%d %d %d",r,g,b);
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int num;
cin>>num;
char l1[num];
for(int i=0;i<num;i++){
cin>>l1[i];
}
int l2[num];
for(int i=0;i<num;i++){
cin>>l2[i];
}
int x;
cin>>x;
int r=0,g=0,b=0;
for(int i=0;i<num;i++){
if (l1[i]=='R'){
r+=min(l2[i],x);
}
else if (l1[i]=='G'){
g+=min(l2[i],x/2);
}
else{
b+=min(l2[i],x/3);
}
}
printf("%d %d %d",r,g,b);
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),arr[]=new int[n],t,r=0,g=0,b=0;
String s[]=sc.nextLine().split(" ");
for (int i=0;i<n;i++)
arr[i]=sc.nextInt();
t=sc.nextInt();
for(int i=0;i<n;i++) {
if (s[i].equals("R"))
r+=Math.min(arr[i],t);
else if(s[i].equals("G"))
g+=Math.min(arr[i],t/2);
else
b+=Math.min(arr[i],t/3);
}
System.out.print(r+" "+g+" "+b);
}
}
PYTHON
n=int(input())
ch=input().strip().split()
l=list(map(int,input().split()))
t=int(input())
red=0;green=0;blue=0
for i in range(n):
if ch[i]=='R':
red+=min(t,l[i])
if ch[i]=='G':
green+=min(t//2,l[i])
if ch[i]=='B':
blue+=min(t//3,l[i])
print(red,green,blue)

Comments
Post a Comment