Programming Articles - Page 2153 of 3366

Find All Numbers Disappeared in an Array in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:34:03

482 Views

Suppose we have an array of n elements. Some elements appear twice and other appear once. Elements are in range 1 0, then add i + 1 into the answerreturn the answerExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Intersection of Two Arrays II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:31:34

5K+ Views

Suppose we have two arrays A and B, there are few elements in these array. We have to find the intersection of them. So if A = [1, 4, 5, 3, 6], and B = [2, 3, 5, 7, 9], then intersection will be [3, 5]To solve this, we will follow these steps −Take two arrays A and Bif length of A is smaller than length of B, then swap themcalculate the frequency of elements in the array and store them into mfor each element e in B, if e is present in m, and frequency is non-zero, decrease frequency ... Read More

First Bad Version in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:22:26

987 Views

Suppose in a company, one product manager is leading a team who develops a new product. Suppose latest version fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version will be bad. So we have an array A with n elements [1, 2, … n] and we have to find the first bad version from this array.Consider we have a function isBadVersion(version_id), this will return whether the version is bad or not. For an example, suppose n = 5, and version = 4 is first bad version. So if ... Read More

Lowest Common Ancestor of a Binary Search Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:19:10

663 Views

Suppose we have a binary search tree. we have to find the Lowest common ancestor nodes of two given nodes. The LCA of two nodes p and q is actually as the lowest node in tree that has both p and q as decedent. So if the binary tree is like [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5]. The tree will be like −Here LCA of 2 and 8 is 6To solve this, we will follow these steps −If the tree is empty, then return nullif p and q both are same as root, then return ... Read More

How to implement DoublePredicate using lambda and method reference in Java?

raja
Updated on 16-Jan-2020 10:04:48

315 Views

DoublePredicate is a built-in functional interface defined in java.util.function package. This interface can accept one double-valued parameter as input and produces a boolean value as output. DoublePredicate interface can be used as an assignment target for a lambda expression or method reference. This interface contains one abstract method: test() and three default methods: and(),  or() and negate().Syntax@FunctionalInterface public interface DoublePredicate {    boolean test(double value) }Example of lambda expressionimport java.util.function.DoublePredicate; public class DoublePredicateLambdaTest {    public static void main(String args[]) {       DoublePredicate doublePredicate = (double input) -> {    // lambda expression          if(input == 2.0) {     ... Read More

Happy Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:16:36

2K+ Views

Here we will see how to detect a number n is one Happy number or not. So the happy number is a number, where starting with any positive integers replace the number by the sum of squares of its digits, this process will be repeated until it becomes 1, otherwise it will loop endlessly in a cycle. Those numbers, when the 1 has found, they will be happy number.Suppose the number is 19, the output will be true as the number is happy number. As we can see from 19, we will get12 + 92 = 8282 + 22 = ... Read More

Reverse Bits in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:14:13

6K+ Views

Suppose we have one unsigned number x, and we can easily find the binary representation of it (32bit unsigned integer). Our task is to reverse the bits. So if the binary representation is like 00000000000000000000001001110100, then reversed bits will be 00101110010000000000000000000000. So we have to return the actual number after reversing the bitsTo solve this, we will follow these steps −Suppose n is the given numberlet answer := 0for i := 31 down to 0:answer := answer OR (n AND i), and shift it to the left i timesn := n after right shifting 1 bitreturn answerExampleLet us see the ... Read More

Intersection of Two Linked Lists in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:11:47

1K+ Views

Suppose we have two linked lists A and B, there are few elements in these linked lists. We have to return the reference of the intersection points. The inputs are intersectionVal = 8, A = [4, 1, 8, 4, 5], B = [5, 0, 1, 8, 4, 5], skipA = 2 and skipB = 3, these are used to skip 2 elements from A and skip 3 elements from B.To solve this, we will follow these steps −Define a map called dwhile headA is not nulld[headA] := 1headA := next of headAwhile headB is not nullif headB in dreturn headBheadB ... Read More

Min Stack in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:27:30

3K+ Views

Here we will see how to make a stack, that can perform push, pop, top, and retrieve the min element in constant time. So the functions will be push(x), pop(), top() and getMin()To solve this, we will follow these steps −Initialize the stack by min element as infinityFor push operation push(x)if x < min, then update min := x, push x into stackFor pop operation pop()t := top elementdelete t from stackif t is min, then min := top element of the stackFor top operation top()simply return the top elementfor getMin operation getMin()return the min elementExampleLet us see the following ... Read More

Linked List Cycle in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:22:38

2K+ Views

Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node.To solve this, we will follow these steps ... Read More

Advertisements