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
Server Side Programming Articles
Page 955 of 2109
C Program for array rotation?
Array rotation is a fundamental operation where elements of an array are shifted to the left or right by a specified number of positions. In left rotation, elements move left and the leftmost elements wrap around to the end. Left Rotation by 3 positions Original: 1 2 3 4 ...
Read MoreC Program for Pancake sorting?
Pancake sorting is a variation of the sorting problem where the only allowed operation is to reverse elements of some prefix of the sequence. It's named after the problem of sorting a stack of pancakes using a spatula that can flip all pancakes above any insertion point. Syntax void pancakeSort(int arr[], int n); void flip(int arr[], int i); How Pancake Sort Works The algorithm works by repeatedly finding the maximum element in the unsorted portion and moving it to its correct position using at most two flip operations − Step 1: Find ...
Read MoreAlternate 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 More