BackeasyBinary SearchGoogleAmazon

Network Network Optimizer 36 Solution

Problem Statement

Given a sequence of data elements representing network and network metrics, construct an optimal algorithm to evaluate and compute the target optimizer value under given operational constraints. The target optimizer value is the maximum value in the array that is less than or equal to the target value K.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36]
Output
30

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36] and target value K = 30, we perform a binary search to find the maximum value less than or equal to 30. The binary search process is as follows: 1. We start by comparing the middle element (15) with the target value (30). Since 15 is less than 30, we move to the right half of the array. 2. We repeat the process by comparing the middle element (30) with the target value (30). Since 30 is equal to 30, we return the previous element (29) as the maximum value less than or equal to 30.

Example 2
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36]
Output
29

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36] and target value K = 29, we perform a binary search to find the maximum value less than or equal to 29. The binary search process is as follows: 1. We start by comparing the middle element (15) with the target value (29). Since 15 is less than 29, we move to the right half of the array. 2. We repeat the process by comparing the middle element (29) with the target value (29). Since 29 is equal to 29, we return the previous element (28) as the maximum value less than or equal to 29.

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

Network Network Optimizer 36 — Problem Statement & Solution Guide

Binary SearchEasyFixed/Dynamic Window
TimeO(N)
|
SpaceO(1)

Problem Description

Given a sequence of data elements representing network and network metrics, construct an optimal algorithm to evaluate and compute the target optimizer value under given operational constraints. The target optimizer value is the maximum value in the array that is less than or equal to the target value K.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36]

Output

30

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36] and target value K = 30, we perform a binary search to find the maximum value less than or equal to 30. The binary search process is as follows: 1. We start by comparing the middle element (15) with the target value (30). Since 15 is less than 30, we move to the right half of the array. 2. We repeat the process by comparing the middle element (30) with the target value (30). Since 30 is equal to 30, we return the previous element (29) as the maximum value less than or equal to 30.

Example 2

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36]

Output

29

Explanation: Step-by-step: Given the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31, 32, 33, 34, 35, 36] and target value K = 29, we perform a binary search to find the maximum value less than or equal to 29. The binary search process is as follows: 1. We start by comparing the middle element (15) with the target value (29). Since 15 is less than 29, we move to the right half of the array. 2. We repeat the process by comparing the middle element (29) with the target value (29). Since 29 is equal to 29, we return the previous element (28) as the maximum value less than or equal to 29.

Constraints

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

Optimal Approach & Strategy

Use Fixed/Dynamic Window technique to process inputs in O(N) linear time.

Brute Force Approach

Check all possible combinations in O(N^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums, K) {
      let left = 0;
      let right = nums.length - 1;
      while (left <= right) {
         let mid = Math.floor((left + right) / 2);
         if (nums[mid] <= K) {
            if (mid === nums.length - 1 || nums[mid + 1] > K) {
               return nums[mid];
            }
            left = mid + 1;
         } else {
            right = mid - 1;
         }
      }
      return -1;
   }

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.