Found 7347 Articles for C++

Find zeroes to be flipped so that number of consecutive 1’s is maximized in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:51:19

58 Views

In this tutorial, we are going to find the zeroes count that need to be flipped to get maximum number of consecutive 1's in the array.We are going to use the sliding window approach to solve the problem. Let's see the steps to solve the problem.Initialize the array and max zeroes to be flipped.Initialize window starting, ending indexes along with the length.Store the max sub array of consecutive 1's length and starting index.Iterate over the array until ending indexes crosses the array length.If the zeroes count is less than the max zeroes count then increment the ending index and zeroes ... Read More

Find winner of an election where votes are represented as candidate names in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:49:34

632 Views

In this tutorial, we are going to write a program that finds the election winner. We will have an array of votes that each candidate got in the election. Let's see an example.Input {"A", "B", "C", "B", "A", "C", "D", "D", "A", "B", "D", "B", "A", "C", "D"}Output AHere, A and B got the same number of votes. In that case, we have to select the winner based on the alphabetical order of their names.Let's see the steps to solve the problem.Initialize an array of string with dummy data.Initialize a map with string as key and int as value.Iterate over the votes ... Read More

Find ways an Integer can be expressed as sum of n-th power of unique natural numbers in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:47:42

415 Views

In this tutorial, we are going to write a program that find the number of ways a integer can be expressed as sum of given n-th power of unique numbers.We have two integers number and power. And we need to find in how many ways can we represent the given number as sum of n-th power of unique natural numbers. Let's see an example.Input − number = 50, power = 2Output − 3There is only possible way we can write 4 as sum of 2 powers.We will be using recursion to solve the problem. Let's see the steps to solve ... Read More

Find unique pairs such that each element is less than or equal to N in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:43:17

88 Views

In this tutorial, we are going to learn how to find the unique pairs that are less than the given number n.Let's see the steps to solve the problem.Initialize the number.Iterate from i = 1 to i < n.Iterate from j = i + 1 to j < n.Print the (i, j).ExampleLet's see the code. Live Demo#include using namespace std; void uniquePairs(int n) {    for (int i = 1; i < n; ++i) {       for (int j = i + 1; j < n; j++) {          cout

Find Union and Intersection of two unsorted arrays in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:42:05

587 Views

In this tutorial, we are going to learn how to write a program for union and intersection of two unsorted arrays. Let's see an example.Input arr_one = [1, 2, 3, 4, 5] arr_two = [3, 4, 5, 6, 7]Output union: 1 2 3 4 5 6 7 intersection: 3 4 5Let's see the steps to solve the problem.UnionInitialize the two arrays with random values.Create an empty array called union_result.Iterate over the first array and add every element to it.Iterate over the section array and add element if it is not present in union_result array.Print the union_result array.IntersectionInitialize the two arrays with random ... Read More

Find uncommon characters of the two strings in C++ Program

Hafeezul Kareem
Updated on 29-Dec-2020 10:40:04

412 Views

In this tutorial, we are going to learn how to find distinct characters from the given two strings. Let's see an example.Input string_one = "tutorialspoint" string_two = "tutorialsworld"Outputd n p wWe are going to use hashing to solve the problem. It's more efficient than writing two nested loopLet's see the steps to solve the program.Initialize the two strings with some random values.Initialize a map as map chars.Iterate over the first string and insert each character into map with value 1.Now, iterate over the second string.Check whether the character is already present or not.If present, assign 0 to it.If not present insert ... Read More

Find two numbers with sum and product both same as N in C++ Program

Hafeezul Kareem
Updated on 29-Dec-2020 10:38:11

181 Views

In this tutorial, we are going to write a program to find two numbers where x + y = n and x * y = n. Sometimes it's not possible to find those types of numbers. We'll print None if there are no such numbers. Let's get started.The given numbers are the sum and products of a quadratic equation. So the number doesn't exist if n2 - 4*n

Find two distinct prime numbers with given product in C++ Program

Hafeezul Kareem
Updated on 29-Dec-2020 10:36:16

282 Views

In this tutorial, we are going to write a program the find two distinct prime numbers with the given product. Let's see some examples.Input − 21Output − 3 7Here, we need to have all the prime numbers that are less than the given product. Once we have those prime numbers, then we can easily find the pair. Follow the below steps to solve the problem.Initialize a product and an boolean array to store whether a numbers in the range are prime or not.Find all the prime numbers that are less than given product and store them in a array.Iterate till ... Read More

Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program

Hafeezul Kareem
Updated on 29-Dec-2020 10:34:15

326 Views

In this tutorial, we are going to write a program that finds trace of matrix formed by row and column major matrices.Let's see how to form a row and column major matrices when order of the matrix is given.Order − 3 x 3Row Major Matrix −123456789Column Major Matrix −147258369We have row and column major matrices. Now, we have to add both the matrices. And the trace of the resultant matrix is the result that we are looking for.Let's see write the code to solve the problem. We have to complete the following 4 steps to finish the solution for the ... Read More

Find total number of distinct years from a string in C++ Program

Hafeezul Kareem
Updated on 29-Dec-2020 10:32:04

525 Views

In this tutorial, we are going to write a program that finds distinct years in the given string. Let's see some examples. We are assuming the date format is DD/MM/YYYY.Input − Sample example with dates 01/11/2020, 02/12/2020, and 03/10/2019.Output − 2We have two distinct years in the given text 2020 and 2019.We are going to use regex to extract all dates from the given string. If you are not familiar with regex in C++, go through this tutorial.Let's jump into solving the problem.Initialize the text.Write the regex to extract dates from the text.Initialize an empty unordered set.Iterate over the dates ... Read More

Advertisements