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 on Trending Technologies
Technical articles with clear explanations and examples
A 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 MorePrint the given pattern recursively
In C programming, we can create various patterns using recursive functions. A recursive function is one that calls itself repeatedly until a base condition is met. Here we'll learn to print a star pattern where each row contains an increasing number of stars. Syntax void printStars(int n); void printPattern(int n); Algorithm START Step 1 → function printStars(int n) If n > 0 printStars(n-1) Print * End IF End Step 2 → function printPattern(int n) ...
Read MorePrint values of 'a' in equation (a+b) <= n and a+b is divisible by x
Given an equation where we need to find values of 'a' such that a+b ≤ n and (a+b) is divisible by x. This problem involves finding all valid values of 'a' that satisfy both the sum constraint and divisibility condition. Syntax for (divisible = (b / x + 1) * x; divisible = 1) { // divisible - b gives us the value of 'a' } } Algorithm START Step 1 → Declare variables b=10, x=9, n=40 and flag=0, divisible Step ...
Read MorePrint n smallest elements from given array in their original order
Given an array of elements, the program must find the n smallest elements and display them in their original order of appearance in the array. Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Output : 1, 2, 3 Input k is 3 it means 3 smallest elements among the set needs to be displayed in original order like 1 than 2 and than 3 Syntax void findNSmallest(int arr[], int size, int k); Algorithm START Step 1 −> start variables as int i, max, pos, j, k=4 ...
Read MorePrint n terms of Newman-Conway Sequence
The Newman-Conway Sequence is a fascinating mathematical sequence that generates integers using a recursive formula. It starts with 1, 1 and each subsequent term is calculated based on previous values in the sequence. Syntax P(n) = P(P(n - 1)) + P(n - P(n - 1)) where P(1) = P(2) = 1 Algorithm The algorithm to generate n terms of Newman-Conway sequence is − START Step 1 → Input variable n (e.g. 20) Step 2 → Initialize variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 → Loop For i=3 and i
Read MorePrint the kth common factor of two numbers
Given two numbers x and y, we need to find their kth common factor. Common factors are the numbers that divide both x and y without leaving a remainder. Syntax int findKthCommonFactor(int x, int y, int k); Algorithm The approach to find the kth common factor is − Find the smaller of the two numbers (maximum possible common factor) Iterate from 1 to the smaller number Check if the current number divides both x and y Count common factors and return when we reach the kth factor Example Let's ...
Read MorePrint N lines of numbers such that every pair among numbers has a GCD K
In C programming, we can print N lines of numbers where every pair among the numbers has a specific GCD (Greatest Common Divisor). This involves generating sequences of numbers that maintain the required GCD property. What is GCD? GCD stands for Greatest Common Divisor of two or more integers (excluding 0). It is the largest positive integer that divides all the given numbers without remainder. For example, to find the GCD of 48 and 180 − 48 = 2 × 2 × 2 × 2 × 3 180 = 2 × 2 × 3 × ...
Read More