BackeasyTwo PointersInfosys

Parity-Based Pointer Convergence Solution

Problem Statement

Given an array of integers nums, return the index where the two pointers meet for the first time. If the pointers meet at the same index, return that index.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we initialize two pointers, one at the start and one at the end of the array. We then move the pointers towards each other until they meet. In this case, the pointers meet at index 2.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6], we initialize two pointers, one at the start and one at the end of the array. We then move the pointers towards each other until they meet. In this case, the pointers meet at index 3.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
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

Parity-Based Pointer Convergence — Problem Statement & Solution Guide

Two PointersEasyOpposite Pointers
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers nums, return the index where the two pointers meet for the first time. If the pointers meet at the same index, return that index.

Examples

Example 1

Input

[1, 2, 3, 4, 5]

Output

2

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we initialize two pointers, one at the start and one at the end of the array. We then move the pointers towards each other until they meet. In this case, the pointers meet at index 2.

Example 2

Input

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

Output

3

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6], we initialize two pointers, one at the start and one at the end of the array. We then move the pointers towards each other until they meet. In this case, the pointers meet at index 3.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Optimal Approach & Strategy

The optimal approach directly simulates the pointer convergence using the two-pointer technique. We initialize two pointers at both ends of the array and repeatedly calculate their sum, moving either the left or right pointer inward depending on the parity of the sum until they meet. This guarantees finding the meeting point in exactly N - 1 steps.

Brute Force Approach

A naive approach might try to simulate the pointer convergence by copying elements or performing nested scans to anticipate the meeting point. However, since the rules of pointer movement are completely deterministic and reduce the search space by exactly one element per step, any solution other than direct simulation is unnecessarily complex.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function parityBasedPointerConvergence(nums) {
  let left = 0;
  let right = nums.length - 1;
  while (left <= right) {
    if ((nums[left] + nums[right]) % 2 === 0) {
      left++;
    } else {
      right--;
    }
    if (left === right) {
      return left;
    }
  }
  return -1;
}

Asked in Top Tech Interviews

Infosys

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.