HackerRank:Priyanka and Toys
Little Priyanka visited a kids' shop. There areN toys and their weight is represented by an array W=[w1,w2,…,wN] . Each toy costs 1 unit, and if she buys a toy with weight w′ , then she can get all other toys whose weight lies between [w′,w′+4] (both inclusive) free of cost.
https://codepair.hackerrank.com/paper/o5dpMap8?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
int n = ni();
int[] a = na(n);
Arrays.sort(a);
int re = -1;
int ct = 0;
for(int i = 0;i < n;i++){
if(a[i] > re){
re = a[i] + 4;
ct++;
}
}
out.println(ct);
}
Little Priyanka visited a kids' shop. There are
https://codepair.hackerrank.com/paper/o5dpMap8?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
int n = ni();
int[] a = na(n);
Arrays.sort(a);
int re = -1;
int ct = 0;
for(int i = 0;i < n;i++){
if(a[i] > re){
re = a[i] + 4;
ct++;
}
}
out.println(ct);
}