Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Count the array problem solution

YASH PAL, 31 July 202413 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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