Untitled
unknown
c_cpp
a year ago
662 B
5
Indexable
int closestToTarget(std::vector<int> oxygenLevels, int target) { int minDiff = std::abs(oxygenLevels[0] - target); // Initialize minimum difference with the first oxygen level int current = oxygenLevels[0]; // Initialize current value with the first oxygen level for (int i = 0; i < oxygenLevels.size(); ++i) { current &= oxygenLevels[i]; // Update current value by taking bitwise AND with the next oxygen level minDiff = std::min(minDiff, std::abs(current - target)); // Update minimum difference if (minDiff == 0) break; // If the difference becomes zero, no need to continue } return minDiff; }
Editor is loading...
Leave a Comment