Number of N-Digit Stepping Numbers in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:21:46

233 Views

The stepping number is a number where the difference between the consecutive digits is 1. You are given a number n representing the number of digits. You need to count the total number of stepping numbers with n digits.Let's see an example.Input2Output17The lowest number of 2 digits is 10 and highest number of 2 digits is 99. There are 17 stepping numbers in between them.AlgorithmInitialise the number n.Initialise the count to 0.Find the lowest number of n digits i.e.., pow(10, n - 1).Find the highest number of n digits i.e.., pow(10, n) - 1.Write a loop that iterates from the ... Read More

Number of Moves Required to Guess a Permutation in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:14:20

180 Views

Given a number N, we need to find the moves required to guess the permutation in the worst-case scenario. The number of moves required to guess the permutation will be n!. Let's take an example.Input5Output129When we have 5 elements, then we have 5 ways to guess, and 4 ways when we have 4 elements and it continuous till 1.AlgorithmInitialise the number n.Initialise count to 1.Write a loop that iterates from 1 to n.Multiply count with the current number.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNumberMoves(int n) {    int count ... Read More

Number of Leaf Nodes in Subtree of Every Node in N-ary Tree in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:07:20

471 Views

In this tutorial, we are going to write a program that finds the number of leaf nodes for every node in the n-ary tree.Given a n-ary tree, we have to find the number of leaf nodes for every subtree. Let's see an example.InputN = 8 tree = [[2, 3], [], [4, 5, 6], [7, 8], [], [], [], []]Output1->5 2->1 3->4 4->2 5->1 6->1 7->1 8->1AlgorithmInitialise the n-ary tree with tree you like.Use the DFS to traverse through the tree.Maintain an array to store the count of each node leaf nodes count.Increment the count of leaf node after the recursive ... Read More

Number of Leading Zeros in Binary Representation of a Given Number in C++

Hafeezul Kareem
Updated on 26-Oct-2021 14:53:53

657 Views

Given a number, we have to find the number of leading zeroes in the binary representation of it. Assuming total bits to be 32. Let's see an example.Input5Output25The binary representation of 5 is 00000...00101. The number of leading zeroes are 29.AlgorithmInitialise the number n.Find the binary representation of n.Subtract the length of binary representation of n from the total number of bits i.e.., 32.Return the result.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getLeadingZeroesCount(unsigned int n) {    int totalBits = sizeof(n) * 8;    string binary = "";    while (n) { ... Read More

Number of Larger Elements on Right Side in a String in C++

Hafeezul Kareem
Updated on 26-Oct-2021 14:45:24

217 Views

Given a string, we have to count the number of larger elements on right side of each character. Let's see an example.Inputstring = "abc"Output2 1 0There are 2 larger elements than a on its right side.There is 1 larger element than b on its right side.There are 0 larger elements than c on its right side.AlgorithmInitialise the string.Initialise an array to keep track of the count.Write two loops to iterate over the string.Take one char at a time and compare it with all the character after it.Increment the corresponding character count in the count array if the current element is ... Read More

Insert Temporary Text in a Tkinter Entry Widget

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 14:43:03

24K+ Views

To insert a temporary text in a tkinter Entry widget, we will bind the event with the Entry widget and call a user-defined function to delete the text inside the Entry widget.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a user-defined method "temp_text()" to capture the event and delete the temporary text inside the Entry widget.Create an Entry widget inside the Root window and set its properties such as background color, width, and border width.Use the insert() method of the Entry widget to insert a string ... Read More

Update Button Widget in Tkinter

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 14:39:35

3K+ Views

We can update a Button widget in Tkinter in various ways, for example, we can change its size, change its background color, or remove its border, etc. In the following example, we will create three Button widgets and each of the buttons, upon clicking, will call a different function to update their features.Example# Import the required library from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() # Define geometry of the window win.geometry("700x300") # Function to Increase the Size of the Button def Button_Size(): button1.configure(font=('Calibri ... Read More

Number of Integral Solutions of the Equation x1 + x2 + ... + xn = k in C++

Hafeezul Kareem
Updated on 26-Oct-2021 14:35:40

417 Views

The solutions for the equation areThe number of non-negative integral solutions of the equation are $\left(\begin{array}{c}n-k+1\ k\end{array}\right)$The number of positive integral solutions of the equation are $\left(\begin{array}{c}k-1\ n-1\end{array}\right)$Add both to get the required answer. Let's see an example.Inputn = 4 k = 7Output140 AlgorithmInitialise the numbers n and k.Find the integral solutions of not-negative and positive numbers.Add both of them.Return the answer.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int factorial(int n) {    int product = 1;    for (int i = 2; i Read More

Show the Status of Caps Lock Key in Tkinter

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 14:32:34

1K+ Views

We can use the and bindings to check if the CAPS Lock Key is ON or off. In the following example, we will create two user-defined functions "caps_lock_on()" and "caps_lock_off()" which will capture the event of Lock-KeyPress and Lock-KeyRelease and print the status on the screen.Example# Import required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame win = Tk() # Define the geometry of the window win.geometry("700x250") win.title("CAPS Lock Status") def caps_lock_on(e): label_caps.config(text="CAPS Lock is ON") def caps_lock_off(e): label_caps.config(text="CAPS ... Read More

Call a Function Using OptionMenu Widget in Tkinter

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 14:28:55

5K+ Views

Let's take an example and see how to call a function using OptionMenu widget in Tkinter. In the example, we will use a StringVar object and call its get() method. A StringVar object in Tkinter can help manage the value of a widget.We will create an OptionMenu widget and fill it with a list of strings. When the user selects an option, it will invoke a function which in turn will print the selected option as a label.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a set of ... Read More

Advertisements