How to print the longest time that the alarm sounded for during time period?

Is there a way that I can print the longest time that the alarm sounded for during the monitoring period? I would like for the longest event to print every second like the other data. While a newbie and with the help of others I was able to cobble together how to report the # of times the alarm sounded for during the monitoring period, the total time the alarm sounded for and the average amount of time the alarm sounded for each time it was triggered. I am stumped on how to print how long the longest alarm sounded for during the monitoring period. Any help would be appreciated.

int sensorPin = A0; // input pin for the analog sensor
int alarmPin = 13; // pin for the piezo alarm
unsigned int sensorValue = 0; // stores value coming from the sensor
unsigned int alarmCount = 0;
unsigned long duration = 0;
unsigned int average = 0;
int alarmState=LOW;
int lastAlarmState=LOW;
unsigned long time;

unsigned long switchoff = 0;
unsigned int threshold = 512;
long startTime;
long alarmDuration;

void setup(){
pinMode(alarmPin, OUTPUT);
Serial.begin(9600);
alarmState=digitalRead(13);
}

void loop() {
if(alarmState==HIGH && lastAlarmState==LOW) {
alarmCount++;
}
lastAlarmState=alarmState;
alarmState=digitalRead(13);
sensorValue = analogRead(A0);
Serial.println(sensorValue);
Serial.println(alarmCount);
Serial.println(duration/1000);
Serial.println(average = (duration/alarmCount)/1000);
delay(250);

if(sensorValue > threshold) {
digitalWrite(alarmPin, HIGH); // Set off alarm
switchoff = millis()+4000; // keep alarmPin HIGH for a minimum of millis
}
if (millis() > switchoff && switchoff > 0) { // turn alarmPin off after minimum of millis and if threshold is no longer exceeded
digitalWrite(alarmPin, LOW);
switchoff = 0;
}
if(alarmState == HIGH) {
duration = millis(); // total time alarm was on
}

}

Add a variable, long longestAlarmDuration, and every time you set alarmDuration check to see if longestAlarmDuration needs to be reset?

if(alarmState==HIGH && lastAlarmState==LOW) {
  alarmCount++;
  } 
  lastAlarmState=alarmState;
  alarmState=digitalRead(13);

First, do something with alarmState and lastAlarmState, and then assign them meaningful values. Why?

  if(alarmState == HIGH) {
  duration = millis();   // total time alarm was on
  }

What is being assigned to duration() is not the total time the alarm has been on. It is "now". If you are going to put in comments like this, make them accurate.

You really need to set a variable, like alarmStarted, when you discover that the alarm is on but was off last time, and set a variable, like alarmEnded, when you discover that the alarm is off but was on last time. The duration value is then the difference, and is computed once, when the alarm goes off.