Given an array A[] of N elements, You'd like to know how many triangles can be formed with side lengths equal to adjacent elements from A[].
Construct an array of integers of length N - 2 where ith element is equal to 1 if it is possible to form a triangle with side lengths A[i], A[i+1], and A[i+2]. otherwise 0.
Note: A triangle can be formed with side lengths a, b and c if a+b>c and a+c>b and b+c>a.
Example 1:
Input:
N = 4
A[] = {1, 2, 2, 4}
Output:
1 0
Explanation:
output[0] = 1 because we can form a 
triangle with side lengths 1,2 and 2.
output[1] = 0 because 2+2<4 so, we cannot 
form a triangle with side lengths 2,2 and 4.
Example 2:
Input: 
N = 5
A[] = {2, 10, 2, 10, 2}
Output:
0 1 0
Explanation:
output[0] = 0 because 2+2<10 so, we cannot
form a triangle with side lengths 2, 10 and 2. 
output[1] = 1 we can form a triangle with 
side lengths 10,2 and 10. 
output[1] = 0 because 2+2<10 so, we can
form a triangle with side lengths 2, 10 and 2. 
Your Task:
You dont need to read input or print anything. Your task is to complete the function canMakeTriangle() which takes the array A and the integer N as the input parameters, and returns the array of answers.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
3 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Geometry

Mathematics

Starter code: #User function Template for python3

class Solution:

    def canMakeTriangle(self, A, N): 

        #code here



Solutions:
class Solution:

	def canMakeTriangle(self, A, N):
		res = [0] * (N - 2)
		for i in range(N - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i + 1] + A[i + 2] > A[i] and (A[i + 2] + A[i] > A[i + 1]):
				res[i] = 1
		return res


class Solution:

	def canMakeTriangle(self, A, N):
		lst = []
		for i in range(N - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i + 2] + A[i + 1] > A[i] and (A[i] + A[i + 2] > A[i + 1]):
				lst.append(1)
			else:
				lst.append(0)
		return lst


class Solution:

	def canMakeTriangle(self, A, N):
		k = []
		for i in range(N - 2):
			check = A[i:i + 3]
			check.sort()
			if check[0] + check[1] > check[2] and check[0] + check[2] > check[1]:
				k.append(1)
			else:
				k.append(0)
		return k


class Solution:

	def canMakeTriangle(self, A, N):
		res = [0] * (N - 2)
		for i in range(0, N - 2):
			mx = max(max(A[i], A[i + 1]), A[i + 2])
			sm = sum(A[i:i + 3])
			if mx < sm - mx:
				res[i] = 1
		return res


class Solution:

	def canMakeTriangle(self, A, N):
		ans = []
		for i in range(N - 2):
			x = A[i:i + 3]
			c = sum(x)
			m = max(x)
			if c - m > m:
				ans.append(1)
			else:
				ans.append(0)
		return ans


class Solution:

	def canMakeTriangle(self, a, n):
		l = []
		for i in range(n - 2):
			if a[i] + a[i + 1] > a[i + 2] and a[i] + a[i + 2] > a[i + 1] and (a[i + 1] + a[i + 2] > a[i]):
				l += [1]
			else:
				l += [0]
		return l


class Solution:

	def canMakeTriangle(self, A, N):
		e = []
		for i in range(N - 2):
			a = A[i]
			b = A[i + 1]
			c = A[i + 2]
			if a + b > c and b + c > a and (a + c > b):
				e += [1]
			else:
				e += [0]
		return e


class Solution:

	def canMakeTriangle(self, a, N):
		op = []
		for i in range(2, N):
			l = [a[i - 2], a[i - 1], a[i]]
			l.sort()
			if l[0] + l[1] > l[2]:
				op.append(1)
			else:
				op.append(0)
		return op


class Solution:

	def canMakeTriangle(self, arr, n):
		res = [0] * (n - 2)
		for i in range(n - 2):
			a = arr[i]
			b = arr[i + 1]
			c = arr[i + 2]
			if a + b > c and b + c > a and (c + a > b):
				res[i] = 1
		return res


class Solution:

	def canMakeTriangle(self, A, N):

		def istriangle(a, b, c):
			if a + b > c and b + c > a and (c + a > b):
				return True
			return False
		arr = []
		for i in range(N - 2):
			if istriangle(A[i], A[i + 1], A[i + 2]):
				arr.append('1')
			else:
				arr.append('0')
		s = ''.join(arr)
		return s


class Solution:

	def canMakeTriangle(self, A: list, N: int) -> list:
		result = []
		for i in range(N - 2):
			condition1 = A[i] + A[i + 1] > A[i + 2]
			condition2 = A[i] + A[i + 2] > A[i + 1]
			condition3 = A[i + 1] + A[i + 2] > A[i]
			if condition1 and condition2 and condition3:
				result.append(1)
			else:
				result.append(0)
		return result


class Solution:

	def canMakeTriangle(self, A, N):
		out = []
		for i in range(N - 2):
			elems = sorted([A[i], A[i + 1], A[i + 2]])
			if elems[0] + elems[1] > elems[2]:
				out.append(1)
			else:
				out.append(0)
		return out


class Solution:

	def makeTriangle(self, x, y, z):
		if x + y > z and x + z > y and (y + z > x):
			return True
		return False

	def canMakeTriangle(self, A, N):
		outArr = []
		for i in range(0, N - 2, 1):
			if self.makeTriangle(A[i], A[i + 1], A[i + 2]):
				outArr.append(1)
			else:
				outArr.append(0)
		return outArr


class Solution:

	def canMakeTriangle(self, a, n):
		l = []
		for i in range(len(a) - 2):
			if a[i] + a[i + 1] <= a[i + 2] or a[i] + a[i + 2] <= a[i + 1] or a[i + 1] + a[i + 2] <= a[i]:
				l.append(0)
			else:
				l.append(1)
		return l


class Solution:

	def canMakeTriangle(self, arr, N):
		ans = list()
		for i in range(N - 2):
			a = arr[i]
			b = arr[i + 1]
			c = arr[i + 2]
			if a + b > c and a + c > b and (c + b > a):
				ans.append(1)
			else:
				ans.append(0)
		return ans


class Solution:

	def canMakeTriangle(self, A, N):
		b = []
		for i in range(0, N - 2):
			if A[i] + A[i + 1] <= A[i + 2]:
				b.append(0)
			elif A[i] + A[i + 2] <= A[i + 1]:
				b.append(0)
			elif A[i + 1] + A[i + 2] <= A[i]:
				b.append(0)
			else:
				b.append(1)
		return b


class Solution:

	def canMakeTriangle(self, a, n):
		b = []
		for i in range(0, n - 2):
			if a[i] + a[i + 1] <= a[i + 2]:
				b.append(0)
			elif a[i] + a[i + 2] <= a[i + 1]:
				b.append(0)
			elif a[i + 1] + a[i + 2] <= a[i]:
				b.append(0)
			else:
				b.append(1)
		return b


class Solution:

	def canMakeTriangle(self, A, N):
		list1 = []
		for i in range(len(A) - 2):
			a = A[i]
			b = A[i + 1]
			c = A[i + 2]
			if a + b > c and a + c > b and (b + c > a):
				list1.append(1)
			else:
				list1.append(0)
		return list1


class Solution:

	def canMakeTriangle(self, A, N):
		B = []
		for i in range(0, N - 2, 1):
			(x, y, z) = (A[i], A[i + 1], A[i + 2])
			if x + y > z and y + z > x and (x + z > y):
				B.append(1)
			else:
				B.append(0)
		return B


class Solution:

	def canMakeTriangle(self, A, N):
		triangles = [0] * (N - 2)
		for i in range(N - 2):
			tri = []
			tri.append(A[i])
			tri.append(A[i + 1])
			tri.append(A[i + 2])
			tri = sorted(tri)
			if tri[0] + tri[1] > tri[2]:
				triangles[i] = 1
		return triangles


class Solution:

	def canMakeTriangle(self, A, N):
		re = []
		for i in range(N - 2):
			r = A[i:i + 3]
			r.sort()
			if r[0] + r[1] > r[2]:
				re.append(1)
			else:
				re.append(0)
		return re


class Solution:

	def canMakeTriangle(self, A, N):
		res = []
		for i in range(0, N - 2):
			z = A[i:i + 3]
			z.sort()
			if sum(z[0:2]) > z[2]:
				res.append(1)
			elif sum(z[0:2]) <= z[2]:
				res.append(0)
		return res


class Solution:

	def canMakeTriangle(self, A, N):
		result = []
		for i in range(N - 2):
			temp = A[i:i + 3]
			if temp[0] + temp[1] > temp[2] and temp[0] + temp[2] > temp[1] and (temp[2] + temp[1] > temp[0]):
				result.append(1)
			else:
				result.append(0)
		return result


import math

class Solution:

	def canMakeTriangle(self, A, N):
		a = []
		for i in range(N - 2):
			l1 = A[i]
			l2 = A[i + 1]
			l3 = A[i + 2]
			if l1 + l2 > l3 and l1 + l3 > l2 and (l2 + l3 > l1):
				a.append(1)
			else:
				a.append(0)
		return a


class Solution:

	def canMakeTriangle(self, a, n):
		j = 0
		i = 0
		arr = []
		sar = []
		for i in range(n):
			if j + 3 <= n:
				sar[:] = a[i:i + 3]
				sar = sorted(sar)
				if sar[0] + sar[1] > sar[2]:
					arr.append(1)
					j += 1
				else:
					arr.append(0)
					j += 1
			else:
				break
		return arr


class Solution:

	def canMakeTriangle(self, A, N):
		L = []
		for k in range(N - 2):
			if A[k] + A[k + 1] > A[k + 2] and A[k] + A[k + 2] > A[k + 1] and (A[k + 2] + A[k + 1] > A[k]):
				L.append(1)
			else:
				L.append(0)
		return L


class Solution:

	def canMakeTriangle(self, A, N):
		r = []
		for i in range(N - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i] + A[i + 2] > A[i + 1] and (A[i + 1] + A[i + 2] > A[i]):
				r.append(1)
			else:
				r.append(0)
		return r
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = list(map(int, input().strip().split()))
		ob = Solution()
		ans = ob.canMakeTriangle(A, N)
		for i in ans:
			print(i, end=' ')
		print()


class Solution:

	def canMakeTriangle(self, A, N):
		res = []
		for i in range(N):
			l1 = []
			if i + 2 > N - 1:
				break
			l1.append(A[i])
			l1.append(A[i + 1])
			l1.append(A[i + 2])
			m1 = min(l1)
			l1.remove(m1)
			m2 = min(l1)
			l1.remove(m2)
			if m1 + m2 > l1[0]:
				res.append(1)
			else:
				res.append(0)
		return res


class Solution:

	def canMakeTriangle(self, A, N):
		import numpy as np
		B = []
		for i in range(N - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i + 1] + A[i + 2] > A[i] and (A[i + 2] + A[i] > A[i + 1]):
				B.append(1)
			else:
				B.append(0)
		return np.asarray(B)


class Solution:

	def triangle(self, a, b, c):
		if a + b <= c:
			return False
		elif a + c <= b:
			return False
		elif b + c <= a:
			return False
		return True

	def canMakeTriangle(self, A, N):
		res = []
		for i in range(N - 2):
			if self.triangle(A[i], A[i + 1], A[i + 2]):
				res.append(1)
			else:
				res.append(0)
		return res


class Solution:

	def canMakeTriangle(self, a, n):
		s = []
		for i in range(n - 2):
			x = 0
			(d, b, c) = (a[i], a[i + 1], a[i + 2])
			if d + b > c:
				x += 1
			if b + c > d and x == 1:
				x += 1
			if d + c > b and x == 2:
				x += 1
			if x == 3:
				s.append(1)
			if x < 3:
				s.append(0)
		return s


class Solution:

	def canMakeTriangle(self, A, N):
		s = []
		for i in range(0, N - 2):
			test = A[i:i + 3]
			test.sort()
			m = test[2]
			test = test[0:2]
			if sum(test) > m:
				s += [1]
			else:
				s += [0]
		return s


class Solution:

	def canMakeTriangle(self, A, N):

		def isform(arr):
			a = arr[0]
			b = arr[1]
			c = arr[2]
			if a + b > c and a + c > b and (b + c > a):
				return 1
			return 0
		i = 0
		j = 2
		l = []
		while j <= N - 1:
			l.append(isform(A[i:j + 1]))
			i += 1
			j += 1
		return l


class Solution:

	def canMakeTriangle(self, A, N):
		result = []
		x = len(A)
		for i in range(x - 2):
			if A[i] + A[i + 1] > A[i + 2] and A[i] + A[i + 2] > A[i + 1] and (A[i + 1] + A[i + 2] > A[i]):
				result.append(1)
			else:
				result.append(0)
		return result


class Solution:

	def canMakeTriangle(self, A, N):
		lis = []

		def triange_yes_or_no(side1, side2, side3):
			if side1 + side2 > side3 and side3 + side2 > side1 and (side1 + side3 > side2):
				return 1
			return 0
		for i in range(N - 2):
			hello = triange_yes_or_no(A[0], A[1], A[2])
			lis.append(hello)
			del A[0]
		return lis