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

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