Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 345 of 377

Program to find out the k-th smallest difference between all element pairs in an array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 434 Views

Suppose we are given a list containing several integer numbers. We have to find out the difference between each pair of values in the array and find out the k-th smallest difference number. The index starts at 0 and the value k is given to us as input.So, if the input is like numbers = {2, 6, 4, 8}, k = 2, then the output will be 2.The differences between the pairs are −(2, 6) = 4(2, 4) = 2(2, 8) = 6(6, 4) = 2(6, 8) = 2(4, 8) = 4If we sort the values, it becomes 2, 2, ...

Read More

Program to decode a given message in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 1K+ Views

Suppose we are given an encoded message that is a string of integer numbers. Now, these integer numbers can be mapped to a specific letter in the alphabet. a is mapped to 1, b is mapped to 2, c is mapped to 3, and so on. There is also a character '*' that can be in the message and that can be mapped to any of the numbers from 1 to 9. So given a message 'input', we have to find out how many ways it can be decoded.So, if the input is like input = "18", then the output ...

Read More

Program to find out the cost to merge an array of integers into a single value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 197 Views

Suppose we are given an array arr that contains n positive integer numbers. We are also given an integer number j. The task we have to perform is to merge j numbers into a single number by adding them. The cost of merging is equal to the addition of the j numbers we have selected. We have to find out the minimum possible cost for this merging operation.So, if the input is like arr = [2, 5, 6, 2, 3, 1, 3], j = 4, then the output will be 31.Cost to merge 2, 3, 1, 3 is equal to ...

Read More

Program to find out the number of non-zero submatrices in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 16-Oct-2021 518 Views

Suppose we are given a matrix that contains only two values; 1s and 0s. We have to find out the number of submatrices in the given matrix that contains all 1s. We print the value as output.So, if the input is like0010010001011101then the output will be 12.To solve this, we will follow these steps −n := size of matrixm := size of matrix[0]Define an array add of size: n+1 x m+1.for initialize i := 0, when i < n, update (increase i by 1), do −for initialize j := 0, when j < m, update (increase j by 1), do ...

Read More

Program to find minimum cost to connect each Cartesian coordinates in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 16-Oct-2021 313 Views

Suppose we have a list of 2D Cartesian coordinate points (x, y). We can connect (x0, y0) and (x1, y1), whose cost is |x0 - x1| + |y0 - y1|. If we are allowed to connect any number of points, we have to find the minimum cost necessary such that every point is connected by a path.So, if the input is like points = [[0, 0], [0, 2], [0, -2], [2, 0], [-2, 0], [2, 3], [2, -3]], then the output will be 14 because, from (0, 0) to (0, 2), (0, -2), (2, 0), (-2, 0), costs are 2, ...

Read More

C++ program to demonstrate function of macros

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 549 Views

Suppose we are given a integer array that contains several integer values. We have to find out the difference between the smallest value and the largest value in the array. To solve this problem, we have to use macros. The inputs are taken from stdin, and the result is printed back to stdout.So, if the input is like array = {120, 589, 324, 221, 234}, then the output will be The answer is : 469The difference between the largest value 589 and the smallest value 120 is 469.To solve this, we will follow these steps −mini := infinitymaxi := negative ...

Read More

C++ Program to add few large numbers

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 406 Views

Suppose we have an array nums of some large numbers. The large numbers are in range (-2^31 to 2^31 - 1). We have to find the sum of these numbers.So, if the input is like nums = [5000000003, 3000000005, 8000000007, 2000000009, 7000000011], then the output will be 25000000035.To solve this, we will follow these steps −x := 0for initialize i := 0, when i < size of nums, update (increase i by 1), do −x := x + nums[i]return xExampleLet us see the following implementation to get better understanding#include #include using namespace std; long long int solve(vector nums){    long long int x = 0;    for(int i=0; i

Read More

C++ program to demonstrate exception handling

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 444 Views

Suppose there is a function that calculates some serious complex mathematical operations. But during the operations, some exceptions can occur. We have to handle the different types of exceptions that can occur and perform the following.If the computer is unable to allocate the memory for computation, we have to print 'Memory Low!'If any other C++-related exception occurs, we have to print 'Exception:' then the exception.If something else happens, we print 'Unhandled exception'.We are given an array that contains a pair of values, and we pass it to the function. If any exception occurs, we handle it, or otherwise, we print ...

Read More

C++ program to search specific values in an array

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 4K+ Views

Suppose we are given an array 'arr' that contains n number of sorted integer values. We are also given an array 'query' of size q, and we have to tell either the values in ‘query’ are present in the given array 'arr' or not. If a value in query is present in arr, we print "Present" along with the position where the value is situated in. Otherwise, we print "Not present" and print the position in arr, where the minimum value greater than the value in query is located. We have to remember that the arrays are 1-indexed.So, if the ...

Read More

C++ program to print values in a specified format

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 1K+ Views

Suppose we are given three double values. We have to format them and print them in the following format.We have to print the integer part of the first value in hexadecimal format with lowercase letters.We have to print the second value up to two decimal places, putting a sign before it to show whether it is positive or negative. The second value to be printed has to be rightjustified and 15 characters long, padded with underscores in the left unused positions.We have to print the third value up to nine decimal places in a scientific notation.So, if the input is ...

Read More
Showing 3441–3450 of 3,768 articles
« Prev 1 343 344 345 346 347 377 Next »
Advertisements