Found 7347 Articles for C++

n-th number whose sum of digits is ten in C++

Hafeezul Kareem
Updated on 22-Oct-2021 06:08:19

269 Views

The numbers whose digits sum is equal to 10 are19, 28, 37, 46, 55, 64, 73, 82, 91, etc.., If you observe the series, each number is incremented by 9. There are numbers in the above sequence whose digits sum does not equal 10 while incrementing by 9. But, you will get all the numbers whose digits sum is equal to 10.So, we can have a loop that increments by 9 and checks for digits sum and finds the n-th number. Let's see some examplesInputs 3 7Outputs 37 73AlgorithmInitialise the number nInitialise a counter to 0.Write a loop that iterates ... Read More

N-th multiple in sorted list of multiples of two numbers in C++

Hafeezul Kareem
Updated on 22-Oct-2021 05:54:52

132 Views

You are given three numbers. You need to find the n-th multiple from the multiples of the first two numbers. Let's see an example to understand it more clearly.Input x = 2 y = 3 n = 7Output 10The first n ****multiples of 2 are 2 4 6 8 10 12 14The first n ****multiples of 3 ****are 3 6 9 12 15 18 21If combine both multiples and sort them we get 2 3 4 6 8 9 10 12 14 15 18 21 and the nth number from the list is 10.AlgorithmInitialise a vector to store all the ... Read More

N digit numbers divisible by 5 formed from the M digits in C++

Hafeezul Kareem
Updated on 21-Oct-2021 12:57:36

129 Views

We have given a number N along with an array of M digits. Our job is to find the number of n digit numbers formed from the given M digits that are divisible by 5.Let's see some examples to understand the problem inputs and outputs.In − N = 2 M = 3 arr = {5, 6, 3}Out − 2There are 2 N digit numbers 35 and 65 possible that are divisible by 5. Let's see another example.Input − N = 1 M = 7 arr = {2, 3, 4, 5, 6, 7, 8}Output − 1There is only 1 number with ... Read More

Latin Square in C++

Hafeezul Kareem
Updated on 21-Oct-2021 09:12:56

357 Views

The Latin square is a matrix that has a special pattern. Let's see different examples to examine the pattern.1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1 The Latin square that you get will be of different size as you notice in the above examples. But, if you carefully observe the above matrices' pattern, you will find out that the last number of the previous row comes as the first element of the next row.That's the pattern ... Read More

How Region of Interest (ROI) works in OpenCV using C++?

Ginni
Updated on 03-May-2021 10:29:30

3K+ Views

To separate a particular portion from the image, we have to locate the area first. Then we have to copy that area from the main image to another matrix. This is how the ROI in OpenCV works.In this example, two matrices have been declared at the beginning. After that, an image named 'image_name.jpg' has been loaded into the 'image1' matrix. The next line 'image2=image1 (Rect(100, 100, 120, 120));' requires special attention. This line is cropping out the defined region of the image and storing it in the 'image2' matrix.The figure shows what we have done here with the 'Rect(100, 100, ... Read More

How to read the pixel value from the multichannel image in OpenCV using C++?

Ginni
Updated on 03-May-2021 10:27:28

639 Views

We have declared three variables named-'blue_Channel', 'green_channel' and 'red_channel'. The goals of these variables is to save the pixel values. We have used these variables inside the 'for loops'. Then we declared a matrix named 'color_Image_Matrix'.The syntax of this method is:blue_Channel = color_image_Matrix.at(i, j)[0];We used a BGR image. It has three channels. These channels maintain specific sequence, color_image_Matrix.at (i, j) represents the pixel values located at (i, i) and [0] represents the first channel. For example, if we write the line as follows:blue_Channel=color_image_Matrix.at (30, 35) [0];It means the variable 'blue_Channel' will have the first channel's pixel value located at(30, 35). ... Read More

Largest sum subarray with at-least k numbers in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:06:53

291 Views

Let's see the steps to complete the program.Initialise the array.Initialise max_sum array of size n.Find the max sum for every index and store it in max_sum array.Compute the sum of all the elements and store it in a variable sum.Write a loop that iterates from i = k to n.Add a[i] - a[i - k] to the sum.Update the result with max of result, sum.Update the result with max of result, sum + max_sum[i - k].ExampleLet's see the code. Live Demo#include using namespace std; int getMaxSum(int a[], int n, int k) {    int maxSum[n];    maxSum[0] = a[0];    int ... Read More

Largest subarray with equal number of 0s and 1s in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:14:28

133 Views

Let's see the steps to complete the program.Initialise the array.Make all zeroes in the array to -1.Have a map an empty map to store the previous indexes.Initialise sum to 0, max length to 0 and ending index to -1.Write a loop that iterates till n.Add current element to sum.If the sum is equal to 0.Update the max length with i + 1.And ending index to i.If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.Update the max length and ending index.Else add the sum to the previous indexes map.Print the starting index ... Read More

Largest subarray having sum greater than k in C++

Hafeezul Kareem
Updated on 09-Apr-2021 14:00:21

278 Views

In this tutorial, we are going to write a program that finds the largest subarray have sum greater than k.Let's see the steps to solve the problem.Initialise the array.Iterate over the array and store sum at each index in a vector along with the index.Sort the above sums based on sum and index.Initialise an array to store the indexes.Write a loop that iterates till n.Update the values with min index of above indexes array and previous sums array index.Initialise sum to 0.Write a loop that iterates till n.Add current element to sum.If the sum is greater than k.The maximum subarray ... Read More

Largest smaller number possible using only one swap operation in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:58:06

119 Views

In this tutorial, we are going to write a program that finds the largest number with a single swap that is less than the given number n.Let's see the steps to solve the problem.Initialise the number n.Iterate from the end of the string and find the index of the digit which is greater than its next digit. Store it in a variable.Break the loop as soon as u find it.Iterate over the number from the end of the string to the above index.Find the index of the digit which is less the above indexed digit and is greater among all ... Read More

Advertisements