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
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Health of a person problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Health of a person problem solution There N people in your city. The jth person contains Aj health point. You are trying to make these people walk in your locality. You walk for M days. On an ith day, every walk decreases Bj health points of the person you are walking with. Also, on an ith day, you can only walk with the jth person if the person is still healthy and j is a multiple of i. You can walk with as many people as you can but you can walk with a specific person only once a day.
 
A person becomes unhealthy if their health point becomes less than or equal to 0. At the end of each day, their health points are restored to their original level if they are not unhealthy already. Your task is to determine the day in which the ith person becomes unhealthy or will remain healthy.
 
 
HackerEarth Health of a person problem solution

 

 

HackerEarth Health of a person problem solution.

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000009;

int t;
int n, m;

int anss[maxn], health[maxn], pwr[maxn];

int main()
{

cin >> t;
while(t--){

scanf("%d %d", &n, &m);
memset(anss, -1, sizeof(anss));
for(int i = 1; i <= n; i++) scanf("%d", &health[i]);

for(int i = 1; i <= m; i++){
int x;
scanf("%d", &x);
for(int j = i; j <= n; j += i) {
if(anss[j] != -1) continue;
if(health[j] <= x) anss[j] = i;
}
}

for(int i = 1; i <= n; i++) printf("%dn", anss[i]);

}

return 0;
}
 
 

second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 17;
int col[maxn], a[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
memset(col, -1, sizeof col);
for(int i = 1; i <= m; i++){
int b;
cin >> b;
for(int j = i; j <= n; j += i)
if(col[j] == -1 && a[j] - b <= 0)
col[j] = i;
}
for(int i = 1; i <= n; i++)
cout << col[i] << 'n';
}
}
 
 
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