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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Red John is Back
  5. Discussions

Red John is Back

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • mandarbhatye
    8 months ago+ 0 comments

    public static int redJohn(int n) { int countWays = numberOfWays(n); int totalPrimes = sieve(countWays);

        return totalPrimes;
    }
    
    public static int numberOfWays(int N){
        if (N<=3) return 1;
        if (N==4) return 2;
        return numberOfWays(N-1) + numberOfWays(N-4);
    }
    
    public static int sieve(int n) {
        int count = 0;
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;
    
        for (int p = 2; p * p <= n; p++)
            if (prime[p] == true)
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
    
        for (int i = 2; i <= n; i++)
            if (prime[i] == true)
                count++;
    
        return count;
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy