HackerRank Challenge
Construct a polygon from the given set of number. You can break the number also
class Program
{
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
if(IsPolygon(a,n)==false)
{
DoCut(a,n);
}
else
Console.WriteLine(0);
Console.ReadLine();
}
static void DoCut(int[] a,int n)
{
int cut=1;
int sum = 0;
int maxnum=findGreatest(a,n);
int num1=maxnum/2;
int num2 = maxnum - num1;
int[] new_List= new int[n+1];
for(int i=0;i<n;i++)
{
if (a[i] != maxnum)
{
sum = sum + a[i];
}
}
if (num1 > num2)
{
sum = sum + num1;
maxnum=num2;
}
else
{
sum = sum + num2;
maxnum=num1;
}
if (sum > maxnum)
{
Console.WriteLine(cut);
}
}
static bool IsPolygon(int[] a ,int n)
{
int sum=0;
int maxnum=findGreatest(a,n);
for(int i=0;i<n;i++)
{
if(a[i] != maxnum)
{
sum=sum+a[i];
}
}
if(sum > maxnum)
return true;
else
return false;
}
static int findGreatest(int[] a,int n)
{
int greatestnum=0;
for(int i=0 ;i<n ;i++)
{
if(a[i] > greatestnum)
{
greatestnum=a[i];
}
}
return greatestnum;
}
}
class Program
{
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
if(IsPolygon(a,n)==false)
{
DoCut(a,n);
}
else
Console.WriteLine(0);
Console.ReadLine();
}
static void DoCut(int[] a,int n)
{
int cut=1;
int sum = 0;
int maxnum=findGreatest(a,n);
int num1=maxnum/2;
int num2 = maxnum - num1;
int[] new_List= new int[n+1];
for(int i=0;i<n;i++)
{
if (a[i] != maxnum)
{
sum = sum + a[i];
}
}
if (num1 > num2)
{
sum = sum + num1;
maxnum=num2;
}
else
{
sum = sum + num2;
maxnum=num1;
}
if (sum > maxnum)
{
Console.WriteLine(cut);
}
}
static bool IsPolygon(int[] a ,int n)
{
int sum=0;
int maxnum=findGreatest(a,n);
for(int i=0;i<n;i++)
{
if(a[i] != maxnum)
{
sum=sum+a[i];
}
}
if(sum > maxnum)
return true;
else
return false;
}
static int findGreatest(int[] a,int n)
{
int greatestnum=0;
for(int i=0 ;i<n ;i++)
{
if(a[i] > greatestnum)
{
greatestnum=a[i];
}
}
return greatestnum;
}
}
Comments
Post a Comment