Using HC-SRO4 sensor and looking for obstacle
There's not much code there, so not much to optimize. I will however show you how to combine your two ifs.

You have:

if(mappedValue&gt;=distance)//checking if the object is in range
    Serial.println("YES");
  else
    Serial.println("NO");
  if(mappedValue&gt;=distance)
    digitalWrite(11,HIGH);
  else
    digitalWrite(11,LOW);


and I guess you tried this:

if(mappedValue&gt;=distance)//checking if the object is in range
    Serial.println("YES");
    digitalWrite(11,HIGH);
  else
    Serial.println("NO");
    digitalWrite(11,LOW);


That would be correct in Python where the indenting matters (oh how that brings back the PTSD of having to work with COBOL...) but not in C.  In C indenting is ignored completely. It's only there for our benefit in reading the flow of code.

Instead to group statements together within an if or an else (or any other conditional or loop structure) you wrap those statements in a block. That is, place { before it and } after it.  Thus:

if(mappedValue&gt;=distance)//checking if the object is in range
{
    Serial.println("YES");
    digitalWrite(11,HIGH);
}
  else
{
    Serial.println("NO");
    digitalWrite(11,LOW);
}


Or with my preferred formatting:

if(mappedValue&gt;=distance) { //checking if the object is in range
    Serial.println("YES");
    digitalWrite(11,HIGH);
} else {
    Serial.println("NO");
    digitalWrite(11,LOW);
}