Question: Infosys SP | Recent Online Assessment 8th Feb | Maximum Count of Processed Numbers | Latest Infosys Placement Questions 2026 | Frequency Array Optimization
0
Entering edit mode

Question: Maximum Count of Processed Numbers (Frequency Logic)

Problem Statement:

You are given an array A of N numbers.

You may choose to process a number if its current frequency is different from the frequency of the previously taken number.

Find the maximum count of numbers that can be processed.

Input Format:

  • The first line contains an integer, N, denoting the size of the array.
  • Each line i of the N subsequent lines (where 0 \le i < N) contains an integer describing A[i].

ADD COMMENTlink 11 days ago Sarthak • 10
0
Entering edit mode

Solution1 -: Maximum Count of Processed Numbers Solution

Topics Involved / Prerequisites

  • Frequency Maps (Hash Maps)
  • Combinatorics / Pigeonhole Principle
  • Greedy Scheduling

Overview We must first calculate the static frequency of every number in the array to act as that number's distinct "color" or "label". To maximize the processed count, we must select numbers such that no two consecutively chosen numbers share the same frequency label. By grouping the array elements by these frequency labels, we can determine the maximum possible alternating arrangement using a simple mathematical bounding formula.

Approach

1. Mapping Frequencies First, we need to know the exact frequency of every element in the initial array. We use a Hash Map to count the occurrences of each number. Once we have this, every single element in our array is assigned a "property" equal to its frequency. For example, if the number 5 appears exactly 3 times in the array, its property label is 3.

2. The Mathematical Bound Since we want to arrange these elements such that no two adjacent elements have the same property, we group all array elements by their property labels. We find the property that appears the most often overall—let's call its count maxCount. All the other remaining elements have a combined count of rest = N - maxCount.

  • If we have enough rest elements to act as "buffers" between our majority elements (maxCount <= rest + 1), we can successfully process the entire array, returning N.
  • If we don't have enough buffers, the majority elements will bottleneck us. The maximum sequence we can form is exactly 2 * rest + 1.

(Note: If the problem strictly requires you to process the array from left-to-right without changing the original order, the logic is even simpler: just traverse the array greedily and increment your count whenever the current element's frequency differs from the last processed element's frequency).

function maxProcessedNumbers(N, A):
    // Step 1: Calculate the static frequency of each specific number
    create a hash map 'freqMap'
    for i from 0 to N - 1:
        freqMap[A[i]] = freqMap[A[i]] + 1
        
    // Step 2: Group elements by their frequency "property"
    create a hash map 'propertyCount'
    for i from 0 to N - 1:
        property = freqMap[A[i]]
        propertyCount[property] = propertyCount[property] + 1
        
    // Step 3: Find the majority property count
    maxCount = 0
    for each key-value pair in propertyCount:
        maxCount = max(maxCount, pair.value)
        
    // Step 4: Calculate the mathematical bound
    rest = N - maxCount
    
    if maxCount <= rest + 1:
        return N // We have enough buffers to process everything
    else:
        return (2 * rest) + 1 // Bottlenecked by the majority property

Time Complexity

  • Time: O(N) - We traverse the array a few times to build the frequency maps and group the counts, which operates in strictly linear time.
  • Space: O(N) - We require extra memory to store the frequencies and property counts in Hash Maps.
ADD COMMENTlink 3 days ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts