Search This Blog

Tuesday, November 18, 2014

Hacker Rank - Weekly Challenges - Week 12

Hacker Rank - Weekly Challenges - Week 12 

Weekly Challenges are contests hosted by Hackerrank. We are given some set of problems to solve. You can see more about weekly challenges here.

Currently one problem is available to solve for this week.


  1. Priyanka and Toys
Following is the solution to the problem.

package hackerRank;

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class PriyankaAndToys
{
       public static void main (String[] args) throws java.lang.Exception
       {
              // your code goes here
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              int count=1; int c =0;
              int[] array = new int[n];
              //aray
              for(int i=0;i<n;i++)
                     array[i] = sc.nextInt();
                    
              Arrays.sort(array);
              if(array.length >1)
              {
                     //constraint
                     c = array[0];
                    
                     for(int i=1;i<n;i++)
                     {
                           if(array[i]>=c && array[i]<=c+4 )
                                  continue;
                           else
                                  count++;
                                  c = array[i];
                     }
              }
              else if(array.length==1)
                     count =1;
              System.out.println(count);
              sc.close();
       }
}

No comments:

Post a Comment

Labels