Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 329 of 377

Find sum of even index binomial coefficients in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 196 Views

Consider we have a number n, we have to find the sum of even indexed binomial coefficients like $$\left(\begin{array}{c}n\ 0\end{array}\right)+\left(\begin{array}{c}n\ 2\end{array}\right)+\left(\begin{array}{c}n\ 4\end{array}\right)+\left(\begin{array}{c}n\ 6\end{array}\right)+...\left(\begin{array}{c}4\ 0\end{array}\right)+\left(\begin{array}{c}4\ 2\end{array}\right)+\left(\begin{array}{c}4\ 4\end{array}\right)++=1+6+1=8$$So here we will find all the binomial coefficients, then only find the sum of even indexed values.Example Live Demo#include using namespace std; int evenIndexedTermSum(int n) {    int coeff[n + 1][n + 1];    for (int i = 0; i

Read More

Find length of period in decimal value of 1/n in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 621 Views

Suppose we have a number n. We have to find the length of period in decimal value of 1/n. So if the value of n is 7, then 1/7 = 0.142857142857… That part in bold letters are repeating. So here the length of period is 6.For a number n, there can be n distinct remainders in the output, but the period may not begin from the first remainder as some initial remainders are non-repeating. So we have to make sure that a remainder from period is picked, start from (n+1)th remainder, and start looking for the next occurrence. The distance ...

Read More

Find k-th bit in a binary string created by repeated invert and append operations in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 186 Views

Suppose we have a binary string s, initially this is say “0”. Now in each iteration invert it, and append it, thus after nth iteration, we will find the kth bit. Suppose the number of iteration is 4, and k = 7, so it will be −IterationValue (Initially 0)1012011030110100140110100110010110so 7th bit is 1.In each iteration, find complement, and append, thus after nth iteration, finds kth bitExample Live Demo#include using namespace std; string getComplement(string bin){    string temp = "";    for(int i= 0; i

Read More

Find index of closing bracket for a given opening bracket in an expression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 468 Views

Consider we have an expression with brackets. If the index of one starting bracket is given, we have to find the closing ending bracket of that. So if the expression is like: (25*6+(88-32+(50/10)+20)), and the index of opening bracket is 6, then closing bracket will be at position 23.Here we will use the stack data-structure to solve this problem. We will traverse the expression from given index, and start pushing the opening brackets, when closing bracket is found, then pop element from stack, when the stack is empty, then return the index.Example Live Demo#include #include using namespace std; void getEndingBracketIndex(string exp, ...

Read More

Find smallest number n such that n XOR n+1 equals to given k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 331 Views

Suppose we have a positive number k. We have to find the positive number n, such that XOR of n and n+1 is same as k. So if k = 7 (111), output will be 3. As 3 (011), and 3 + 1 = 4 (100), so 011 XOR 100 = 111 (7)There are two possible cases. Consider n is even. The last bit of n = 0. Then the last bit of n + 1 = 1. Rest of the bits are same. So XOR will be 1, when n is odd, last bit 1, and last bit of ...

Read More

Find set of m-elements with difference of any two elements is divisible by k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 269 Views

Suppose we have an array with N positive integers, and another variable K. We have to find the exactly m-elements, such that difference between any two elements is equal to k. So if the array is A = [4, 7, 10, 6, 9], and k = 3 and m = 3, then output will be “yes”. As we can find three elements like 4, 7, 10.To solve this, we have to keep track of the remainders, when an element is divided by k. Now create a multi-dimensional array rem[][] of size k, its index is showing the remainder, and elements ...

Read More

Find foot of perpendicular from a point in 2D plane to a Line in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 1K+ Views

Consider we have a point P in 2D plane and equation of a line, the task is to find the foot of the perpendicular from P to the line.The equation of the straight line is ax + by + c = 0. Equation of line passing through P and perpendicular to line. Equation of line passing through P and Q will be ay – bx + d = 0. Also P(x1, y1), and Q(x2, y2), so we put coordinate of P on the equation.ay 1−bx 1+d=0, so d=bx1−ay 1Also Q is the intersection of the given line and the line ...

Read More

Find missing number in another array which is shuffled copy in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 361 Views

Suppose, we have two arrays A and B, the array A has n elements. The second array B has all the elements of A, but they are shuffled and one element is removed. We have to find the missing elements. So if A = [4, 8, 1, 3, 7], and B = [7, 4, 3, 1], the output is 8.This can be solved using XOR trick. The combined occurrence of each element is twice, one in A, and other in B, except one element which only has a single occurrence in A. As we know that x XOR x = ...

Read More

Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 311 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as possible number of times. We have to find the repeating numbers without taking any extra space. If the value of n = 7, and list is like [5, 2, 3, 5, 1, 6, 2, 3, 4, 5]. The answer will be 5, 2, 3.To solve this, we have to follow these steps −for each element e in the list, do the following steps −sign := A[absolute value of e]if the sign is positive, then make it negativeOtherwise it is a repetition.Example Live ...

Read More

Find minimum value to assign all array elements so that array product becomes greater in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 250 Views

Suppose we have an array of n elements. Update all elements of the given array to some min value x, such that arr[i] = x. Such that product of all elements in the new array is strictly greater than the product of all elements of the initial array, where i

Read More
Showing 3281–3290 of 3,768 articles
« Prev 1 327 328 329 330 331 377 Next »
Advertisements