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("%d\n", 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';
  }
}