给定一个数组arr [] ,任务是在该数组中找到可以形成三角形的按字典顺序排列的最大三元组。如果不存在这样的三元组,则打印-1 。
例子:
Input: arr[] = { 4, 2, 10, 3, 5 }
Output: 3 4 5
Explanation:
The lexicographically largest triplet is (4, 5, 10). But it does not form a triangle.
The next lexicographically largest triplet is (3, 4, 5).
Since it forms a triangle, it is the required answer.
Input: arr[] = { 20, 12, 120, 3, 15 }
Output: 12 15 20
方法:
当且仅当满足以下条件时,三元组才能形成三角形:
If A, B and C forms a triplet, then the triplet can form a triangle if
A < B + C or B < A + C or C < A + B
这个想法是使用排序。以升序对数组进行排序。从数组的末尾开始遍历,并检查是否有任何符合上述条件的三元组。打印第一个三元组以满足条件,作为输出。如果找不到这样的三元组,则打印-1 。
下面是上述方法的实现:
C++
// C++ Program to implement
// the the above approach
#include
using namespace std;
// Function to find lexicographically
// largest triplet that forms a
// triangle in the given array
void findTriplet(int arr[], int N)
{
// Sort the array
sort(arr, arr + N);
int flag = 0, i;
// Iterate from the end of the array
for (i = N - 1; i - 2 >= 0; i--) {
// If the triplet forms a triangle
if (arr[i - 2] + arr[i - 1] > arr[i]) {
flag = 1;
break;
}
}
// If triplet found
if (flag) {
// Print the triplet
cout << arr[i - 2] << " "
<< arr[i - 1] << " "
<< arr[i] << endl;
}
// Otherwise
else {
cout << -1 << endl;
}
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 10, 3, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
findTriplet(arr, N);
return 0;
}
Java
// Java program to implement
// the the above approach
import java.util.*;
class GFG{
// Function to find lexicographically
// largest triplet that forms a
// triangle in the given array
static void findTriplet(int arr[], int N)
{
// Sort the array
Arrays.sort(arr);
int flag = 0, i;
// Iterate from the end of the array
for(i = N - 1; i - 2 >= 0; i--)
{
// If the triplet forms a triangle
if (arr[i - 2] + arr[i - 1] > arr[i])
{
flag = 1;
break;
}
}
// If triplet found
if (flag != 0)
{
// Print the triplet
System.out.println(arr[i - 2] + " " +
arr[i - 1] + " " +
arr[i] );
}
// Otherwise
else
{
System.out.println(-1);
}
}
// Driver Code
public static void main (String []args)
{
int arr[] = { 4, 2, 10, 3, 5 };
int N = arr.length;
findTriplet(arr, N);
}
}
// This code is contributed by chitranayal
Python3
# Python3 implementation of
# the above approach
# Function to find lexicographically
# largest triplet that forms a
# triangle in the given array
def findTriplet(arr, N):
# Sort the array
arr.sort()
# Iterate from the end of the array
i = N - 1
while i - 2 >= 0:
# If the triplet forms a triangle
if(arr[i - 2] + arr[i - 1] > arr[i]):
flag = 1
break
i -= 1
# If triplet found
if(flag):
# Print the triplet
print(arr[i - 2],
arr[i - 1],
arr[i])
# Otherwise
else:
print(-1)
# Driver Code
if __name__ == '__main__':
arr = [ 4, 2, 10, 3, 5 ]
N = len(arr)
findTriplet(arr, N)
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the the above approach
using System;
class GFG{
// Function to find lexicographically
// largest triplet that forms a
// triangle in the given array
static void findTriplet(int []arr, int N)
{
// Sort the array
Array.Sort(arr);
int flag = 0, i;
// Iterate from the end of the array
for(i = N - 1; i - 2 >= 0; i--)
{
// If the triplet forms a triangle
if (arr[i - 2] + arr[i - 1] > arr[i])
{
flag = 1;
break;
}
}
// If triplet found
if (flag != 0)
{
// Print the triplet
Console.Write(arr[i - 2] + " " +
arr[i - 1] + " " +
arr[i] );
}
// Otherwise
else
{
Console.Write(-1);
}
}
// Driver Code
public static void Main (string []args)
{
int []arr = { 4, 2, 10, 3, 5 };
int N = arr.Length;
findTriplet(arr, N);
}
}
// This code is contributed by Ritik Bansal
3 4 5
时间复杂度: O(N * log(N))
辅助空间: O(1)