Find a Pair with the Given Difference in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:53:24

411 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the difference between x and y is same as given difference d. Suppose a list of elements are like A = [10, 15, 26, 30, 40, 70], and given difference is 30, then the pair will be (10, 40) and (30, 70)To solve this problem, we will assume that the array is sorted, then starting from left we will take two pointers to point elements, initially first one ‘i’ will point to the first element, ... Read More

Find Pair with Maximum Product in Array of Integers in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:50:20

204 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return ... Read More

Find Pair of Elements to Make Sum of Two Arrays Same in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:44:48

409 Views

Consider we have two arrays with different number of elements. We have to find a pair of elements (x, y), where x is present in the first array, and y is present at the second array. The pair will be chosen such that after swapping the elements between these two arrays, the sum of these two arrays will be same.Suppose first array A is holding [4, 1, 2, 2, 1, 1] and B is holding [3, 3, 6, 3], now the sum of A is 11 and sum of B is 15, we will take a pair like (1, 3), ... Read More

Find Number X Such That Sum of X and Its Digits Equals Given N in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:40:45

350 Views

Here we will see one problem, where we take a number n, we have to find another value say x, such that x + digit sum of x is same as the given number n. Suppose the value of n is 21. This program will return a number x = 15, as 15 + digit sum of 15, i.e. 15 + 1 + 5 = 21 = n.To solve this problem, we have to follow simple approach. We will iterate through 1 to n, in each iteration, we will see if the sum of the number and its digit sum ... Read More

Find a Fixed Point in an Array with Duplicates Allowed in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:37:06

189 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted. Here duplicate elements are allowed in the array.Here we will use binary search approach to solve this problem in O(log n) time. But we need some modification, if the normal binary search is used, then it may fail for duplicate elements. To check left, we ... Read More

Find Fixed Point Value Equal to Index in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:34:24

257 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted.Here we will use binary search approach to solve this problem in O(log n) time. At first we will check whether the middle element is fixed point or not, if yes, then return it, if not, then there will be two situations, if the index of ... Read More

HTML DOM MouseEvent screenX Property

AmitDiwan
Updated on 24-Oct-2019 12:33:14

108 Views

The HTML DOM MouseEvent screenX property returns the horizontal (x) coordinate of the mouse pointer relative to the users screen display if a mouse event was triggered. Use with screenY to get the vertical coordinate as well.Following is the syntax −Returning reference to the screenX objectMouseEventObject.screenXLet us see an example of MouseEvent screenX property −Example Live Demo MouseEvent screenX    * {       padding: 2px;       margin:5px;    }    form {       width:70%;       margin: 0 auto;       text-align: center;    }    #outer {     ... Read More

Find Distinct Pair (x, y) Such That x Divides y in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:31:50

144 Views

Here we will see one interesting problem, we will find a pair (x, y), where x and y are in range so l

Final Cell Position in the Matrix in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:29:04

206 Views

Suppose we have a set of commands as a string, the string will have four different letters for four directions. U for up, D for down, L for left and R for right. We also have initial cell position (x, y). Find the final cell position of the object in the matrix after following the given commands. We will assume that the final cell position is present in the matrix. Suppose the command string is like “DDLRULL”, initial position is (3, 4). The final position is (1, 5).The approach is simple, count the number of up, down, left and right ... Read More

MouseEvent.relatedTarget in HTML DOM

AmitDiwan
Updated on 24-Oct-2019 12:28:44

110 Views

The HTML DOM MouseEvent relatedTarget property returns the corresponding element that triggered the respective mouseover or mouseout event.Following is the syntax −Returning reference to the relatedTarget objectMouseEventObject.relatedTargetLet us see an example of MouseEvent relatedTarget property −Example Live Demo MouseEvent relatedTarget    * {       padding: 2px;       margin:5px;    }    form {       width:70%;       margin: 0 auto;       text-align: center;    }    #outer {       width:70%;       margin: 0 auto;       padding: 0;       text-align: center;   ... Read More

Advertisements