• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Find the kth number with prime factors 3, 5 and 7

July 17, 2014 by Dhaval Dave

Find the kth number with prime factors 3, 5 and 7

Design an algorithm such that we have to find the k th element in the array such that their only prime factors are 3,5 an 7.
Example: Array will contain 1,3,5,7,9,15,21,25,…
We have to return the kth element in array

Algo

Step 1: Initialize three queues say q3,q5,q7 and an integer variable say x =1.
Step 2: for i=1  to k-1
a) Insert x*3,x*5,x*7 into q1,q2,q3 respectively
b) x=minimum (q1.front, q2.front, q3.front)
c) if x==q1.front then do q1.pop
d) if x==q2.front then do q2.pop
e) if x==q3.front then do q3.pop
Step 3:  Print x
Code in C++ :
 
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int k=10;
    int a=3,b=5,c=7,x=1;
    queue<int>q3,q5,q7;
    for(int i=1;i<k;i++)
    {
        q3.push(x*3);
        q5.push(x*5);
        q7.push(x*7);
        x=min(q3.front(),min(q5.front(),q7.front()));
        if(x==q3.front())
        {
            q3.pop();
        }
        if(x==q5.front())
        {
            q5.pop();
        }
        if(x==q7.front())
        {
            q7.pop();
        }
    }
    cout<<x<<endl;
    return 0;
}
Complexity:
 
O(k) time complexity
Thanks Dheeraj for providing Question and Solution.

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Mathematical, Queue

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Maximum sum contiguous subarray of an Array

Fibonacci Hashing & Fastest Hashtable

Apriori algorithm C Code Data Mining

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Binary Tree Isomorphic to each other

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

DFS (Depth First Search)

Number of Islands BFS/DFS

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Search element in a matrix with all rows and columns in sorted order

Binary Tree in Java

Templates in C++

Convert number to words java

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Trapping Rain Water

Flipkart Set 1 On Campus with Answers

System Design: Designing a LLD for Hotel Booking

Python Array String

Check Binary Tree is Binary Search Tree or not

K’th Largest Element in BST when modification to BST is not allowed

The greedy coins game Dynamic Programming

Cisco Hiring Event 21st – 22nd Feb 2015

Mirror of Tree

Walmart Labs Interview Experience

Given Set of words or A String find whether chain is possible from these words or not

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Skiing on Mountains Matrix

Knight Tour Problem (Graph – Breadth First Search)

Maximum path sum between two leaves

Copyright © 2025 · Genesis Framework · WordPress · Log in