Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Count the array problem solution

YASH PAL, 31 July 2024
In this HackerEarth Count the array problem solution You are given an integer P. Also, you are given Q queries of the following type:
  1. N: Determine the count of distinct arrays of size <= N and >= 1 such that:
  2. Each array element is a prime number
  3. Product of the value of all the array elements is <= P
  4. Array formed is palindromic
HackerEarth Count the array problem solution

HackerEarth Count the array problem solution.

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int MAX_N = 1e6 + 14;
vector<int> primes;
bool is_not_prime[MAX_N];
int p;

int bt(int n, int z, int mul = 1) {
if (n == 0)
return 1;
int ans = 0;
for (auto pr : primes) {
if (z * pr * mul > p)
break;
ans += bt(n - 1, 2, mul * z * pr);
}
return ans;
}

int main() {
ios::sync_with_stdio(0), cin.tie(0);
for (int i = 2; i < MAX_N; ++i)
if (!is_not_prime[i]) {
primes.push_back(i);
for (int j = i; j < MAX_N; j += i)
is_not_prime[j] = true;
}
int q;
cin >> p >> q;
int ans[21] = {};
for (int i = 1; i < 21; ++i)
ans[i] = bt((i + 1) / 2, 2 - i % 2);
while (q--) {
int n;
cin >> n;
cout << accumulate(ans, ans + min(n + 1, 21), 0) << ' ';
}
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes