Skip to content
Related Articles

Related Articles

Improve Article

Find the first N pure numbers

  • Difficulty Level : Easy
  • Last Updated : 20 Apr, 2020

Given an integer N, the task is to print first Nth pure numbers. A number is said to be pure if

  1. It has even number of digits.
  2. All the digits are either 4 or 5.
  3. And the number is a palindrome.

First few pure numbers are 44, 55, 4444, 4554, 5445, 5555, …

Examples:

Input: N = 4
Output: 44 55 4444 5445

Input: N = 10
Output: 44 55 4444 4554 5445 5555 444444 454454 544445 554455



Approach: The idea is similar to the approach discussed here. At each step of the queue we can generate the next number by adding 4 and 5 on both of the sides i.e. the ending and the beginning of the previous number:

q.push("4" + temp + "4");
q.push("5" + temp + "5");

Proceeding in this way we can take care of palindromes and even length numbers

Below is the implementation of the above approach:




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the first n pure numbers
void nPureNumbers(int n)
{
  
    queue<string> q;
    vector<string> ans;
  
    // Push the starting two elements
    q.push("44");
    q.push("55");
    int total = 2;
  
    // While generated numbers are less than n
    while (ans.size() < n) {
  
        string temp = q.front();
        q.pop();
        ans.push_back(temp);
  
        q.push("4" + temp + "4");
        q.push("5" + temp + "5");
    }
  
    // Sorting strings based on their value
    sort(ans.begin(), ans.end(), [](auto s, auto s2) {
        if (s.size() == s2.size())
            return s < s2;
        else
            return s.size() < s2.size();
    });
  
    // Print first n pure numbers
    for (auto i : ans) {
        cout << i << " ";
    }
}
  
// Driver code
int main()
{
    int n = 4;
  
    nPureNumbers(n);
  
    return 0;
}
Output:
44 55 4444 5445

Approach 2: The approach divides the even palindromic number into two equal parts, both of which mirror each other. The idea is to build the first part using the same approach that is used to build a binary counter by flipping every second bit; except, instead of flipping 0 and 1, we’re flipping 4 and 5. Later, we use the first part of the number and the palindromic property to create the final pure number.

44, 55, 4444, 4554... can be separated out into 
 4 | 4
 5 | 5
44 | 44
45 | 54
54 | 45
55 | 55

The numbers mimic the property of binary numbers of first ‘k’ bits where k = 1, 2, 3 and so on.

 0
 1
00
01
10
11

So, we start by flipping the last bit after every iteration, then moving to the (k-1) bit which is flipped after every 2 iterations and so on. The first time, it will be flipped n times and the next, it will be flipped n/2 times and so on.

Since n is a sum of all possible numbers with k bits where k= 1, 2, 3…, our number will be a sum of 2 + 4 + 8 + …2k numbers. We consecutively divide n by 2 and stop if its value is equal to zero as there will be no further bits required to be added and also because, in the next iteration, the number will be flipped after twice the number of elements it took in the previous iteration.

We can use a stack to store the values and then pop them out in sequential order, or by storing the data in a string and simply reversing it, which uses less space than a stack does.




#include <bits/stdc++.h>
using namespace std;
  
// Creating a function 'pure' that returns the pure number 
// in the form of a string.
string pure(int n)
{
    // initializing string p where the number will be stored
    string p; 
  
    // It keeps on adding numbers as long as n>0
    while (n > 0) { 
  
        // To equalize the value of n to the corresponding flips 
        n--; 
        if (n % 2 == 0)
            p.append("4");
        else
            p.append("5");
  
        // Dividing n by 2 to move to the next bit
        n /= 2; 
    }
  
    int len = p.length(); // Taking the length of the string
    reverse(p.begin(), p.end()); // Reversing the string
  
    for (int i = len - 1; i >= 0; i--) // Building the palindrome
        p += p[i];
  
    return p;
}
  
int main()
{
    for (int i = 1; i <= 10; i++)
        cout << pure(i) << " "; // Print Ith pure number
  
    return 0;
}
Output:
44 55 4444 4554 5445 5555 444444 445544 454454 455554

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :