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 Assorted Arrangement problem solution

YASH PAL, 31 July 2024
In this HackerEarth Assorted Arrangement problem solution, You have a set of n distinct positive numbers. You also have m colors. Your colors are labeled from 1 to m. You’re also given a list c with m distinct integers.
You paint the numbers according to the following rules: For each i in order from 1 to m, paint numbers divisible by c[i] with color i. If multiple rules apply to a number, only the last rule applies.
You sorted your set of numbers and painted them. It turns out that no number was left unpainted. Unfortunately, you lost the set of numbers you had originally. Return the smallest possible value of the maximum number in your set. The input will be constructed such that it’s guaranteed there is at least one set of numbers that is consistent with the given information.
HackerEarth Assorted Arrangement problem solution

HackerEarth Assorted Arrangement problem solution.

import java.util.Scanner;

public class AssortedArrangement {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt();
int[] c = new int[m];
for (int i = 0; i < m; i++) c[i] = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < n; i++) x[i] = in.nextInt()-1;
int last = 0;
for (int i = 0; i < n; i++) {
last = (last/c[x[i]]+1)*c[x[i]];
while (true) {
boolean ok = true;
for (int j = x[i]+1; j < m; j++) {
if (last % c[j] == 0) {
ok = false;
break;
}
}
if (ok) break;
last += c[x[i]];
}
}
System.out.println(last);
System.exit(0);
}
}

Second solution

#include<bits/stdc++.h>

using namespace std;

const int N = 500031;

int n, m;
int mark[N];
int ans;

bool bad(int val, int ps)
{
if (val%mark[ps])
return true;
for (int i = ps + 1; i <= m; i++)
{
if (val%mark[i] == 0)
return true;
}
return false;
}

int main(){
ios_base::sync_with_stdio(0);

cin >> n >> m;

for (int i = 1; i <= m; i++)
{
cin >> mark[i];
}

ans = 0;
for (int i = 1; i <= n; i++)
{
++ans;
int val;
cin >> val;
while (bad(ans, val))
++ans;
}

cout << ans << endl;

return 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