C Articles

Page 73 of 96

Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 271 Views

Here we will see how we can find the absolute difference between the product of all prime numbers and all non-prime numbers of an array. To solve this problem, we have to check whether a number is prime or not. One possible way for primality testing is by checking a number is not divisible by any number between 2 to square root of that number. So this process will take O(√n) amount of time. Then get the product and try to find the absolute difference. Syntax int diffPrimeNonPrimeProd(int arr[], int n); bool isPrime(int n); Algorithm ...

Read More

Absolute difference between the first X and last X Digits of N?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 516 Views

Here we will see how to get the absolute difference between the first X and last X digits of a number N. To solve this problem, we first extract the last X digits using the modulus operator, then extract the first X digits by reducing the number's length. Finally, we calculate the absolute difference between these two values. Syntax int diffFirstLastDigits(int n, int x); Algorithm diffFirstLastDigits(N, X) begin p := 10^X last := N mod p len := length of the ...

Read More

Absolute difference between sum and product of roots of a quartic equation?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 373 Views

In this section we will see how to find the absolute difference between the sum of the roots and the product of the roots of a quartic equation. A quartic equation has the form ax4 + bx3 + cx2 + dx + e = 0. We can solve the equation and then calculate the sum and product of the roots using traditional methods, but that approach is time-consuming and inefficient. For a quartic equation, we have two important formulas − Sum of roots = -b/a Product of roots = e/a Therefore, we need to find ...

Read More

A sorting algorithm that slightly improves on selection sort?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 338 Views

Two-way selection sort is an improved version of selection sort that processes the array from both ends simultaneously. Instead of finding only the minimum element in each pass, it finds both the minimum and maximum elements and places them at their correct positions from the left and right ends respectively. Syntax void twoWaySelectionSort(int arr[], int n); Algorithm begin for i := 0, and j := n-1, increase i by 1, and decrease j by 1, until i >= j, do min := minimum element from index i ...

Read More

A Problem in Many Binary Search Implementations?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 185 Views

We know that the binary search algorithm is better than the linear search algorithm. This algorithm takes O(log n) amount of time to execute. Though most of the cases the implemented code has some problem. Let us consider one binary search algorithm function like below − Syntax int binarySearch(int array[], int start, int end, int key); Problem in Common Implementation Here is a common but problematic implementation of binary search − #include int binarySearch(int array[], int start, int end, int key){ if(start key) ...

Read More

A permutation where each element indicates either number of elements before or after it?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 177 Views

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 More

A backtracking approach to generate n bit Gray Codes ?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 563 Views

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 More

Generic keyword in C ?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 414 Views

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 More

C Program for Egg Dropping Puzzle - DP-11?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 417 Views

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 More

C Program for Basic Euclidean algorithms?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 388 Views

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 More
Showing 721–730 of 953 articles
« Prev 1 71 72 73 74 75 96 Next »
Advertisements