BackmediumArraysRazorpay

Galaxy Signal Peaks Solution

Problem Statement

In a deep-space telemetry array, each sensor node records a signal strength value. The array is processed by replacing every element with the product of its immediate neighbors. For the first and last elements, the missing neighbor is treated as 1. After this transformation, identify the indices of all peak signals in the resulting array. A peak signal is defined as an element that is greater than or equal to both of its immediate neighbors in the modified array. For boundary elements, only the single existing neighbor is considered for the comparison. Return the list of indices of all such peaks in ascending order.

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

Explanation: Step 1: Compute the modified array. Index 0: 1 * 3 = 3. Index 1: 2 * 4 = 8. Index 2: 3 * 5 = 15. Index 3: 4 * 6 = 24. Index 4: 5 * 1 = 5. Modified array: [3, 8, 15, 24, 5]. Step 2: Identify peaks. Index 0: 3 >= 8? No. Index 1: 8 >= 3 and 8 >= 15? No. Index 2: 15 >= 8 and 15 >= 24? No. Index 3: 24 >= 15 and 24 >= 5? Yes. Index 4: 5 >= 24? No. Wait, let's re-evaluate. Index 1: 8 >= 3 (left) and 8 >= 15 (right)? No. Index 2: 15 >= 8 (left) and 15 >= 24 (right)? No. Index 3: 24 >= 15 (left) and 24 >= 5 (right)? Yes. So only index 3 is a peak. Let me correct the output. Output: [3].

Example 2
Input
signals = [1, 1, 1, 1, 1]
Output
[0, 1, 2, 3, 4]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 1 = 1. Index 1: 1 * 1 = 1. Index 2: 1 * 1 = 1. Index 3: 1 * 1 = 1. Index 4: 1 * 1 = 1. Modified array: [1, 1, 1, 1, 1]. Step 2: Identify peaks. Index 0: 1 >= 1? Yes. Index 1: 1 >= 1 and 1 >= 1? Yes. Index 2: 1 >= 1 and 1 >= 1? Yes. Index 3: 1 >= 1 and 1 >= 1? Yes. Index 4: 1 >= 1? Yes. All indices are peaks.

Example 3
Input
signals = [5, 2, 8, 1, 9]
Output
[2, 4]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 2 = 2. Index 1: 5 * 8 = 40. Index 2: 2 * 1 = 2. Index 3: 8 * 9 = 72. Index 4: 1 * 1 = 1. Modified array: [2, 40, 2, 72, 1]. Step 2: Identify peaks. Index 0: 2 >= 40? No. Index 1: 40 >= 2 and 40 >= 2? Yes. Index 2: 2 >= 40? No. Index 3: 72 >= 2 and 72 >= 1? Yes. Index 4: 1 >= 72? No. Peaks at indices 1 and 3. Wait, let me re-check. Index 1: 40 >= 2 (left) and 40 >= 2 (right)? Yes. Index 3: 72 >= 2 (left) and 72 >= 1 (right)? Yes. So peaks are at 1 and 3. Output: [1, 3].

Example 4
Input
signals = [10, 20, 30]
Output
[1]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 20 = 20. Index 1: 10 * 30 = 300. Index 2: 20 * 1 = 20. Modified array: [20, 300, 20]. Step 2: Identify peaks. Index 0: 20 >= 300? No. Index 1: 300 >= 20 and 300 >= 20? Yes. Index 2: 20 >= 300? No. Peak at index 1.

Constraints

  • 1 <= signals.length <= 10^5
  • 1 <= signals[i] <= 10^9
  • The product of any two adjacent elements will not exceed 10^18
  • Return indices in strictly increasing order
  • If no peaks exist, return an empty list
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

Galaxy Signal Peaks — Problem Statement & Solution Guide

ArraysMediumArray manipulation and peak element identification
TimeO(n)
|
SpaceO(1)

Problem Description

In a deep-space telemetry array, each sensor node records a signal strength value. The array is processed by replacing every element with the product of its immediate neighbors. For the first and last elements, the missing neighbor is treated as 1. After this transformation, identify the indices of all peak signals in the resulting array. A peak signal is defined as an element that is greater than or equal to both of its immediate neighbors in the modified array. For boundary elements, only the single existing neighbor is considered for the comparison. Return the list of indices of all such peaks in ascending order.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galaxy Signal Peaks"

medium

WHY DOES IT MATTER?

Identifying peaks after a neighbor‑product transformation is a micro‑cosm of many signal‑processing pipelines where local aggregations are followed by anomaly detection; mastering this pattern sharpens a candidate’s ability to reason about constant‑window dependencies and in‑place computation.

OPTIMIZATION CHALLENGE

The key insight is that the transformed value for index i can be derived from original[i‑1] and original[i+1] alone, so you never need to materialize the whole new array. By keeping a sliding window of three original values you can compute new[i‑1] and evaluate its peak status on the fly, collapsing two passes into one.

REAL-WORLD CONNECTION

In distributed monitoring systems, each node often reports a metric that is a function of its neighboring nodes (e.g., load balancing decisions based on adjacent server loads). Detecting spikes after such aggregation mirrors the "Galaxy Signal Peaks" problem, helping engineers design low‑latency alerting mechanisms.

When coding, write the loop to first compute the product for the previous index, then immediately test the peak condition before moving the window forward – this eliminates off‑by‑one bugs and keeps memory usage O(1).

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The transformation step replaces each element a[i] with the product of its immediate neighbors: new[i]= (i>0? a[i-1]:1)*(i<n-1? a[i+1]:1). This can be computed in a single linear pass because each new value depends only on two original adjacent values, not on previously computed new values. After the transformation, a peak is an index i such that new[i] > new[i-1] and new[i] > new[i+1] (with virtual -∞ at the boundaries). A naive solution would first build the entire transformed array, then run a nested loop to compare each element with every other element, leading to O(n²) time, which quickly becomes infeasible for large telemetry streams (n can be up to 10⁶ or more). The optimal paradigm leverages the locality of the problem: both the transformation and peak detection are local operations that can be performed while scanning the original array once, yielding O(n) time and O(1) auxiliary space. This approach mirrors classic sliding‑window and prefix‑suffix techniques where each output depends on a constant‑size neighbourhood of inputs.

Interview Questions on This Problem

Q1How would you compute the transformed array and peak indices in a single pass without using extra O(n) space?

Iterate once, keeping the previous original value and the current original value; compute the product for the previous index using its left neighbor (stored from two steps back) and its right neighbor (the current value). Simultaneously check the peak condition for the index whose transformed value is now known, storing the index if it satisfies the > neighbor criteria.

Q2What overflow considerations are necessary when multiplying neighbor values, and how can you mitigate them in languages like Java or C++?

Since signal strengths can be up to 10⁹, the product may exceed 32‑bit range. Use 64‑bit integer types (long long in C++, long in Java) or BigInteger if the constraints allow arbitrarily large numbers. Additionally, you can early‑exit if a product exceeds a known maximum when only relative comparisons are needed.

Q3Explain how the "first/last neighbor as 1" rule affects the peak detection at the boundaries and how you would handle it in code.

Treat the virtual neighbor outside the array as 1, so new[0]=1*a[1] and new[n-1]=a[n-2]*1. For peak checks, consider a virtual -∞ outside the array, meaning the first and last positions can be peaks if their transformed value is greater than their only real neighbor. Implement this by initializing leftPeakValue = Long.MIN_VALUE for the leftmost comparison and similarly for the rightmost after the loop.

Examples

Example 1

Input

signals = [2, 3, 4, 5, 6]

Output

[1, 2, 3]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 3 = 3. Index 1: 2 * 4 = 8. Index 2: 3 * 5 = 15. Index 3: 4 * 6 = 24. Index 4: 5 * 1 = 5. Modified array: [3, 8, 15, 24, 5]. Step 2: Identify peaks. Index 0: 3 >= 8? No. Index 1: 8 >= 3 and 8 >= 15? No. Index 2: 15 >= 8 and 15 >= 24? No. Index 3: 24 >= 15 and 24 >= 5? Yes. Index 4: 5 >= 24? No. Wait, let's re-evaluate. Index 1: 8 >= 3 (left) and 8 >= 15 (right)? No. Index 2: 15 >= 8 (left) and 15 >= 24 (right)? No. Index 3: 24 >= 15 (left) and 24 >= 5 (right)? Yes. So only index 3 is a peak. Let me correct the output. Output: [3].

Example 2

Input

signals = [1, 1, 1, 1, 1]

Output

[0, 1, 2, 3, 4]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 1 = 1. Index 1: 1 * 1 = 1. Index 2: 1 * 1 = 1. Index 3: 1 * 1 = 1. Index 4: 1 * 1 = 1. Modified array: [1, 1, 1, 1, 1]. Step 2: Identify peaks. Index 0: 1 >= 1? Yes. Index 1: 1 >= 1 and 1 >= 1? Yes. Index 2: 1 >= 1 and 1 >= 1? Yes. Index 3: 1 >= 1 and 1 >= 1? Yes. Index 4: 1 >= 1? Yes. All indices are peaks.

Example 3

Input

signals = [5, 2, 8, 1, 9]

Output

[2, 4]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 2 = 2. Index 1: 5 * 8 = 40. Index 2: 2 * 1 = 2. Index 3: 8 * 9 = 72. Index 4: 1 * 1 = 1. Modified array: [2, 40, 2, 72, 1]. Step 2: Identify peaks. Index 0: 2 >= 40? No. Index 1: 40 >= 2 and 40 >= 2? Yes. Index 2: 2 >= 40? No. Index 3: 72 >= 2 and 72 >= 1? Yes. Index 4: 1 >= 72? No. Peaks at indices 1 and 3. Wait, let me re-check. Index 1: 40 >= 2 (left) and 40 >= 2 (right)? Yes. Index 3: 72 >= 2 (left) and 72 >= 1 (right)? Yes. So peaks are at 1 and 3. Output: [1, 3].

Example 4

Input

signals = [10, 20, 30]

Output

[1]

Explanation: Step 1: Compute the modified array. Index 0: 1 * 20 = 20. Index 1: 10 * 30 = 300. Index 2: 20 * 1 = 20. Modified array: [20, 300, 20]. Step 2: Identify peaks. Index 0: 20 >= 300? No. Index 1: 300 >= 20 and 300 >= 20? Yes. Index 2: 20 >= 300? No. Peak at index 1.

Constraints

  • 1 <= signals.length <= 10^5
  • 1 <= signals[i] <= 10^9
  • The product of any two adjacent elements will not exceed 10^18
  • Return indices in strictly increasing order
  • If no peaks exist, return an empty list

Optimal Approach & Strategy

Compute neighbor products on the fly with a sliding window of three original values and evaluate peak conditions in the same linear pass – O(n) time, O(1) extra space.

Brute Force Approach

Create the whole transformed array, then for each index compare it with all other indices to find peaks – O(n²) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
const fs = require('fs');
const data = fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
let pos=0;
const n = data[pos++]||0;
const signals = data.slice(pos, pos+n);
function findPeaks(arr){
    const res=[];
    const len=arr.length;
    if(len===0) return res;
    for(let i=0;i<len;i++){
        const left = (i===0)?1:arr[i-1];
        const right= (i===len-1)?1:arr[i+1];
        const prod = left*right;
        const leftOrig = (i===0)?1:arr[i-1];
        const rightOrig= (i===len-1)?1:arr[i+1];
        if(prod>leftOrig && prod>rightOrig) res.push(i);
    }
    return res;
}
const ans=findPeaks(signals);
if(ans.length) console.log(ans.join(' '));
else console.log('');

Asked in Top Tech Interviews

Razorpay

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.