First N Natural Numbers Divided into Two Sets with Given Difference in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:07:42

102 Views

In this tutorial, we have to find whether the natural numbers from 1 to n is divided into two halves or not. It has to satisfy the following conditions.The absolute difference between the two series sum should be m.And the GCD of two sums should be 1 i.e.., co-primes.The sum of first n natural numbers is (n*(n+1))/2. We can find the sumOne and sumTwo as we have total sum and difference m. See the below equations.sumOne + sumTwo = (n*(n+1))/2 sumOne - sumTwo = mExampleCheck whether the absolute sum is equal to m or not. And then check for GCD. Live ... Read More

First Digit in Product of an Array of Numbers in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:06:13

168 Views

In this tutorial, we are going to learn how to find first digit of the product of an array.Let's see the steps to solve the problem.Initialize the array.Find the product of the elements in the array.Divide the result until it's less than 10.Print the single-digitExampleLet's see the code. Live Demo#include using namespace std; int productOfArrayDigits(int arr[], int n) {    int product = 1;    for (int i = 0; i < n; i++) {       product *= arr[i];    }    return product; } int firstDigitOfNumber(int n) {    while (n >= 10) {       n /= 10;    }    return n; } int main() {    int arr[] = { 1, 2, 3, 4, 5, 6 };    cout

First Digit in Factorial of a Number in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:04:26

151 Views

In this tutorial, we are going to write a program the finds the first digit of a factorial. Let's see an example.Input − 7Output − 5Let's see the steps to solve the problem.Initialize the numberFind the factorial of the number.Divide the number until it becomes a single digit.ExampleLet's see the code. Live Demo#include using namespace std; void findFirstDigitOfFactorial(int n) {    long long int fact = 1;    for (int i = 2; i = 10) {       fact = fact / 10;    }    cout

Finding Vertex, Focus, and Directrix of a Parabola in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:02:50

175 Views

In this tutorial, we are going to learn how to find the vertex, focus, and directrix of a parabola. We are given constants of the parabola equation x, y, and z.There are straightforward formulas to find the vertex, focus, and directrix. Let's them.Vertex − (-y/2x, 4xz-y^2/4x)Focus − (-y/2x, 4xz-y^2+1/4x)Directrix − z-(y^2+1)4xExampleLet's see the code. Live Demo#include using namespace std; void findParabolaProperties(float x, float y, float z) {    cout

Finding the Parity of a Number Efficiently in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:01:23

3K+ Views

In this tutorial, we are going to write a program that finds the parity of a number.We can find the parity of a number efficiently by performing the following operations using xor and right-shift operators.int b; b = n ^ (n >> 1); b = b ^ (b >> 2); b = b ^ (b >> 4); b = b ^ (b >> 8); b = b ^ (b >> 16);If the last bit of the result is 1 then it's an odd parity else even parity.ExampleLet's see the code. Live Demo#include using namespace std; void findParity(int n) {   ... Read More

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

379 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

101 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

123 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

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

Advertisements