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
Programming Articles
Page 974 of 2547
Alternate vowel and consonant string in C/C++?
We are given a string with both vowels and consonants. Our task is to rearrange it so that vowels and consonants appear alternately, while keeping their original order within their groups. This rearrangement is only possible if the number of vowels and consonants is equal, or their difference is exactly one. If multiple valid arrangements are possible, we return the one that is lexicographically smaller. Syntax char* alternateVowelConsonant(char* str); bool isVowel(char ch); Example Scenarios Scenario 1 Input: "objective" Output: "obejctive" Explanation: Vowels = [o, e, i, e], Consonants = [b, j, ...
Read MoreC/C++ Program for Finding the Number Occurring Odd Number of Times?
In C programming, finding the number that occurs an odd number of times in an array is a common problem. Given an array where all elements appear an even number of times except one, we need to identify that unique element. Consider the array [1, 2, 1, 3, 3, 2, 2]. Here, the number 2 appears 3 times (odd), while others appear even times. Example Scenarios Input: arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7} Output: 7 The number 7 appears 3 times (odd frequency). Input: arr[] = {2, 3, 2, 1, ...
Read MoreC/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
This program finds the sum of a mathematical series where the n-th term is defined as Tn = n2 - (n-1)2. We need to calculate the sum Sn = T1 + T2 + T3 + ... + Tn modulo (109 + 7). Syntax result = ((n % mod) * (n % mod)) % mod; Mathematical Derivation First, let's simplify the term Tn − Tn = n2 - (n-1)2 Tn = n2 - (n2 - 2n + 1) Tn = n2 - n2 + 2n - 1 Tn = 2n - 1 ...
Read MoreAdd 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 MoreTypes of Polymorphisms - Ad-hoc, Inclusion, Parametric & Coercion
Polymorphism is a fundamental concept in programming that allows entities to take multiple forms. There are four main types of polymorphism − Ad-Hoc Polymorphism (Function Overloading) Inclusion Polymorphism (Subtyping) Parametric Polymorphism (Generics/Templates) Coercion Polymorphism (Type Casting) Note: This article explains polymorphism concepts using C syntax for illustration. Pure C doesn't support object-oriented polymorphism natively, but these concepts can be demonstrated through function pointers and other techniques. Ad-Hoc Polymorphism (Function Overloading) Ad-hoc polymorphism allows multiple functions with the same name to operate on different data types. In C, this is achieved through different ...
Read MoreA Puzzle using C Program
Here we will see one C puzzle question. Suppose we have two numbers 48 and 96. We have to add the first number after the second one. So final result will be like 9648. But we cannot use any logical, arithmetic, string related operations, also cannot use any pre-defined functions. So how can we do that? This is easy. We can do by using Token Pasting operator (##) in C. The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the macro ...
Read More3-Way QuickSort (Dutch National Flag)
The 3-Way QuickSort, also known as the Dutch National Flag algorithm, is an optimized version of the standard QuickSort algorithm. While traditional QuickSort partitions the array into two parts (less than and greater than pivot), 3-Way QuickSort creates three partitions: elements less than pivot, equal to pivot, and greater than pivot. Syntax void partition(int arr[], int left, int right, int *i, int *j); void quicksort3Way(int arr[], int left, int right); Algorithm The partition function divides the array into three sections − partition(arr, left, right, i, j): if right - left
Read More