BackeasyBinary SearchCognizantPaytm

Dynamic Target Index Solution

Problem Statement

Given an array of numerical values, compute the dynamic target index according to the target algorithm rules. The target index is the index of the number that is closest to the total sum of the array divided by 2.

Example 1
Input
[4, 7, 12, 3]
Output
1

Explanation: Step-by-step: First, we calculate the total sum of the array, which is 4 + 7 + 12 + 3 = 26. Then, we divide the total sum by 2, which gives us 13. The number 7 is closest to 13, so the dynamic target index is 1.

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

Explanation: Step-by-step: First, we calculate the total sum of the array, which is 1 + 2 + 3 + 4 = 10. Then, we divide the total sum by 2, which gives us 5. The number 3 is closest to 5, so the dynamic target index is 2.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(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

Dynamic Target Index — Problem Statement & Solution Guide

Binary SearchEasyMin Capacity Target
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of numerical values, compute the dynamic target index according to the target algorithm rules. The target index is the index of the number that is closest to the total sum of the array divided by 2.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Dynamic Target Index"

easy

WHY DOES IT MATTER?

This pattern demonstrates how a global property (the total sum) can be leveraged to solve a local optimization problem efficiently. It teaches candidates to separate preprocessing from evaluation, a common theme in algorithmic interviews.

OPTIMIZATION CHALLENGE

The key insight is that the target value depends only on the sum, not on individual elements. By computing the sum once and reusing it, you avoid recomputing or sorting, reducing time from O(n^2) or O(n log n) to O(n).

REAL-WORLD CONNECTION

In load‑balancing scenarios, you often compute the total workload and then assign tasks to servers such that each server’s load is as close as possible to the average. The algorithm mirrors that process: compute the average load and find the server whose current load is nearest to it.

When presenting this solution, emphasize the two‑pass structure and the constant‑space nature. Highlight that the algorithm is stable, handles negative numbers, and naturally resolves ties by first occurrence, which is often a requirement in interview questions.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The problem reduces to a simple linear scan once the total sum of the array is known. First compute the sum S of all elements; the target value is S/2. The goal is to find the element whose value is closest to this target, i.e., minimize |arr[i] – S/2|. A naive approach that recomputes the sum for each candidate or sorts the array would add unnecessary O(n) or O(n log n) overhead, making it unsuitable for large inputs. The optimal paradigm is a single pass: compute S in O(n), then iterate again to track the minimum absolute difference and its index, achieving O(n) time and O(1) extra space.

Because the array may contain negative numbers or zeros, the target can be any real number, not necessarily an integer. The algorithm must therefore use floating‑point division for S/2 and absolute difference calculations. Additionally, ties (two elements equally close to the target) should be resolved by choosing the first occurrence, which is naturally handled by a strict “less than” comparison when updating the best index.

This pattern exemplifies the classic “single‑pass optimization” in algorithm design: compute a global statistic once, then use it to evaluate each element in a single subsequent pass. It avoids recomputation and leverages linearity, which is essential for scalability in interview settings and real‑world systems where data streams can be massive.

Interview Questions on This Problem

Q1How would you modify the algorithm if the array were sorted and you needed to find the index of the element closest to the target in O(log n) time?

With a sorted array, you can use binary search to locate the insertion point of the target value. The closest element will be either the element just before or just after that insertion point. Compare their absolute differences to the target and return the index of the smaller one, achieving O(log n) time.

Q2In a distributed system where the array is partitioned across multiple nodes, how would you compute the dynamic target index efficiently?

Each node first computes the local sum of its partition and the local minimum difference with its local elements. These local sums are reduced (e.g., via MPI Reduce) to obtain the global sum S. Then each node recomputes the global target S/2 and scans its local elements to find the local best index and difference. A final reduction (e.g., MPI Allreduce) selects the global best index based on the smallest difference, ensuring O(n) total work plus O(log p) communication for p nodes.

Q3What edge cases would you test to ensure robustness of your implementation?

Test cases should include arrays with all positive numbers, all negative numbers, a mix of both, a single element, duplicate values, and arrays where two elements are equally close to the target. Also verify behavior when the sum is zero, when the target is exactly an array element, and when the array contains large integers that could cause overflow if not handled with 64‑bit arithmetic.

Examples

Example 1

Input

[4, 7, 12, 3]

Output

1

Explanation: Step-by-step: First, we calculate the total sum of the array, which is 4 + 7 + 12 + 3 = 26. Then, we divide the total sum by 2, which gives us 13. The number 7 is closest to 13, so the dynamic target index is 1.

Example 2

Input

[1, 2, 3, 4]

Output

2

Explanation: Step-by-step: First, we calculate the total sum of the array, which is 1 + 2 + 3 + 4 = 10. Then, we divide the total sum by 2, which gives us 5. The number 3 is closest to 5, so the dynamic target index is 2.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(N)

Optimal Approach & Strategy

First compute the total sum once in O(n). Then perform a single pass to find the element with the smallest absolute difference to sum/2, achieving O(n) time and O(1) space.

Brute Force Approach

Compute the sum of the array for each element, then find the element whose sum is closest to half of that sum. This requires O(n^2) time because you recompute the sum for every candidate.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
   let totalSum = nums.reduce((a, b) => a + b, 0);
   let target = Math.floor(totalSum / 2);
   let minDiff = Infinity;
   let result = -1;
   for (let i = 0; i < nums.length; i++) {
       let diff = Math.abs(nums[i] - target);
       if (diff < minDiff) {
           minDiff = diff;
           result = i;
       } else if (diff === minDiff && nums[i] < nums[result]) {
           result = i;
       }
   }
   return result;
}

Asked in Top Tech Interviews

CognizantPaytm

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.