Binary Number with Alternating Bits in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:39:16

461 Views

Suppose we have a positive integer, we have to check whether it has alternating bits − so, two adjacent bits will always have different values.So, if the input is like 10, then the output will be True, as binary representation of 10 is 1010.To solve this, we will follow these steps −p := n AND 1if n < 2, then −return truen := n/2while n is non-zero, do −c := n AND 1if c XOR p is same as 0, then −return falsep := cn := n/2return trueLet us see the following implementation to get better understanding −Example Live Demo#include ... Read More

Java Program for Counting Sort

AmitDiwan
Updated on 04-Jul-2020 09:39:15

209 Views

The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Example Live Demoimport java.util.*; public class Demo{    static void count_sort(int[] arr){       int max_val = Arrays.stream(arr).max().getAsInt();       int min_val = Arrays.stream(arr).min().getAsInt();       int range = max_val - min_val + 1;       int count[] = new int[range];       int result[] = new int[arr.length];       for (int i = 0; i < arr.length; i++){          count[arr[i] - min_val]++; ... Read More

Employee Importance in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:38:36

267 Views

Suppose we have a data structure of employee information, there are employee's unique id, his importance value and his direct subordinates' id. As an example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. And suppose their importance values are 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []].So, if we have the employee information of a company, and an employee id, we have to find the total importance value of ... Read More

Baseball Game in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:36:37

2K+ Views

Suppose we have a baseball game point recorder. We have a list of strings; each string can be one of the 4 following types −Integer (one round's score) − Indicates the number of points we get in this round."+" (one round's score) − Indicates the points we get in this round are the sum of the last two valid round's points."D" (one round's score ) − Indicates the points we get in this round are the doubled data of the last valid round's points."C" (an operation, which isn't a round's score) − Indicates the last valid round's points we get ... Read More

Longest Continuous Increasing Subsequence in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:34:52

845 Views

Suppose we have an array of integers; we have to find the length of longest continuous increasing subarray.So, if the input is like [2,4,6,5,8], then the output will be 3. As the longest continuous increasing subsequence is [2,4,6], and its length is 3.To solve this, we will follow these steps −if size of nums

Second Minimum Node in a Binary Tree in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:33:20

602 Views

Suppose there is a non-empty special binary tree with some non-negative value, here each node in this tree has exactly two or zero children. If the node has two children, then this node's value is the smaller value among its two children. In other words, we can say that [root.val = minimum of root.left.val, root.right.val]. If we have such binary tree, we have to find the second minimum value in the set made of all the nodes' value in the whole tree. If there is no such element, then return -1 instead.So, if the input is likethen the output will ... Read More

Construct String from Binary Tree in Python

Arnab Chakraborty
Updated on 04-Jul-2020 09:28:32

868 Views

Suppose we have a binary tree we have to make a string consists of parenthesis and integers from a binary tree with the preorder traversing way. A null node will be represented by empty parenthesis pair "()". And we need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.So, if the input is likethen the output will be 5(6()(8))(7)To solve this, we will follow these steps −ans := blank stringDefine a function pot(). This will take node, sif is null, thenreturn blank stringif if left or right ... Read More

Numeric Promotion in Conditional Expression in Java

AmitDiwan
Updated on 04-Jul-2020 09:20:21

154 Views

The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Example Live Demoimport java.io.*; public class Demo{    public static void main (String[] args){       Object my_obj = true ? new Integer(91) : new Float(89);       System.out.println(my_obj);    } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression ... Read More

Java Lambda Expression with Collections

AmitDiwan
Updated on 04-Jul-2020 09:16:54

873 Views

Sorting the elements of a list using lambda expression −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements ... Read More

Clear Method in Java ConcurrentHashMap

AmitDiwan
Updated on 04-Jul-2020 09:06:38

323 Views

The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.Syntaxpublic void clear()Let us see an example −Example Live Demoimport java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{    public static void main(String[] args){       Map my_map = new ConcurrentHashMap();       my_map.put("This", "35");       my_map.put("is", "78");       my_map.put("sample", "99");       System.out.println("The map contains the below elements " + my_map);       my_map.clear();       System.out.println("The elements after the clear function is called on it " + my_map);    } ... Read More

Advertisements