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
C++ Articles
Page 7 of 597
Add n binary strings?
In C programming, adding n binary strings involves performing binary addition on multiple binary numbers represented as strings. We need to add all binary strings together to produce a single binary result. The approach uses binary addition logic with carry propagation, adding all strings one by one to get the final result. Syntax char* addBinaryStrings(char* binary1, char* binary2); char* addNBinaryStrings(char** binaryArray, int n); Example: Adding Multiple Binary Strings This example demonstrates adding three binary strings using a helper function that adds two binary strings at a time − #include #include ...
Read MoreArray Manipulation and Sum using C/C++
In this problem, you are given an integer array arr of size n and an integer S. Your task is to find an element k in the array such that if all the elements greater than k in the array are replaced with k, then the sum of all the elements of the resultant array becomes equal to S. If such an element exists, print it; otherwise, print -1. Syntax int getElement(int arr[], int n, int S); Algorithm To solve this problem efficiently, we follow these steps − Sort the array in ...
Read MoreAddition and Subtraction of Matrix using pthreads in C/C++
Matrix addition and subtraction using pthreads allows us to perform operations on large matrices efficiently by utilizing multiple threads. Each thread handles a portion of the matrix, enabling parallel computation and improved performance. To compile and run pthread programs, you need to link with the pthread library using: gcc -pthread program.c -o program Syntax pthread_create(&thread_id, NULL, function_name, (void*)argument); pthread_join(thread_id, NULL); Example: Matrix Operations with Pthreads This example demonstrates matrix addition and subtraction using multiple threads. Each thread processes one row of the matrices − #include #include #include ...
Read MoreC/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
Here we will see how to get the sum of the series with n-th term as n2 − (n−1)2. The recurrence relation is like below − Tn = n2 − (n−1)2 So the series is − S = T₁ + T₂ + T₃ + ... + Tₙ S = (1² - 0²) + (2² - 1²) + (3² - 2²) + ... + (n² - (n-1)²) S = 1 + 3 + 5 + ... + (2n-1) = n² We need to find S mod (109 + 7), ...
Read MoreA C/C++ Pointer Puzzle?
This C programming puzzle demonstrates how pointer arithmetic works with different types of pointers and multi-dimensional arrays. Understanding pointer sizes and type differences is crucial for mastering pointer arithmetic in C. Syntax sizeof(pointer_variable) (char*)(pointer + 1) - (char*)pointer Example: Pointer Arithmetic Puzzle Let's analyze this step-by-step with a complete C program that demonstrates pointer arithmetic with various pointer types − #include int main() { int a[4][5][6]; int x = 0; int* a1 = &x; ...
Read MoreC/C++ Program for the n-th Fibonacci number?
The Fibonacci sequence is a series where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. In this problem, we will find the nth number in the Fibonacci series. For this we will calculate all the numbers and print the n terms. Syntax // Basic approach using loop for (i = 1; i
Read MoreC/C++ Program to Count number of binary strings without consecutive 1’s?
In this article, we will learn how to count all distinct binary strings of length n such that no two 1's appear consecutively. We'll explore this problem using both recursive and dynamic programming approaches in C. What is a Binary String? A binary string is a sequence of characters that contains only '0' and '1'. It represents information in base-2 format. For example, "0101" is a binary string of length 4. We are given a positive integer n, and our task is to count all possible distinct binary strings of length n that do not contain consecutive ...
Read MoreC/C++ Program for the Odd-Even Sort (Brick Sort)?
The odd-even sort, also known as brick sort, is a comparison-based sorting algorithm that divides the sorting process into two alternating phases: odd phase and even phase. This technique is similar to bubble sort but operates on different index pairs in each phase, making it suitable for parallel processing. The odd phase compares and swaps elements at odd indices (1, 3, 5...) with their adjacent elements, while the even phase works on even indices (0, 2, 4...) with their adjacent elements. Syntax void oddEvenSort(int arr[], int n); How It Works The algorithm alternates ...
Read MoreC/C++ Program for Number of solutions to Modular Equations?
We have a given number of coins and we need to arrange them to form a pyramid of maximum height. The coins are arranged such that the first row contains 1 coin, the second row contains 2 coins, the third row contains 3 coins, and so on. 1 2 3 4 5 ...
Read MoreC/C++ Program for nth Catalan Number?
Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects. The nth Catalan number can be calculated using the recursive formula or dynamic programming approaches. Syntax C(n) = (2n)! / ((n+1)! * n!) C(n) = C(0)*C(n-1) + C(1)*C(n-2) + ... + C(n-1)*C(0) Mathematical Properties Catalan numbers have several interpretations − Cn is the number of Dyck words of length 2n (strings with n X's and n Y's where no prefix has more Y's than X's) Cn counts valid parentheses combinations with n pairs Cn ...
Read More