We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Priyanka and Toys
  5. Discussions

Priyanka and Toys

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 411 Discussions, By:

votes

Please Login in order to post a comment

  • roadatlas
    5 years ago+ 5 comments

    Going to be honest here and say that this is my third foray into HackerRank (first one was a work thing, second one was a quiz for a potential job opportunity) and I have to say that these test write-ups for what is expected is REALLY difficult to suss out.

    No, I'm not a college graduate, no I don't have extensive experience in high level math, but I'm pretty good at reading comprehension and the way this stuff is written makes my head hurt trying to figure out what is expected.

    And I've been doing code reading and writing for a living for the past 25 years.

    Perhaps you guys could offer a tutorial on how to read the test descriptions given that the use of this site for applicant filtering seems to have become an industry standard?

    Just a thought.

    Thanks! Mike Poz

    107|
    Permalink
    View more Comments..
  • gauravdhingra_g1
    5 years ago+ 8 comments

    I am sharing my solution below, feel free to leave comments please.

    #!/usr/bin/python3
    
    N = int(input())
    W = set(map(int, input().split()))
    
    i = 0
    while len(W) > 0:
        i += 1
        m = min(W)
        W = W.difference(set(range(m, m + 5)))
    
    print(i)
    
    26|
    Permalink
    View more Comments..
  • humoyun_ahmad
    6 years ago+ 4 comments

    I think the question is a bit ambigious: "Minimum units with which Priyanka could buy all of toys", I think "Minimum group of units with which Priyanka could buy all of toys" would explain the problem statement better, and in the given example it would be [(1,2,3),(10),(17)] -> 3 groups of units

    20|
    Permalink
    View more Comments..
  • saif01md
    5 years ago+ 5 comments
    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) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int[] a=new int[n];
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
            }
            Arrays.sort(a);
            int cost=1;
            int p=a[0];
            for(int i=0;i<n;i++){
                if(a[i]>p+4){
                    cost=cost+1;
                    p=a[i];
                }
            }
            System.out.println(cost);
        }
    }
    
    16|
    Permalink
    View more Comments..
  • __spartax
    6 years ago+ 6 comments

    my code friends

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        vector<int> v(n);
        for(int i = 0; i < n; ++i) cin >> v[i];
        sort(v.begin(), v.end());
        int cost = 0;
        for(int i = 0; i < n; i = upper_bound(v.begin(), v.end(), v[i]+4) - v.begin())
            cost++;
        cout << cost;
        return 0;
    }
    
    7|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature