C++ Articles

Page 564 of 597

Find column with maximum sum in a Matrix using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 540 Views

Suppose we have a matrix of size M x N. We have to find the column, that has a maximum sum. In this program we will not follow some tricky approach, we will traverse the array column-wise, then get the sum of each column, if the sum is the max, then print the sum and the column index.Example#include #define M 5 #define N 5 using namespace std; int colSum(int colIndex, int mat[M][N]){    int sum = 0;    for(int i = 0; i maxSum) {           maxSum = sum;           index = i;       }    }    cout

Read More

Find an equal point in a string of brackets using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 498 Views

Here we will see how to get the equal points in a string of brackets. The equal point is the index I, such that the number of opening brackets before it is equal to the number of the closing bracket after it. Suppose a bracket string is like "(()))(()()()))))", if we see closer, we can getSo the number of opening brackets from 0 to 9 is 5, and the number of the closing brackets from 9 to 14 is also 5, so this is the equal point.To solve this problem, we have to follow these few steps −Store the number ...

Read More

Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Oct-2019 183 Views

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always. This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row. Example #include using namespace std; void makeSymmetricMatrix(int n) {     int matrix[n][n];     for(int i = 0; i

Read More

Find a subset with greatest geometric mean in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Oct-2019 207 Views

Here we have an array A with some elements. Our task is to find the subset where the geometric mean is maximum. Suppose A = [1, 5, 7, 2, 0], then the subset with greatest geometric mean will be [5, 7].To solve this, we will follow one trick, we will not find the mean, as we know that the largest two elements will form the greatest geometric mean, so the largest two elements will be returned as subset.Example#include using namespace std; void largestGeoMeanSubset(int arr[], int n) {   if (n < 2) {     cout max) {       second_max = max; ...

Read More

Find a pair with maximum product in array of Integers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Oct-2019 241 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return ...

Read More

Binary Search in C++ program?

sudhir sharma
sudhir sharma
Updated on 24-Oct-2019 1K+ Views

binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the ...

Read More

An interesting time complexity question in C++

sudhir sharma
sudhir sharma
Updated on 24-Oct-2019 1K+ Views

Time complexity can be defined as the time required by the algorithm to run its average case.Let's see and calculate the time complexity of some of the basic functions.Methodvoid counter(int n){    for(int i = 0 ; i < n ; i++){       for(int j = 1 ; j

Read More

An Insertion Sort time complexity question in C++

sudhir sharma
sudhir sharma
Updated on 24-Oct-2019 1K+ Views

What is the time complexity of insertion sort?Time complexity is the amount of time taken by a set of codes or algorithms to process or run as a function of the amount of input.For insertion sort, the time complexity is of the order O(n) i.e. big O of n in best case scenario. And in the average or worst case scenario the complexity is of the order O(n2).What will be the time complexity of sorting when insertion sort algorithm is applied to n sized array of the following form: 6, 5, 8, 7, 10, 9 …… I, i-1The time complexity ...

Read More

Amortized analysis for increment in counter in C++

sudhir sharma
sudhir sharma
Updated on 24-Oct-2019 700 Views

Amortized analysis for a sequence of operations is used to determine the run time, the average time required by the sequence. In cannot be treated as an average-case analysis done on the algorithm as it does not always take the average case scenario. There are cases that occur as a worst-case scenario of analysis. So, amortized analysis can be treated as a worst-case analysis for multiple operations in a sequence. Here, the cost of doing each operations in different and for some its high. This problem is a general view using the binary counter.Let’s see the working and implementation in ...

Read More

Minimize the total number of teddies to be distributed in C++

Narendra Kumar
Narendra Kumar
Updated on 22-Oct-2019 197 Views

Problem statementGiven N number of students and an array which represent the mark obtained by students. School has dicided to give them teddy as a price. Hoever, school wants to save money, so they to minimize the total number of teddies to be distrubuted by imposing following constrain −All students must get atleast one teddyIf two students are sitting next to each other then student with the higher marks must get moreIf two students have same marks then they are allowed to get different number of teddiesExampleLet us suppose there are 3 students and marks obtained are represented in array ...

Read More
Showing 5631–5640 of 5,962 articles
« Prev 1 562 563 564 565 566 597 Next »
Advertisements