Most Fixed Points After Array Deletions

Quick Overview

Maximize fixed points after arbitrary deletions from an integer array, preserving survivor order and allowing non-fixed elements to remain after reindexing.

Most Fixed Points After Array Deletions

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Most Fixed Points After Array Deletions You are given an integer array `nums`. In an array indexed from `0`, an element is a fixed point when its value equals its current index. You may delete any number of elements, including none or all of them. The remaining elements keep their relative order, shift left, and receive consecutive indices starting at `0`. Values do not change. Return the largest number of fixed points obtainable in the remaining array. Elements that are not fixed points are allowed to remain. ### Function `int maxFixedPoints(vector<int>& nums)` ### Example 1 **Input:** `nums = [8, 0, 1, 7, 2, 3]` **Output:** `4` **Explanation:** Delete the values `8` and `7`, leaving `[0, 1, 2, 3]`. All four positions are fixed points. Neither `8` nor `7` can equal a final index, so more than four is impossible. ### Example 2 **Input:** `nums = [6, 1, 1, 3]` **Output:** `2` **Explanation:** Keep every element. Indices `1` and `3` are fixed points. The two occurrences of `1` cannot both be fixed points because only one final index is `1`, and value `6` cannot equal a final index in an array of at most four elements. Thus the answer is `2`. ### Constraints - `1 <= nums.length <= 100000` - `0 <= nums[i] <= 100000` ### Clarifications - Fixed points are counted after the deletions and reindexing. - Deleting an element never changes the values or the relative order of the survivors. - The goal is the number of fixed points, not the length of the remaining array. - An empty remaining array has `0` fixed points. ### What a Strong Answer Covers - Accounting for the shift caused by deletions before an element. - Keeping useful non-fixed elements when necessary. - Handling repeated values and already-fixed consecutive elements correctly at the full input limit.

Overview: Maximize fixed points after arbitrary deletions from an integer array, preserving survivor order and allowing non-fixed elements to remain after reindexing.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Most Fixed Points After Array Deletions

You are given an integer array nums. In an array indexed from 0, an element is a fixed point when its value equals its current index.

You may delete any number of elements, including none or all of them. The remaining elements keep their relative order, shift left, and receive consecutive indices starting at 0. Values do not change.

Return the largest number of fixed points obtainable in the remaining array. Elements that are not fixed points are allowed to remain.

Function

int maxFixedPoints(vector<int>& nums)

Example 1

Input: nums = [8, 0, 1, 7, 2, 3]

Output: 4

Explanation: Delete the values 8 and 7, leaving [0, 1, 2, 3]. All four positions are fixed points. Neither 8 nor 7 can equal a final index, so more than four is impossible.

Example 2

Input: nums = [6, 1, 1, 3]

Output: 2

Explanation: Keep every element. Indices 1 and 3 are fixed points. The two occurrences of 1 cannot both be fixed points because only one final index is 1, and value 6 cannot equal a final index in an array of at most four elements. Thus the answer is 2.

Constraints

  • 1 <= nums.length <= 100000
  • 0 <= nums[i] <= 100000

Clarifications

  • Fixed points are counted after the deletions and reindexing.
  • Deleting an element never changes the values or the relative order of the survivors.
  • The goal is the number of fixed points, not the length of the remaining array.
  • An empty remaining array has 0 fixed points.

What a Strong Answer Covers Guidance

  • Accounting for the shift caused by deletions before an element.
  • Keeping useful non-fixed elements when necessary.
  • Handling repeated values and already-fixed consecutive elements correctly at the full input limit.