Finding Sum of Digits Until Single Digit in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:59:58

9K+ Views

In this tutorial, we are going to write a program that sums digits of the given number until it becomes a single digit. Let's see an example.Input −4543Output −7Let's see the steps to solve the problem.Initialize a number.Initialize the sum to 0.Iterate until the sum is less than 9.Add each digit of the number to the sum using modulo operatorPrint the sumExampleLet's see the code. Live Demo#include using namespace std; void findTheSingleDigit(int n) {    int sum = 0;    while(n > 0 || sum > 9) {       if(n == 0) {          n = sum;          sum = 0;       }       sum += n % 10;       n /= 10;    }    cout

Finding K Such That Its Modulus With Each Array Element Is Same in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:58:00

400 Views

In this tutorial, we are going to write a program that finds a number such that its modulus with each array element is same. Let's see an example.Input − arr = {10, 4, 2}Output − 1 2If there are two numbers x, y and x > y, assume that x - y = d.Then the x = y + d.Let's say we have a number k such that x%k = y%k. Apply modulo k for the above equation and find the value d.x%k = (y+d)%k y%k = y%k +d%k d%k = 0From the above calculation, if the number k is ... Read More

Find X to Convert Fraction A/B to C/D in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:55:18

128 Views

In this tutorial, we are going to write a program that calculates ∆ X value that satisfies the given equation. The equation is (a + ∆ X)/(b + ∆ X) = c/d.Here, we need a little bit of math to solve the equation. And it's straightforward. Cross multiply and take ∆X to one side.You will get the value of ∆X as (b*c-a*d)/(d-c).We are given a, b, c, and d values. Finding \Delta XΔX value is straightforward.ExampleLet's see the code. Live Demo#include using namespace std; int findTheXValue(int a, int b, int c, int d) {    return (b * c - ... Read More

Maximize Consecutive 1s by Flipping Zeroes in C++

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

139 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 Election in C++

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

824 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 as Sum of N-th Power of Unique Natural Numbers in C++

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

540 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 Where Each Element is Less Than or Equal to N in C++

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

151 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

828 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 Two Strings in C++

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

609 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++

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

269 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

Advertisements