Found 26504 Articles for Server Side Programming

Minimum removals from array to make max – min <= K in C++

Narendra Kumar
Updated on 23-Dec-2019 06:32:46

373 Views

Problem statementGiven N integers and K, find the minimum number of elements that should be removed such that Amax - Amin

Printing Pyramid in C++

Ajay yadav
Updated on 23-Dec-2019 06:28:19

436 Views

This article yields a “pyramid-like structure” as an output using the C++ programming code. In which the pyramid height and space are being determined by traversing double for loop constructs as following;Example Live Demo#include using namespace std; int main() {    int space, rows=6;    for(int i = 1, k = 0; i

Minimum removal to make palindrome permutation in C++

Narendra Kumar
Updated on 23-Dec-2019 06:27:04

246 Views

Problem statementGiven a string S, we have to find minimum characters that we can remove to make any permutation of the string S a palindromeExampleIf str = “abcdba” then we remove to 1 character i.e. either ‘c’ or ‘d’.Algorithms1. There can be two types of a palindrome, even length, and odd length palindromes 2. We can deduce the fact that an even length palindrome must have every character occurring even number of times 3.An odd palindrome must have every character occurring even number of times except one character occurring odd number of time 4. Check the frequency of every character ... Read More

Reverse a String (Iterative) C++

Ajay yadav
Updated on 23-Dec-2019 06:24:14

386 Views

There are many ways defined to reverse a string in the C++ code including stack, In-place, and iteration. In this sample, a simple string will be reversed iteratively with the following algorithm;AlgorithmSTART    Step-1: Input the string    Step-2: Get the length of the string using length() method    Step-3: Swap the last character to first using for loop    Step-4: Print ENDIncompatibility of the above calculation, the accompanying code in c++ language tried as following;Example Live Demo#include using namespace std; void strReverse(string& str){    int n = str.length();    // Swap character starting from two    cout

Minimum number of stops from given path in C++

Narendra Kumar
Updated on 23-Dec-2019 06:23:05

171 Views

Problem statementThere are many points in two-dimensional space which need to be visited in a specific sequence.Path from one point to other is always chosen as shortest path and path segments are always aligned with grid lines.We are given the path which is chosen for visiting the points. We need to tell the minimum number of points that must be needed to generate given paths.Algorithm1. We can solve this problem by observing the pattern of movement when visiting the stop 2. If we want to take the shortest path from one point to another point, then we will move in ... Read More

C++ Program for First Fit algorithm in Memory Management

Sunidhi Bansal
Updated on 23-Dec-2019 06:26:47

7K+ Views

Given with the n number of processes and m size of memory blocks and the task is to find the best fit memory block for the corresponding process using the first fit memory management algorithm.What is First Fit Memory Management Algorithm?There is multiple memory partitioning algorithms available which are used by the operating system to allocate the memory blocks to the processes like −First Fit AlgorithmNext Fit AlgorithmBest Fit AlgorithmWorst Fit AlgorithmQuick Fit AlgorithmFirst Fit Algorithm is the simplest technique of allocating the memory block to the processes amongst all. In this algorithm, the pointer keeps track of all the ... Read More

Minimum number of swaps required to sort an array in C++

Narendra Kumar
Updated on 23-Dec-2019 06:19:04

615 Views

Problem statementGiven an array of N distinct elements, find the minimum number of swaps required to sort the arrayExampleIf array is {4, 2, 1, 3} then 2 swaps are requiredSwap arr[0] with arr[2]Swap arr[2] with arr[3}Algorithm1. Create a vector of pair in C++ with first element as array alues and second element as array indices. 2. Sort the vector of pair according to the first element of the pairs. 3. Traverse the vector and check if the index mapped with the value is correct or not, if not then keep swapping until the element is placed correctly and keep counting ... Read More

Reverse a string using the pointer in C++

Ajay yadav
Updated on 23-Dec-2019 06:14:43

3K+ Views

This article reverse a string using the pointer in C++ coding, First, it calculates the length of a pointer to string then run a for loop in decrement order to display the reverse string as follows;Example Live Demo#include #include using namespace std; int main(){    char *str="ajaykumar";    cout

Minimum number using set bits of a given number in C++

Narendra Kumar
Updated on 23-Dec-2019 06:12:59

218 Views

Problem statementGiven an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number.ExampleIf input = 10 then answer would be 3Binary representation of 10 is 1010 and minimum number with 2 set bits is 0011 i.e. 3Algorithm1. Count the number of set bits. 2. (Number of set bits) ^ 2 – 1 represents the minimized number)Example Live Demo#include using namespace std; int getSetBits(int n) {    int cnt = 0;    while (n) {       ++cnt;       n = n & (n - 1);    }    return cnt; } int getMinNumber(int n){    int bits = getSetBits(n);    return pow(2, bits) - 1; } int main() {    int n = 10;    cout

C++ Program for dot product and cross product of two vectors

Sunidhi Bansal
Updated on 12-Aug-2021 14:28:43

7K+ Views

We are given two vectors let’s say vector A and vector B containing x, y, and directions, and the task is to find the cross product and dot product of the two given vector arrays.What is a vector?In mathematics, a quantity that has a magnitude and a direction is known as a vector whereas a quantity that has only one value as magnitude is known as a scalar. The point from where the vector starts is known as the initial point and the point where the vector ends is known as the terminal point. The distance between the initial point ... Read More

Advertisements