Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 19 of 377
All palindrome numbers in a list?
Here we will see one simple problem. We have to find all numbers that are palindrome in nature in a given list. The approach is simple, take each number from list and check it is palindrome or not, and print the number. Syntax bool isPalindrome(int n); void getAllPalindrome(int arr[], int n); Algorithm getAllPalindrome(arr, n) Begin for each element e in arr, do if e is palindrome, then print e ...
Read MoreAdd elements of given arrays with given constraints?
Here we will see a problem where we add two array elements and store them into another array following specific constraints. These constraints are − Addition should be started from 0th index of both arrays Split the sum if it is more than a single-digit number, and place each digit to the corresponding locations Remaining digits of the larger input array will be stored in the output array Syntax void addArrayConstraints(int arr1[], int arr2[], int m, int n); void splitDigit(int num, int* out, int* size); Algorithm addArrayConstraints(arr1, arr2) Begin ...
Read MoreActive and Inactive cells after k Days?
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 MoreRat in a Maze with multiple steps or jump allowed?
The rat in a maze problem is a classic backtracking algorithm. In this variation, the rat can jump multiple steps instead of moving one cell at a time. The rat starts at the top-left corner M[0, 0] and must reach the bottom-right corner M[N-1, N-1]. Each cell contains a number indicating the maximum jump distance allowed from that position. Syntax bool ratMazeSolve(int maze[N][N], int x, int y, int sol[N][N]); Rules The rat can move either towards the right or towards the down. Maze with 0 in cell indicates that the cell is blocked. ...
Read MoreA Puzzle on C/C++ R-Value Expressions?
Here we will see one puzzle about R-value expressions in C. Suppose there is a program which is given below, we have to tell what will be the output and why? Syntax ~expression; // R-value: computes but doesn't store variable = ~expression; // L-value: stores the result Example 1: R-value Expression In this example, the complement operation is performed but not assigned to any variable − #include int main() { int x = 0xab; ~x; /* ...
Read MoreA Pancake Sorting Problem?
Pancake sorting is a unique sorting algorithm that mimics the process of sorting a stack of pancakes by flipping portions of the stack. In this algorithm, we can only use one operation: rev(arr, i), which reverses elements from index 0 to index i. The goal is to sort the entire array using only this flip operation. The algorithm works similarly to selection sort − we repeatedly find the largest element and move it to its correct position at the end of the current unsorted portion. Syntax void rev(int arr[], int i); int maxIndex(int arr[], int n); ...
Read MoreA nested loop puzzle?
In this section we will see one interesting nested loop performance puzzle. We will analyze two code segments with different loop arrangements to understand which one runs faster, assuming the compiler does not optimize the code. The Problem Both code segments execute the same total number of iterations (10 × 100 = 1000), but their performance characteristics differ due to loop overhead − Segment 1: Outer Loop with Fewer Iterations #include int main() { int count = 0; for(int i ...
Read MoreA matrix probability question ?
This problem calculates the probability of staying within a matrix after N moves from a given position. From any cell, we can move in four directions (left, right, up, down) with equal probability (0.25 each). If we cross the matrix boundary, the probability becomes 0. Syntax double matProb(int m, int n, int x, int y, int N); int isSafe(int x, int y, int m, int n); Parameters m, n − Matrix dimensions (m rows, n columns) x, y − Starting position coordinates N − Number of moves to make Algorithm ...
Read MoreC Program for Find largest prime factor of a number?
In this section, we will see how we can get the largest prime factor of a number in an efficient way. There is a number say n = 1092, we have to get the largest prime factor of this. The prime factors of 1092 are 2, 2, 3, 7, 13. So the largest is 13. To solve this problem, we have to follow this rule − When the number is divisible by 2, then store 2 as largest, and divide the number by 2 repeatedly. Now the number must be odd. Now starting from 3 to square root ...
Read MoreC Program for Extended Euclidean algorithms?
The Extended Euclidean Algorithm is used to find the greatest common divisor (GCD) of two integers along with the coefficients x and y such that − ax + by = gcd(a, b) This algorithm extends the standard Euclidean algorithm by not only computing the GCD but also finding the linear combination coefficients. It uses the recursive relation gcd(a, b) = gcd(b mod a, a) while keeping track of the coefficients. Syntax int extendedGCD(int a, int b, int* x, int* y); Algorithm ExtendedEuclidean(a, b, x, y) begin ...
Read More