#181. 检查有序数组相邻三元组差值

    ID: 181 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatAp-1gesp3一维数组循环结构

检查有序数组相邻三元组差值

Check Adjacent Triplet

Background

Congcong is currently organizing some score data. He wants to find if there are any specific patterns within it.

Problem Description

Given an array of scores sorted in increasing order, return true if the array contains 33 adjacent scores such that the difference between the maximum and minimum score within that triplet is at most 22. For example, with triplets like {3, 4, 5} or {3, 5, 5}, it should return true.

Input Format

Input is given from Standard Input in the following format.

score_1 score_2 ... score_n (representing an array of integers, elements separated by spaces)

Output Format

Output is printed to Standard Output in the following format.

true or false

Sample

3 4 5
true
3 4 6
false
1 3 5 5
true

Sample Explanation

Sample 1: The array is [3, 4, 5]. These three numbers are adjacent, and the difference between the maximum 55 and minimum 33 is 53=25 - 3 = 2, which satisfies the condition (at most 22). Thus, true is returned.

Sample 2: The array is [3, 4, 6]. These three numbers are adjacent, but the difference between the maximum 66 and minimum 33 is 63=36 - 3 = 3, which does not satisfy the condition (at most 22). Thus, false is returned.

Sample 3: The array is [1, 3, 5, 5].

  • Consider the adjacent triplet [1, 3, 5]. The difference between the maximum 55 and minimum 11 is 51=45 - 1 = 4, which does not satisfy the condition.
  • Consider the adjacent triplet [3, 5, 5]. The difference between the maximum 55 and minimum 33 is 53=25 - 3 = 2, which satisfies the condition. Thus, true is returned.

Constraints

Time limit: 11 second. Memory limit: 10241024 MiB.