Found 7197 Articles for C++

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

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

776 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

514 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

129 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

791 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 two numbers with sum and product both same as N in C++ Program

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

242 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 total number of distinct years from a string in C++ Program

Aman Kumar
Updated on 07-Aug-2025 16:06:02

721 Views

In this article, we will write a C++ program to find the total number of distinct years from a string. Here, we have a string that contains the words and the dates. Our task is to find the number of distinct years mentioned. Let's see the following example scenario to understand the problem better: Scenario 1 Input: str = "The Berlin Wall fell on 09/11/1989. The first website was launched on 06/08/1991." Output: Total number of distinct years: 2 Explanation: Two distinct years are referenced: 1989, and 1991 Scenario 2 Input: str = "TutorialsPoint India was founded on ... Read More

Find time taken for signal to reach all positions in a string - C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:30:07

142 Views

In this tutorial, we are going to write a program that computes the time taken for a signal to reach all positions in a string. Let me explain it with an example.We will have a string that contains only s and p characters. s is a signal and p is a position in the string. A signal starts at s and travels in both left and right directions. We are assuming it takes one unit of time to travel to the next position in the string. Our task is to compute the time needed to convert all positions into signals.Let's ... Read More

Find three integers less than or equal to N such that their LCM is maximum - C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:27:54

495 Views

In this tutorial, we are going to write a program that is based on the concepts of LCM. As the title says, we have to find three numbers that are less than or equal to the given number whose LCM is maximum.Let's see an example.Before diving into the problem let's see what LCM is and write program for it.LCM is the least common multiple of a number. It is also known as Least common divisor. For two positive number a and b, the LCM is the smallest integer that is evenly divisible by both a and b.If the given integers ... Read More

Program to Find Out Median of an Integer Array in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:55:00

306 Views

Suppose we have to implement a class named MedianClass which contains the following methods −add(value) which adds a value to the data structure.median() finds the median of all the numbers currently present in the data structure.So, if we add 5, 3, 8 and find median, the output will be 5.0, then if we add 9 and find the median, the output will be 6.5.To solve this, we will follow these steps −Define priority queue left and rightDefine addNum method, this will take the number as input −if left is empty or num < top element of left, then, insert num ... Read More

Program to Find Out Integers Less than n Containing Multiple Similar Digits in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:29:16

209 Views

Suppose we have an integer n, we have to find the number of positive integers that are less than or equal to n, where the integer numbers at least have a digit occurring more than once.So, if the input is like n = 200, then the output will be 38To solve this, we will follow these steps −Define an array afor initialize x := n, when x is non−zero, update x := x / 10, do −insert x mod 10 at the end of areverse the array aret := nfor initialize w := 1, d := 1, when w < ... Read More

Advertisements