Sort by

recency

|

363 Discussions

|

  • + 0 comments

    Java solution with sieve of eratosthenes for primes

    public static List<Integer> waiter(List<Integer> number, int q) {
            Stack <Integer> supportStack = new Stack<>();
            List<Integer> result = new ArrayList<>();
            int max = getMax(number);
            List<Integer> primes = getPrimes(max, number.size());
           
            for (int i = 0; i < q; i++) {
                int prime = primes.get(i);
                for (Integer n : number) {
                    if (n % prime == 0) {
                        result.add(n);
                    } else {
                        supportStack.push(n);
                    }
                }
                number.clear();
                while (!supportStack.isEmpty()) {
                    number.add(supportStack.pop());
                }
            }
            for (int i = number.size() - 1; i >= 0; i--) {
                result.add(number.get(i));
            }       
            return result;
        }
        
        private static int getMax(List<Integer> number) {
            int max = 0;
            for (Integer i : number) {
                if (i > max) {
                    max = i;
                }
            }
            return max;
        }
        
        private static List<Integer> getPrimes (int n, int cap) {
            List<Integer> primes = new ArrayList<Integer>();
            boolean[] prime = new boolean[n + 1];
            for (int i = 0; i <= n; i++) {
                prime[i] = true;
            }
    
            for (int p = 2; p * p <= n; p++) {
                if (prime[p]) {
                    for (int i = p * p; i <= n; i += p)
                        prime[i] = false;
                }
            }
    
            for (int i = 2; i <= n; i++) {
                if (prime[i] && primes.size() < cap)
                    primes.add(i);
            }
            return primes;        
        }
    
  • + 0 comments
    bool isprime(int n)
    {
        for(int i=2;i<(n/2)+1;i++)
        {
            if(n%i == 0)
            {
                return false;
            }
        }
        return true;
    }
    vector<int> waiter(vector<int> number, int q) 
    {
        vector<int> p,a,b,res;
        vector<int> temp;
        int x;
        for(int i=2;i<10000;i++)
        {
            if(isprime(i))
            {
                p.push_back(i);
            }
        }    
        
        for(int i=0;i<q;i++)
        {
            int size = number.size();
            for(int j=0;j<size;j++)
            {
                x = number.back();
                
                if(x%p[i] == 0)
                {
                    b.push_back(x);
                    number.pop_back();
                }
                else
                {
                    a.push_back(x);
                    number.pop_back();
                }
            }
            number = a;
            a.clear();
            while(!b.empty())
            {
                res.push_back(b.back());
                b.pop_back();
            } 
        }
        while(!number.empty())
        {
            res.push_back(number.back());
            number.pop_back();
        }
        return res;
    }
    
  • + 0 comments

    java solution

    class Result {

    /*
     * Complete the 'waiter' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY number
     *  2. INTEGER q
     */
     public static boolean isprime(int n ) {
         for (int i = 2 ; i <= n+2 ; i++) {
             if ( i!=n && n%i==0) {
                 return false;
             }
         }
         return true;
     }
    

    public static List waiter(List number, int q) { // Write your code here

           Stack <Integer> sc = new Stack<>();
           Stack <Integer> scB = new Stack<>();
           List<Integer> ans = new ArrayList<>();
           int prime = 1 ;
    
       //for prime  
       for (int i = 1 ; i <= q ; i++ ) {
          prime++;
          while (!isprime(prime)) {
              prime++;
          } 
          //process 
           if (i == 1 ) { 
               for (int k = number.size()-1; k>=0 ; k--) {
                   if (number.get(k) % prime == 0) {
                       scB.push(number.get(k));
                        //System.out.println(scB.peek() +"  " + i+ "scb");
                   } else {
                       sc.push(number.get(k));
                       // System.out.println(sc.peek() +"  " + i+ "sc");
                   }
               }
    
           } else {
                Stack <Integer> scA1 = new Stack<>();
                  while ( !sc.empty()){ 
                    if (sc.peek()% prime ==0) {
                       scB.add(sc.pop());
                       //System.out.println(scB.peek() +"  " + i+ "scb");
                   } else {
                       scA1.add(sc.pop()); 
                           // System.out.println(scA1.peek() + "   " +i + "sca1");
                   }
                }
                sc = scA1;
           }
    
    
          while (!scB.empty()) {
              ans.add(scB.pop());
             // System.out.println(scB.pop() +"    /"+ i + "  scbpr");
    
          }
      }
    
          while (!sc.empty()) {
              ans.add(sc.pop());
          }
       return ans;
    }
    

    }

  • + 0 comments

    Python

    def get_primes_till_N(N):
        """
        Creates an array of primes till N.
        """
        if N < 2:
            return []
        
        # Initialize a list to track prime numbers
        is_prime = [True] * (N + 1)
        is_prime[0], is_prime[1] = False, False  # 0 and 1 are not primes
        
        # Apply the Sieve of Eratosthenes algorithm
        for start in range(2, int(N**0.5) + 1):
            if is_prime[start]:
                for multiple in range(start*start, N + 1, start):
                    is_prime[multiple] = False
        
        # Collect all prime numbers into a list
        primes = [num for num, prime in enumerate(is_prime) if prime]
        
        return primes
    
    # Create array of primes till 10001:
    primes = get_primes_till_N(10001)
    
    def waiter(number, q):
        answers = list()
        A = number[:]
    
        for i in range(q):
            # Finish iteration if A is empty
            if not A:
                break
            # Store values in pile B
            B = [num for num in A if num % primes[i] == 0][::-1]
            # Remove values from pile A
            A = [num for num in A if num % primes[i] != 0][::-1]
            # Add values of pile B to answers:
            answers += B[::-1]
            
        return answers + A[::-1]
    
  • + 0 comments
    def is_prime(num):
        if num < 2:
            return False
        for i in range(2, int(num**0.5) + 1):
            if num % i == 0:
                return False
        return True
    
    def first_n_primes(n):
        primes = []
        num = 2
        while len(primes) < n:
            if is_prime(num):
                primes.append(num)
            num += 1
        return primes
    
    
    def waiter(number, q):
        primes = first_n_primes(q)
        answers = []
        arr = number
        for p in primes:
            B=[]
            A =[]
            for e in arr:
                if e % p == 0:
                    B.append(e)
                else:
                    A.append(e)
            answers+=B
            arr = A[::-1]
        answers +=A
        return answers