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 15 of 377
A permutation where each element indicates either number of elements before or after it?
In this problem, we need to check whether there exists a permutation of a given array such that each element indicates the number of elements either before or after it in that permutation. For example, consider the array {2, 1, 3, 3}. A valid permutation is {3, 1, 2, 3} where the first 3 indicates there are three elements after it, 1 indicates there is one element before it, 2 indicates there are two elements before it, and the last 3 indicates there are three elements before it. Algorithm The approach uses a frequency map to track ...
Read MoreA backtracking approach to generate n bit Gray Codes ?
In this article, we will learn how to generate n-bit Gray codes using a backtracking approach in C. Gray code is a binary numeral system where successive values differ in only one bit. For n = 3 bits, the Gray code sequence is: 000, 001, 011, 010, 110, 111, 101, 100 (decimal: 0, 1, 3, 2, 6, 7, 5, 4). Syntax void generateGray(int arr[], int *index, int n, int num); Algorithm The backtracking algorithm works recursively − generateGray(arr, index, n, num): if n = 0: arr[*index] = num (*index)++ return generateGray(arr, index, n-1, num) num = num XOR (1
Read MoreGeneric keyword in C ?
The _Generic keyword in C (introduced in C11) enables type-generic programming by allowing different expressions to be selected based on the type of an argument. This provides a solution to the type-safety limitations of traditional macros. Syntax _Generic(expression, type1: expression1, type2: expression2, ..., default: default_expression) Problem with Traditional Macros Traditional C macros lack type checking and perform the same operation regardless of data type − #include #define INCREMENT(X) ++X int main() { int x = 5; float y = 2.56; ...
Read MoreC Program for Egg Dropping Puzzle - DP-11?
The egg dropping puzzle is a classic dynamic programming problem. Given n floors and m eggs, we need to find the minimum number of drops required to determine the highest floor from which an egg can be dropped without breaking. There are some important points to remember − When an egg does not break from a given floor, then it will not break for any lower floor also. If an egg breaks from a given floor, then it will break for all upper floors. When an egg breaks, it must be discarded, otherwise we can use it ...
Read MoreC Program for Basic Euclidean algorithms?
The Euclidean algorithm is an efficient method for finding the Greatest Common Divisor (GCD) of two integers. It works by repeatedly applying the principle that GCD(a, b) = GCD(b, a mod b) until one of the numbers becomes zero. Syntax int euclideanGCD(int a, int b); Algorithm The recursive Euclidean algorithm follows these steps − begin if a is 0, then return b end if return euclideanGCD(b mod a, a) end ...
Read MoreC/C++ Program for Largest Sum Contiguous Subarray?
An array of integers is given. We have to find the sum of all contiguous elements whose sum is largest. This problem is known as the Maximum Subarray Problem and is efficiently solved using Kadane's Algorithm. Using dynamic programming, we store the maximum sum up to the current element. This helps find the optimal contiguous subarray with maximum sum. Syntax int maxSubarraySum(int arr[], int n); Algorithm: Kadane's Algorithm The algorithm maintains two variables − Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of ...
Read MoreC/C++ program to shutdown a system?
Here we will see how we can shut down the system by writing a simple C program. The shutdown process varies in different operating systems. If we are Linux user, we can use this terminal command to shut down − shutdown -P now If we are using Windows system, we can use this command − c:\windows\system32\shutdown /i Note: These programs require administrator/root privileges to execute successfully. On Linux, you may need to run with sudo. On Windows, run the program as administrator. Syntax int system(const char *command); ...
Read MoreC/C++ Program to Count set bits in an integer?
Here we will see how we can count the number of set bits in an integer. Set bits are the bits that have a value of 1 in the binary representation of a number. For example, the number 13 has binary representation 1101, which contains three set bits, so the count will be 3. To solve this problem, we will shift the number to the right and check if the least significant bit (LSB) is 1. If it is, we increment our count. This process continues until the number becomes 0. Syntax int countSetBits(int n); ...
Read MoreC/C++ Program to Count Inversions in an array using Merge Sort?
The inversion count in an array indicates how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions. When the array is sorted in reverse order, the number of inversions is maximum. We can efficiently solve this problem using a modified merge sort algorithm to achieve O(n log n) time complexity. Syntax int merge(int arr[], int temp[], int left, int mid, int right); int mergeSort(int arr[], int temp[], int left, int right); int countInversions(int arr[], int n); Algorithm The approach uses a ...
Read MoreC/C++ Program for Triangular Matchstick Number?
Here we will see how to count number of matchsticks required to make a triangular pyramid. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks. Base = 1 3 matchsticks Base = 2 9 matchsticks ...
Read More