Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIProgram to find the number from given holes

Program to find the number from given holes

Given a number H which represent the total number of holes. The task is to find the smallest number which has that many numbers of holes. 
NOTE: 
 

  1. 0, 4, 6, 9 has 1 holes each and 8 has 2 holes in it.
  2. The number should not contain leading zeros.

Examples: 
 

Input: H = 1 
Output: 0
Input: H = 5 
Output: 488 
Explanation: 
Number which has 5 holes in it is 488. i.e (1 + 2 + 2) 
 

Refer: Count the number of holes in an integer
 

Approach: 
 

  1. First of all, Check whether the number of holes given is 0 or 1, if 0 then print 1 and if 1 then print 0.
  2. If number of holes given is more than 1 then divide the number of holes by 2 and store the remainder in ‘rem’ variable. and quotient in ‘quo’ variable.
  3. Now, if value of rem variable is equal to 1 then first print 4 once and then print 8 quo number of times.
  4. Else print 8 only quo number of times.

Below is the implementation of the above approach: 
 

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that will find out
// the number
void printNumber(int holes)
{
 
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        cout << "1";
 
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        cout << "0";
 
    // If number of holes
    // is more than 0 or 1.
    else {
        int rem = 0, quo = 0;
 
        rem = holes % 2;
        quo = holes / 2;
 
        // If number of holes is
        // odd
        if (rem == 1)
            cout << "4";
 
        for (int i = 0; i < quo; i++)
            cout << "8";
    }
}
 
// Driver code
int main()
{
    int holes = 3;
 
    // Calling Function
    printNumber(holes);
 
    return 0;
}


Java




// Java implementation of the above approach
import java.io.*;
 
class GFG
{
         
// Function that will find out
// the number
static void printNumber(int holes)
{
 
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        System.out.print("1");
 
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        System.out.print("0");
 
    // If number of holes
    // is more than 0 or 1.
    else
    {
        int rem = 0, quo = 0;
 
        rem = holes % 2;
        quo = holes / 2;
 
        // If number of holes is
        // odd
        if (rem == 1)
            System.out.print("4");
 
        for (int i = 0; i < quo; i++)
                System.out.print("8");
    }
}
 
// Driver code
public static void main (String[] args)
{
    int holes = 3;
     
    // Calling Function
    printNumber(holes);
}
}
 
// This code is contributed by Sachin.


Python3




# Python3 implementation of
# the above approach
 
# Function that will find out
# the number
def printNumber(holes):
 
    # If number of holes
    # equal 0 then return 1
    if (holes == 0):
        print("1")
 
    # If number of holes
    # equal 0 then return 0
    elif (holes == 1):
        print("0", end = "")
 
    # If number of holes
    # is more than 0 or 1.
    else:
        rem = 0
        quo = 0
 
        rem = holes % 2
        quo = holes // 2
 
        # If number of holes is
        # odd
        if (rem == 1):
            print("4", end = "")
 
        for i in range(quo):
            print("8", end = "")
 
# Driver code
holes = 3
 
# Calling Function
printNumber(holes)
 
# This code is contributed by Mohit kumar


C#




// C# implementation of the above approach
using System;
 
class GFG
{
     
// Function that will find out
// the number
static void printNumber(int holes)
{
 
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        Console.Write ("1");
 
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        Console.Write ("0");
 
    // If number of holes
    // is more than 0 or 1.
    else
    {
        int rem = 0, quo = 0;
 
        rem = holes % 2;
        quo = holes / 2;
 
        // If number of holes is
        // odd
        if (rem == 1)
        Console.Write ("4");
 
        for (int i = 0; i < quo; i++)
            Console.Write ("8");
    }
}
 
// Driver code
static public void Main ()
{
    int holes = 3;
     
    // Calling Function
    printNumber(holes);
}
}
 
// This code is contributed by jit_t


Javascript




<script>
    // Javascript implementation of the above approach
     
    // Function that will find out
    // the number
    function printNumber(holes)
    {
 
        // If number of holes
        // equal 0 then return 1
        if (holes == 0)
            document.write("1");
 
        // If number of holes
        // equal 0 then return 0
        else if (holes == 1)
            document.write("0");
 
        // If number of holes
        // is more than 0 or 1.
        else {
            let rem = 0, quo = 0;
 
            rem = holes % 2;
            quo = parseInt(holes / 2, 10);
 
            // If number of holes is
            // odd
            if (rem == 1)
                document.write("4");
 
            for (let i = 0; i < quo; i++)
                document.write("8");
        }
    }
     
    let holes = 3;
   
    // Calling Function
    printNumber(holes);
     
    // This code is contributed by divyeshrabadiya07.
</script>


Output: 

48

 

Time Complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments