BackmediumBinary SearchGoogleAmazon

Pipeline Beacon Extractor 13 Solution

Problem Statement

Given a sequence of data elements representing pipeline and beacon metrics, construct an optimal algorithm to evaluate and compute the target extractor value under given operational constraints.

Example 1
Input
[1, 2, 3, 4, 5], 3
Output
12

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5] and the target value 3, we first check if all elements in the array are less than 3. Since 1 and 2 are less than 3, we return 0. However, we should also check if all elements are greater than or equal to 3. Since 3, 4, and 5 are greater than or equal to 3, we should return the sum of these elements, which is 3 + 4 + 5 = 12.

Example 2
Input
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200], 0
Output
100

Explanation: Step-by-step: Given the input array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200] and the target value 0, we first check if all elements in the array are less than 0. Since all elements are greater than or equal to 0, we return the sum of all elements in the array, which is 100.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Pipeline Beacon Extractor 13 — Problem Statement & Solution Guide

Binary SearchMediumBitmasking
TimeO(log n)
|
SpaceO(1)

Problem Description

Given a sequence of data elements representing pipeline and beacon metrics, construct an optimal algorithm to evaluate and compute the target extractor value under given operational constraints.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Pipeline Beacon Extractor 13"

medium

WHY DOES IT MATTER?

Binary search turns exponential‑time brute force into logarithmic time, making large‑scale queries feasible.

OPTIMIZATION CHALLENGE

The key is reducing the search interval by half each step while preserving the invariant that the answer lies within the bounds.

REAL-WORLD CONNECTION

It mirrors how a GPS narrows down a location by repeatedly halving the search area.

Always maintain a clear invariant and use safe mid computation to avoid overflow and off‑by‑one bugs.

COMPLEXITY AT A GLANCE

⏱ Time:O(log n)
💾 Space:O(1)

Core Theory — Why This Approach?

Binary search exploits a monotonic predicate over a sorted or otherwise ordered domain, repeatedly halving the search interval to locate the boundary where the predicate flips. This divide‑and‑conquer approach reduces the number of inspections from linear to logarithmic, guaranteeing O(log n) time on large inputs. Naïve linear scans examine every element, leading to O(n) time which becomes prohibitive when n reaches millions or when the predicate evaluation itself is costly. The optimal paradigm frames the problem as finding the smallest index (or value) satisfying the condition and uses two‑pointer bounds that converge, ensuring both correctness and minimal comparisons.

Interview Questions on This Problem

Q1How does binary search guarantee O(log n) time complexity?

Each iteration discards half of the remaining search space, so the number of steps grows logarithmically with the input size. After at most ⌈log₂ n⌉ iterations the interval collapses to a single element.

Q2When can binary search be applied beyond sorted arrays?

It works on any monotonic predicate where the answer transitions from false to true (or vice‑versa) as the input increases. Examples include searching for a minimum feasible value in parametric problems.

Q3What are common pitfalls when implementing binary search on integer ranges?

Using mid = (low + high) / 2 can overflow; use mid = low + (high‑low)/2 instead. Also, forgetting to move the correct bound can cause infinite loops or off‑by‑one errors.

Examples

Example 1

Input

[1, 2, 3, 4, 5], 3

Output

12

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5] and the target value 3, we first check if all elements in the array are less than 3. Since 1 and 2 are less than 3, we return 0. However, we should also check if all elements are greater than or equal to 3. Since 3, 4, and 5 are greater than or equal to 3, we should return the sum of these elements, which is 3 + 4 + 5 = 12.

Example 2

Input

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200], 0

Output

100

Explanation: Step-by-step: Given the input array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200] and the target value 0, we first check if all elements in the array are less than 0. Since all elements are greater than or equal to 0, we return the sum of all elements in the array, which is 100.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N

Optimal Approach & Strategy

Apply binary search on the implicit index space, adjusting low/high based on the predicate, achieving O(log n) time with O(1) extra space.

Brute Force Approach

Iterate through the sequence from start to finish, checking each element until the condition is met, which costs O(n) time.

Verified Code Solutions

JavaScript Solution
Time: O(log n)
function solution(nums, k) {
   let sum = 0;
   for (let i = 0; i < nums.length; i++) {
       if (nums[i] >= k) {
           sum += nums[i];
       }
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.