Given an integer array arr[] of integers, consider every group of three consecutive (adjacent) elements in the array. For each such triplet (arr[i], arr[i + 1], arr[i + 2]), determine whether these three values can represent the sides of a valid triangle.
Return an array of length n - 2 where the ith element is 1 if arr[i], arr[i+1], and arr[i+2] can form a triangle, otherwise 0.
Note: Three sides a, b, and c can form a valid triangle if and only if all of the following conditions hold:
- a + b > c
- a + c > b
- b + c > a
Examples:
Input: arr[] = [1, 2, 2, 4]
Output: [1, 0]
Explanation: The consecutive triplets are [1, 2, 2] and [2, 2, 4]. The first triplet satisfies all the triangle inequalities, so it forms a valid triangle. The second triplet does not, because 2 + 2 = 4 is not greater than 4. Therefore, the output is [1, 0].Input: arr[] = [2, 10, 2, 10, 2]
Output: [0, 1, 0]
Explanation: The array contains three consecutive triplets: [2, 10, 2], [10, 2, 10], and [2, 10, 2]. The first and third triplets cannot form a triangle because 2 + 2 < 10. The middle triplet satisfies all the triangle inequalities and forms a valid triangle. Hence, the output is [0, 1, 0].
Using Direct Simulation - O(n) Time and O(n) Space
For each consecutive triplet in the array, the idea is to check whether it satisfies the triangle inequality theorem. If the sum of any two sides is greater than the third side, the triplet forms a valid triangle, so record 1; otherwise, record 0.
- Initialize an empty result array ans.
- Traverse the array from index 0 to n - 3.
- For each consecutive triplet (arr[i], arr[i + 1], arr[i + 2]), check the triangle inequality conditions.
- If all three conditions are satisfied, append 1 to ans; otherwise, append 0.
- Repeat until all consecutive triplets have been processed.
- Return the result array ans.
#include <bits/stdc++.h>
using namespace std;
// Function to determine whether every consecutive triplet
// in the array can form a valid triangle
vector<int> canMakeTriangle(vector<int> &arr)
{
int n = arr.size();
vector<int> ans;
// Traverse all consecutive triplets
for (int i = 0; i <= n - 3; i++)
{
int a = arr[i];
int b = arr[i + 1];
int c = arr[i + 2];
// Check the triangle inequality
if (a + b > c && a + c > b && b + c > a)
ans.push_back(1);
else
ans.push_back(0);
}
return ans;
}
int main()
{
vector<int> arr = {2, 10, 2, 10, 2};
vector<int> result = canMakeTriangle(arr);
for (int i = 0; i < result.size(); i++)
{
cout << result[i];
if (i != result.size() - 1)
cout << " ";
}
return 0;
}
import java.util.*;
class GFG {
// Function to determine whether every consecutive
// triplet in the array can form a valid triangle
static ArrayList<Integer> canMakeTriangle(int[] arr)
{
int n = arr.length;
ArrayList<Integer> ans = new ArrayList<>();
// Traverse all consecutive triplets
for (int i = 0; i <= n - 3; i++) {
int a = arr[i];
int b = arr[i + 1];
int c = arr[i + 2];
// Check the triangle inequality
if (a + b > c && a + c > b && b + c > a)
ans.add(1);
else
ans.add(0);
}
return ans;
}
public static void main(String[] args)
{
int[] arr = { 2, 10, 2, 10, 2 };
ArrayList<Integer> result = canMakeTriangle(arr);
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i));
if (i != result.size() - 1)
System.out.print(" ");
}
}
}
# Function to determine whether every consecutive triplet
# in the array can form a valid triangle
def can_make_triangle(arr):
n = len(arr)
ans = []
# Traverse all consecutive triplets
for i in range(n - 2):
a = arr[i]
b = arr[i + 1]
c = arr[i + 2]
# Check the triangle inequality
if a + b > c and a + c > b and b + c > a:
ans.append(1)
else:
ans.append(0)
return ans
# Driver Code
if __name__ == "__main__":
arr = [2, 10, 2, 10, 2]
result = can_make_triangle(arr)
print(*result)
using System;
using System.Collections.Generic;
class GFG {
// Function to determine whether every consecutive
// triplet in the array can form a valid triangle
static List<int> canMakeTriangle(int[] arr)
{
int n = arr.Length;
List<int> ans = new List<int>();
// Traverse all consecutive triplets
for (int i = 0; i <= n - 3; i++) {
int a = arr[i];
int b = arr[i + 1];
int c = arr[i + 2];
// Check the triangle inequality
if (a + b > c && a + c > b && b + c > a)
ans.Add(1);
else
ans.Add(0);
}
return ans;
}
static void Main()
{
int[] arr = { 2, 10, 2, 10, 2 };
List<int> result = canMakeTriangle(arr);
for (int i = 0; i < result.Count; i++) {
Console.Write(result[i]);
if (i != result.Count - 1)
Console.Write(" ");
}
}
}
// Function to determine whether every consecutive triplet
// in the array can form a valid triangle
function canMakeTriangle(arr)
{
let n = arr.length;
let ans = [];
// Traverse all consecutive triplets
for (let i = 0; i <= n - 3; i++) {
let a = arr[i];
let b = arr[i + 1];
let c = arr[i + 2];
// Check the triangle inequality
if (a + b > c && a + c > b && b + c > a)
ans.push(1);
else
ans.push(0);
}
return ans;
}
// Driver Code
let arr = [ 2, 10, 2, 10, 2 ];
let result = canMakeTriangle(arr);
console.log(result.join(" "));
Output
0 1 0