Programming Articles - Page 2518 of 3366

Add 1 to number represented as array (Recursive Approach)?

Arnab Chakraborty
Updated on 31-Jul-2019 12:24:28

332 Views

In this section we will see one interesting problem. Suppose one number is given. We have to increase this number by 1. This is extremely simple task. But here we will place the number as an array. each digit of that number is placed as an element of the array. If the number is 512, then it will be stored as {5, 1, 2}. And also we have to increase the number using recursive approach. Let us see the algorithm to get the clear idea.Algorithmincrement(arr, n, index)Initially the default value of index is 0begin    if index < n, then ... Read More

Add 1 to a number represented as linked list?

Arnab Chakraborty
Updated on 31-Jul-2019 12:20:04

223 Views

Here we will see how to add 1 with a number stored into a linked list. In the linked list, each digit of the numbers is stored. If the number is 512, then it will be stored like below −512 = (5)-->(1)-->(2)-->NULLWe are providing the list into the increment function. That will return another list after adding 1 with it. Here we are using the C++ STL linked list. Let us see the algorithm to bet better idea.AlgorithmincrementList(l1)Begin    carry := 1    res := an empty list    for each node n from l1, scan from last to first, ... Read More

Active and Inactive cells after k Days?

Arnab Chakraborty
Updated on 31-Jul-2019 12:16:53

361 Views

Here we will see one interesting problem. Suppose one binary array is given of size n. Here n > 3. A true value or 1 value indicates that the active state, and 0 or false indicates inactive. Another number k is also given. We have to find active or inactive cells after k days. After every day state of ith cell will be active if the left and right cells are not same, if they are same, then it will be inactive. The left most and right most cell has no cell before and after it. So left most and ... Read More

Rat in a Maze with multiple steps or jump allowed?

Arnab Chakraborty
Updated on 31-Jul-2019 12:13:48

480 Views

The rat in maze problem is one of the well-known problem of the backtracking. Here we will see that problem with little variation. Suppose one NxN maze M is given. The starting point is top left corner M[0, 0], and the destination is right bottom corner M[N – 1, N - 1]. One rat is placed at the starting point. Our goal is to find a path from starting point to ending point that can be by the rat to reach the destination. Here the rat can jump (The variation). Now there are some constraintsThe rat can move either towards ... Read More

A Puzzle on C/C++ R-Value Expressions?

Arnab Chakraborty
Updated on 31-Jul-2019 12:08:17

185 Views

Here we will see one puzzle. Suppose there is a program which is given as below, we have to tell what will be the output and why?Example#include using namespace std; int main() {    int x = 0xab;    ~x;    cout

A Pancake Sorting Problem?

Arnab Chakraborty
Updated on 31-Jul-2019 12:06:03

230 Views

Here we will see another sorting problem named Pancake sort. This problem is simple. We have one array. We have to sort this. But we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position.This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. Let us see the algorithm to get the idea.AlgorithmpancakeSort(arr, n)Begin    size := n    while size > 1, do       index := index of max element in arr from [0 to size ... Read More

A nested loop puzzle?

Arnab Chakraborty
Updated on 31-Jul-2019 12:03:12

452 Views

In this section we will see one interesting problem. We will see two code segments. Both are with two nested loops. We have to identify which one will run faster. (We will assume that the compiler is not optimizing the code).Segment 1for(int i = 0; i < 10; i++){    for(int j = 0; j

A matrix probability question ?

Arnab Chakraborty
Updated on 31-Jul-2019 12:01:37

210 Views

Here we will see one matrix probability problem. We have one rectangular matrix. We can move four directions from the current cell with equal probability. These four directions are left, right, up and down. We have to calculate the probability after N moves from position M[i, j].Here we will do something related to DFS. We will traverse recursively traverse in each of the four possible rooms from the current room. Then we will calculate the probability with one less move. As each of the four directions has equal probability, then each direction will contribute 0.25 of total probability. If we ... Read More

What is the importance of Runtime class in Java?

raja
Updated on 22-Nov-2023 09:53:24

1K+ Views

The java.lang.Runtime class is a subclass of Object class, can provide access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program. The Runtime class does not have any public constructors, so a program cannot create its own instances of the class. A program must call the getRuntime() method to get a reference to the current Runtime object. The important methods of Runtime class are addShutdownHook(), exec(), exit(), freeMemory(), gc(), halt() and load(). Syntax public class Runtime extends Object Example public class RuntimeTest { ... Read More

How to set the shortcut key to a JCheckBox in Java?

Alshifa Hasnain
Updated on 21-May-2025 12:34:41

356 Views

In this article, we will learn to set the shortcut key to a JCheckBox in Java. To create a JCheckBox with a keyboard shortcut (Alt + C), we will use Java Swing. When we either click the checkbox with the mouse or press Alt+C, the checkbox toggles, and a message dialog is displayed. We can do this by using the setMnemonic('C') method to assign the shortcut key and an ActionListener to respond to the checkbox selection. What is a JCheckBox? A JCheckBox is a subclass of JToggleButton, and it can be a small box that is either checked or unchecked. When ... Read More

Advertisements