Stacks and Queue Amazon Interview Problem

Here’s a simple approach using Sieve of Eratosthenes and 2 arrays.

num1 = [7,21,18,3,12]    # your list
n = max(num1)
prime = [True for i in range(n+1)] 
p = 2
while (p * p <= n):  #creating a Sieve using standard operations
    if (prime[p] == True): 
        for i in range(p * p, n+1, p): 
            prime[i] = False
    p += 1

prim, comp = [], []
for i in num1:
    if prime[i]:
        prim.append(i)
    else:
        comp.append(i)
for i in prim:
    print(i, end = " ")
print()
for i in comp[::-1]:
    print(i, end = " ")
print()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top