Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bitwise operators. For example if your number is 10 then convert to binary 10 =>00001010 if 10 > 3 then shift the binary number 2 bits Now num will be 00000010 ie, 2 Now sum will be 2 num = (shift the number by 2 bits)+(number BITWISE AND 3) num = 2+2 Now the number will be 4 then, 4>3 => true…
nikoo28
nikoo28
a techsavvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone and Canon Kiss X5 in order to capture moments in my life. It's my pleasure to have you here.


Given a number ‘n’, how to check if n is a Fibonacci number. A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A number is Fibonacci if and only if one or both of 5n2+4 or 5×24 is a perfect square (Source: Wikipedia). Following is a simple program based on this concept. Output: 1 is a Fibonacci…

Question: Suppose you are given a Linked List as 45> 123> 87> 11> 53> 24> 412> 22. And we have to find the nth node from the end. Input: n = 3 Output: 24 BRUTE FORCE APPROACH: In this method, start with the first node and count how many nodes are there after that node. If the number of nodes are < n1, then return saying, “fewer number of nodes in the list.” If the number of nodes are > n1 then go to the next node. Continue this until…

We covered some of the basic operations on a Linked List in this post. Basic operations on a Linked List However, in order to create a functional Linked List, we need even more operations before we can proceed further. Some of these operations are: Inserting a node in the beginning of the Linked List. Inserting a node in the middle of a List. Inserting at a certain position Inserting after a specific node. Deleting a node from the Linked List. Deleting the entire Linked List. Inserting a node in the…

Here are some of the basic operations on a singly Linked List. Creating a new List Adding new elements Traversing a List Printing a List Basic functions to perform the above techniques have been defined in the code below. Creating a new list A new Linked List creation means that we do not have any elements in the list and we want to start fresh. This means allocating space in the memory for a node and then inserting the data into it. Since only a single node is created, the…

Linked List is a data structure used to store collections of data where successive elements are connected by pointers. It is really helpful to perform faster and memory efficient operations. This post gives you an introduction to all that with a video explanation.

So if you supply n = 4 in the function what you get as the output is this. 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 You can download the code here.

Backtracking is the method of exhaustive search using divide and conquer. Sometimes the best algorithm for a problem is to try all the possibilities. This is always slow, but there are standard tools that can be used for help. Tools: algorithms for generating basic objects, such as binary strings [2n possibilities for nbit string], permutations [n!], combinations [n!/r!(nr)!], general strings [kary strings of length n has kn possibilities], etc… Backtracking speeds the exhaustive search by pruning. Example Algorithms of Backtracking Binary Strings: generating all binary strings Generating kary Strings. The…

The tower of Hanoi is a mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks on one rod in ascending order of size, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, satisfying the following rules: Only one disk can be moved at a time. Each move consists of taking the upper disk from one of the…

Each recursive call makes a new copy of that method (more specifically speaking ‘the variables’) in memory. Once a method ends (i.e returns some data), the copy of that returning method is removed from the memory. The recursive solutions look simple but visualization and tracing takes time. For better understanding, let us see the following example. For this example. if we call the print function with n = 4, visually our memory assignment may look like this: Now, let us consider our factorial function. The visualization of this will be: