Found 7197 Articles for C++

Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++

Arnab Chakraborty
Updated on 25-Jul-2020 10:51:58

307 Views

ConceptWith respect of a given tree with m nodes and a number associated with every node, we canbreak any tree edge which will result in the formation of 2 new trees. Here, we have to count the number of edges in this way so that the Bitwise OR of the nodes present in the two trees constructed after breaking that edge are equal. It should be noted that the value ofevery node is ≤ 10^6.Inputvalues[]={1, 3, 1, 3}      1    / | \   2 3 4Output2Here, the edge between 1 and 2 can be broken, the Bitwise ... Read More

Find nth term of a given recurrence relation in C++

Arnab Chakraborty
Updated on 25-Jul-2020 10:45:38

246 Views

ConceptAssume bn be a sequence of numbers, which is denoted by the recurrence relation b1=1 and bn+1/bn=2n. Our task is to determine the value of log2(bn) for a given n.Input6Output15Explanationlog2(bn) = (n * (n - 1)) / 2 = (6*(6-1))/2 = 15Input200Output19900Methodbn+1/bn = 2nbn/bn-1 = 2n-1...b2/b1 = 21, We multiply all of above in order to attain(bn+1/bn).(bn/n-1)……(b2/b1) = 2n + (n-1)+……….+1So, bn+1/b1 = 2n(n+1)/2Because we know, 1 + 2 + 3 + ………. + (n-1) + n = n(n+1)/2So, bn+1 = 2n(n+1)/2 . b1; Assume the initial value b1 = 1So, bn+1 = 2sup>n(n+1)/2Now substituting (n+1) for n, we get, ... Read More

Find n-th lexicographically permutation of a strings in C++

Arnab Chakraborty
Updated on 25-Jul-2020 16:30:39

278 Views

ConceptWith respect of a given string of length m containing lowercase alphabets only, our task to determine the n-th permutation of string lexicographically.Inputstr[] = "pqr", n = 3OutputResult = "qpr"ExplanationAll possible permutation in sorted order − pqr, prq, qpr, qrp, rpq, rqpInputstr[] = "xyx", n = 2OutputResult = "xyx"ExplanationAll possible permutation in sorted order − xxy, xyx, yxxMethodHere we use some Mathematical concept for solving this problem.The concept is based on following facts.Here, the total number of permutation of a string generated by N characters (all distinct) is N!Now, the total number of permutation of a string generated by N ... Read More

Find N distinct numbers whose bitwise Or is equal to K in C++

Arnab Chakraborty
Updated on 25-Jul-2020 10:36:52

267 Views

ConceptWith respect of given two integers N and K, our task is to determine N distinct integers whose bitwise OR is equal to K. It has been seen that if there does not exist any possible answer then print -1.InputN = 4, K = 6Output6 0 1 2InputN = 11, K = 6Output-1It is not possible to find any solution.MethodWe have knowledge that if bit-wise OR of a sequence of numbers is K then all the bit indexes which are 0 in K must also be zero in all the numbers.As a result of this, we only have those positions ... Read More

Find multiplication of sums of data of leaves at same levelss in C++

Arnab Chakraborty
Updated on 25-Jul-2020 16:24:47

154 Views

ConceptWith respect of a given Binary Tree, return following value for it.With respect of every level, calculate sum of all leaves if there are leaves at this level. Else ignore it.Calculate multiplication of all sums and return it.InputRoot of following tree       3      / \     8   6          \           10Output80First level doesn’t have leaves. Second levelhas one leaf 8 and third level also has one leaf 10. So result is 8*10 = 80InputRoot of following tree              3     ... Read More

Find missing element in a sorted array of consecutive numbers in C++

Arnab Chakraborty
Updated on 25-Jul-2020 09:38:44

448 Views

ConceptWith respect of a given array array[] of n distinct integers, elements are placed sequentially in ascending order with one missing element. Our task is to determine the missing element.Inputarray[] = {1, 2, 3, 4, 5, 6, 7, 9}Output8Inputarray[] = {-4, -2, -1, 0, 1, 2}Output-3Inputarray[] = {1, 2, 3, 4}Output-1No element is missing.MethodPrinciplesLook for inconsistency: According to this principle, the difference between any element and its index must be array[0] for every element.Example, A[] = {1, 2, 3, 4, 5} -> ConsistentB[] = {201, 202, 203, 204} -> ConsistentC[] = {1, 2, 3, 5, 6} -> Inconsistent as C[3] ... Read More

Find minimum time to finish all jobs with given constraints in C++

Arnab Chakraborty
Updated on 25-Jul-2020 09:34:02

414 Views

ConceptWith respect of a given array of jobs with different time requirements, there exists k identical assignees available and we are also provided how much time an assignee consumesto do one unit of the job. Our task is to determine the minimum time to complete all jobs with following constraints.The first constraint is that an assignee can be assigned only contiguous jobs.Here, for example, an assignee can be assigned jobs at position 1 and 2, but not at position 3, in an array.The second constraint is that two assignees cannot share (or co-assigned) a job, that means, a job cannot ... Read More

Find minimum s-t cut in a flow network in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:16:47

355 Views

Suppose we have following flow network. As we know an s-t cut is a cut that requires the source s node and a sink t node to be in different subsets, and it includes edges going from the source set to the sink side. Here the capacity of an s-t cut is represented by the sum of each edge capacity in the cut-set. Here we have to find minimum capacity s-t cut of the given network. Here the expected output is all edges of the minimum cut.So, if the input is likethen the output will be [(1, 3), (4, 3), ... Read More

Find minimum adjustment cost of an array in C++

Arnab Chakraborty
Updated on 25-Jul-2020 09:22:58

421 Views

ConceptWith respect of a given array of positive integers, we replace each element in the array so that the difference between adjacent elements in the array is either less than or equal to a given target. Now, our task to minimize the adjustment cost, that is the sum of differences between new and old values. So, we basically need to minimize Σ|A[i] – Anew[i]| where 0 ≤ i ≤ n-1, n is denoted as size of A[] and Anew[] is denoted as the array with adjacent difference less than or equal to target. Let all elements of the array is ... Read More

Find memory conflicts among multiple threads in C++

Arnab Chakraborty
Updated on 25-Aug-2020 12:19:14

202 Views

Suppose we have a RAM and that RAM is organized in blocks. there are multiple processes running on the system. We have to keep in mind that every process gets following information, (Thread T, Memory Block M, time t, R/W) This indicates the thread T was implementing memory block M at given time t and operation could be either read(R) or write(W).The following case is indicating whether it is memory conflict or not −More than one read operations at the same location are not the reason of conflict.When writing operation is being performed between x+5 to x-5 to location of ... Read More

Advertisements