Saturday 13 August 2016

Making Polygons

Problem : You have  sticks with positive integer lengths, . You want to create a polygon using all  sticks. Because this is not always possible, you can cut one or more sticks into two smaller sticks (they do not necessarily need to be of integer length) and repeat the process of trying to create a polygon using all the sticks. Given the lengths of all  sticks, find and print the minimum number of cuts necessary to make a non-degenerate polygon.

Solution : 

For a polygon to be non degenerate polygon,each side of the polygon must satisfy this condition sum=a1 +a2+........+an and ai<1/2(sum) wher 1<=i<=n .
Every time above condition is violated , we increase the counter which is nothing but the minimum number of cuts


IMPLEMENTATION: 


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int a[] = new int[n];
        for(int a_i=0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
        int x= 0;
        int sum=0;
        int count=0;
        //sum=0;
            for(int k:a){
              //  if(k==x)continue;
                sum+=k;
            }
        for(int i=0;i<n;i++){
            x=a[i];
            
            if(x>=sum/2) count++;
            
        }
        
        System.out.println(count);
    }

}

No comments:

Post a Comment