BackmediumTwo Pointers Flipkart

Optimal Recipe Combination Solution

Problem Statement

Given a list of recipe scores, where each score is either sweet (positive integer) or savory (negative integer), find the minimum length of a subarray that contains at least one sweet and one savory score. If no such subarray exists, return -1.

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

Explanation: Step-by-step: with input [3, -5, 2, -2, 1], we initialize two pointers, one at the start and one at the end of the subarray. We move the pointers towards each other until we find a sweet and a savory score. The length of the subarray is 5.

Example 2
Input
[-10]
Output
-1

Explanation: Step-by-step: with input [-10], we try to find a subarray with both sweet and savory scores. Since there is only one score, we return -1.

Constraints

  • 1 <= scores.length <= 10^5
  • -10^6 <= scores[i] <= 10^6
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free