Found 7197 Articles for C++

Minimum operation to make all elements equal in array in C++

Narendra Kumar
Updated on 23-Dec-2019 05:56:20

2K+ Views

Problem statementGiven an array with n positive integers. We need to find the minimum number of operation to make all elements equal. We can perform addition, multiplication, subtraction or division with any element on an array element.ExampleIf input array is = {1, 2, 3, 4} then we require minimum 3 operations to make all elements equal. For example, we can make elements 4 by doing 3 additions.Algorithm1. Select element with maximum frequency. Let us call it ‘x’ 2. Now we have to perform n-x operations as there are x element with same valueExample Live Demo#include using namespace std; int getMinOperations(int *arr, ... Read More

Stein’s Algorithm for finding GCD in C++

Ajay yadav
Updated on 23-Dec-2019 06:01:17

417 Views

Stein's Algorithm used for discovering GCD of numbers as it calculates the best regular divisor of two non-negative whole numbers. It replaces division with math movements, examinations, and subtraction. In the event that both an and b are 0, gcd is zero gcd(0, 0) = 0. The algorithm for GCD(a, b) as follows;AlgorithmSTART    Step-1: check If both a and b are 0, gcd is zero gcd(0, 0) = 0.    Step-2: then gcd(a, 0) = a and gcd(0, b) = b because everything divides 0.    Step-3: check If a and b are both even, gcd(a, b) = 2*gcd(a/2, ... Read More

Minimum operations of given type to make all elements of a matrix equal in C++

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

339 Views

Problem statementGiven an integer K and a matrix of M x N, the task is to find the minimum number of operations required to make all the elements of the matrix equal. In a single operation, K can be added to or subtracted from any element of the matrix.ExampleIf input matrix is: {    {2, 4},    {20, 40} } and K = 2 then total 27 operations required as follows; Matrix[0][0] = 2 + (K * 9) = 20 = 9 operations Matrix[0][1] = 4 + (k * 8) = 20 = 8 operations Matrix[1][0] = 20 + (k ... Read More

Stringstream in C++ programming

Ajay yadav
Updated on 23-Dec-2019 05:36:07

941 Views

This sample draft calculates the total number of words in a given string as well as counts the total occurrence of a particular word using stringstream in the C++ programming code. The stringstream class partners a string object with a stream enabling you to peruse from the string as though it were a stream. This code shall achieve two feats, first, it will count the total number of words then calculates the frequencies of individual words subsequently in a string using the map iterator essential methods as follows;Example Live Demo#include using namespace std; int totalWords(string str){    stringstream s(str);   ... Read More

Minimum operations required to make all the array elements equal in C++

Narendra Kumar
Updated on 23-Dec-2019 05:32:38

2K+ Views

Problem statementGiven an array with n positive integers. We need to find the minimum number of operation to make all elements equal. We can perform addition, multiplication, subtraction or division with any element on an array element.ExampleIf input array is = {1, 2, 3, 4} then we require minimum 3 operations to make all elements equal. For example, we can make elements 4 by doing 3 additions.Algorithm1. Select element with maximum frequency. Let us call it ‘x’ 2. Now we have to perform n-x operations as there are x element with same valueExample Live Demo#include using namespace std; int ... Read More

Minimum operations required to remove an array in C++

Narendra Kumar
Updated on 23-Dec-2019 05:28:24

225 Views

DescriptionGiven an array of N integers where N is an even number. There are two kinds of operations allowed on the array.Increase the value of any element of an array by 1.If two adjacent elements in the array are consecutive prime number, delete both the element.The task is to find the minimum number of operations required to remove all the element of the array.ExampleIf array is {10, 13} then minimum 2 operations are requiredIncrement 1st element of an array by 1. So new array becomes {11, 13}Delete 1st and 2nd element as both are consecutive prime numbersAlgorithm1. To remove numbers, ... Read More

C++ Program for sorting variables of any data type

Sunidhi Bansal
Updated on 20-Dec-2019 14:01:57

1K+ Views

We are given with the values of different datatypes like it can be of type integer, float, string, bool, etc. and the task is to sort the variables of any data type using one common method or function and display the result.In C++, we can use std::sort to sort any type of array which is available in C++ standard template library(STL). By default sort function sorts the array element in ascending order. Sort() function takes three arguments −Start element in the array list i.e. from where you want to start your sortEnd element in the array list i.e. till where ... Read More

Product of non-repeating (distinct) elements in an Array in C++

Sunidhi Bansal
Updated on 20-Dec-2019 13:24:19

216 Views

We are given with an array of repeating or duplicate elements and the task is to find the product of all those elements which are non-repeating or distinct in the given array and display the results.ExampleInput-: arr[] = {2, 1, 1, 2, 3, 4, 5, 5 } Output-: 120 Explanation-: Since 1, 2 and 5 are repeating more than once so we will take them into consideration for their first occurrence. So result will be 1 * 2 * 3 * 4 * 5 = 120 Input-: arr[] = {1, 10, 9, 4, 2, 10, 10, 45, 4 } ... Read More

C++ Program for Bisection Method

Sunidhi Bansal
Updated on 20-Dec-2019 13:04:12

17K+ Views

Given with the function f(x) with the numbers a and b where, f(a) * f(b) > 0 and the function f(x) should lie between a and b i.e. f(x) = [a, b]. The task is to find the value of root that lies between interval a and b in function f(x) using bisection method.What is bisection method?Bisection method is used to find the value of a root in the function f(x) within the given limits defined by ‘a’ and ‘b’. The root of the function can be defined as the value a such that f(a) = 0.ExampleQuadratic equation F(x) = ... Read More

C++ Program for Priority Scheduling

Sunidhi Bansal
Updated on 20-Dec-2019 12:43:48

12K+ Views

We are given with the n number of processes i.e. P1, P2, P3, ......., Pn with their corresponding burst times and priorities associated with each process . The task is to find the average waiting time ,average turnaround time and the sequence of process execution using priority CPU scheduling algorithm.What is Waiting Time and Turnaround Time?Turnaround Time is the time interval between the submission of a process and its completion.Turnaround Time = completion of a process – submission of a processWaiting Time is the difference between turnaround time and burst timeWaiting Time = turnaround time – burst timeWhat is Priority ... Read More

Advertisements