-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Polygon_With_the_Largest_Perimeter.java
More file actions
48 lines (47 loc) · 1.09 KB
/
Find_Polygon_With_the_Largest_Perimeter.java
File metadata and controls
48 lines (47 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
import java.io.*;
import java.lang.*;
public class Find_Polygon_With_the_Largest_Perimeter {
class Solution {
public long largestPerimeter(int[] nums) {
return fun(nums);
}
public static long fun(int[] nums)
{
int n = nums.length;
int count = 0;
for(int i = 0 ; i<n ; i++)
{
if(nums[i]>0)
{
count++;
}
}
if(count<3)
{
return -1;
}
long max = 0;
long pm = 0;
int maxind = 0;
for(int i = 0 ; i<n ; i++)
{
if(max<nums[i])
{
max = nums[i];
maxind = i;
}
pm = pm + nums[i];
}
if(pm - max > max)
{
return pm;
}
else
{
nums[maxind] = 0;
return fun(nums);
}
}
}
}