705726. Techfest and the Queue

Medium

Techfest and the Queue

Slug: techfest-and-the-queue1044 Difficulty: Medium Id: 705726 Topic Tags: Prime Number, sieve Company Tags: None

Summary

The Techfest problem "Techfest and the Queue" requires you to solve a problem related to prime numbers and queues. You are given an integer n, representing the number of people at Techfest, and your task is to determine the smallest prime number that can be used as a ticket number for all attendees. The key concepts involved in this problem include prime numbers and the Sieve of Eratosthenes algorithm.

Detailed Explanation

The problem can be solved by using the Sieve of Eratosthenes algorithm, which is an efficient way to generate all primes up to a given limit. Here's a step-by-step breakdown of the solution:

  1. Initialize a boolean array prime of size n+1, where prime[i] represents whether i is a prime number or not.
  2. Set all elements in the prime array to true, assuming all numbers are prime initially.
  3. Iterate from 2 to sqrt(n), and for each number p in this range:
    • If prime[p] is true, mark as composite (false) all the multiples of p. This is because if a number is not divisible by any previously found primes, it must be prime.
  4. After the loop, iterate through the prime array and find the first index where prime[i] is still true. This will be the smallest prime number greater than or equal to n.

Time complexity: O(n log log n) due to the Sieve of Eratosthenes algorithm Space complexity: O(n)

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static void main(String[] args) {
        int n = 10; // Replace with your input value
        boolean[] prime = new boolean[n + 1];
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (prime[i]) {
                for (int j = i * i; j <= n; j += i) {
                    prime[j] = false;
                }
            }
        }
        for (int i = 2; i <= n; i++) {
            if (prime[i]) {
                System.out.println("The smallest prime number is " + i);
                return;
            }
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.