Keeping a Secret On the first day, one person discovers a secret. You are given two…
- User
Keeping a Secret On the first day, one person discovers a secret.
You are given two integers, delay and forget. Each person will share the secret with a new person every day, starting from delay days after discovering the secret. Additionally, each person will forget the secret days after discovering it. It means they cannot share the secret on the same day they forget it or on any day afterward.
Your task is to find the number of people who know the secret at the end of day n.
Important Note: Ensure that you save your solution before progressing to the next question and before submitting your answer.
Example
INPUT 6 2 4
Note: Line 1 : number of days secret get spread Line 2: delay day Line 3 : forgetting day
OUTPUT 5 Exercise-1
Input : 4 1 3
Output: 6 Exercise-2
Input: 3 2 3
Output: 2
- Assistant
- User
import java.util.Scanner;
public class Main {
codepublic static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); // number of days secret get spread int delay = input.nextInt(); // delay day int forget = input.nextInt(); // forgetting day input.close(); System.out.println(findPeople(n, delay, forget)); } public static int findPeople(int n, int delay, int forget) { int people = 0; // Initially, no one knows the secret // Array to keep track of people who know the secret on each day int[] knownAtDay = new int[n + 1]; knownAtDay[delay] = 1; // First person knows the secret on the delay day // Iterate through each day for (int day = delay; day <= n; day++) { // Share the secret with a new person people += knownAtDay[day]; // Check if someone forgets the secret on this day if (day - forget >= 0) { // Subtract the number of people who forget the secret people -= knownAtDay[day - forget]; } // Store the number of people who know the secret on this day if (day < n) knownAtDay[day + 1] += knownAtDay[day]; } return people; } - Assistant
- User
4 1 3 this is giving output 3 you should get 6 as output
- Assistant