algorithm
array
minimal sum
non-subsequent elements
computational problem

Finding two non-subsequent elements in array which sum is minimal

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Finding two non-subsequent elements in an array whose sum is minimal can be an interesting problem, particularly relevant in algorithms and data structure optimizations. This problem requires not just finding the minimum sum but also ensuring the elements are not adjacent, which introduces an additional complexity to the problem.

Problem Definition

Given an array of integers, the task is to find two elements, say `a[i]` and `a[j]`, such that `i` is not adjacent to `j` and the sum `a[i] + a[j]` is minimized. In a typical sequential find, if there are `n` elements in the array, an O(n2n^2) approach seems feasible, but this can be inefficient for large arrays. Below we explore an optimized method to solve this problem.

Approach

  1. Understanding Non-Subsequence: Non-subsequent elements mean those that do not follow one another directly. For instance, in `[3, 5, 2, 4]`, feasible pairs would be `(3, 2)`, `(3, 4)`, `(5, 4)`.
  2. Naive Approach:
    • Consider all possible pairs of elements with the constraint that indices are non-adjacent.
    • Complexity: This would require checking each pair, which results in O(n2)O(n^2) time complexity.
  3. Optimized Approach:
    • Step 1: Traverse the array to collect all elements while skipping adjacent pairs. Utilize two helper arrays `min_left` and `min_right`.
    • Step 2: Populate `min_left[i]` such that it contains the minimum element from start up to index `i-2`.
    • Step 3: Populate `min_right[i]` such that it contains the minimum element from index `i+2` to the end.
    • Step 4: Compute the sum using these helper arrays efficiently in one pass. The minimal sum can be found in O(nn) time by checking the entire `min_left` and `min_right` arrays.

Algorithm

  • min_left: `[inf, inf, 8, 1, 1, 2, 2]`
  • min_right: `[2, 6, 4, 0, 3, inf, inf]`
  • Dynamic Programming: Often in dynamic programming, subproblems with non-sequential indices are leveraged, similar to this problem.
  • Arrays and Searching: Understanding traversal and searching methods, such as binary search in a sorted array.
  • Greedy Algorithms: Parts of the solution look like a greedy choice by picking minimal values conditionally.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.