Programming Articles - Page 2152 of 3363

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

Convert given string so that it holds only distinct characters in C++

Ayush Gupta
Updated on 16-Jan-2020 07:36:25

277 Views

In this tutorial, we will be discussing a program to convert given string so that it holds only distinct characters.For this we will be provided with a string. Our task is to traverse through the string and replace all the recurring characters with any random characters that are not already present into the string.Example Live Demo#include using namespace std; //collecting the distinct characters //in the string int calculate_zero(int i, int occurrences[]){    while (i < 26) {       //if it is present only once       if (occurrences[i] == 0)          return i;     ... Read More

Convert given array to Arithmetic Progression by adding an element in C++

Ayush Gupta
Updated on 16-Jan-2020 07:29:56

239 Views

In this tutorial, we will be discussing a program to convert given array to arithmetic progression by adding an element.For this we will be provided with an array. Our task is to convert the given array into an arithmetic progression by adding a single element to it and return the added element. If it is not possible, return -1.Example Live Demo#include using namespace std; //returning the number to be added int print_number(int arr[], int n){    sort(arr, arr+n);    int d = arr[1] - arr[0];    int numToAdd = -1;    bool numAdded = false;    for (int i = 2; ... Read More

How to implement DoubleConsumer using lambda expression in Java?

raja
Updated on 14-Jul-2020 06:38:16

310 Views

DoubleConsumer is a functional interface from java.util.function package. This functional interface accepts a single double-valued argument as input and produces no output. This interface can be used as an assignment target for a lambda expression or method reference. DoubleConsumer contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface DoubleConsumer {    void accept(double value); }Example-1import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest1 {    public static void main(String args[]) {       DoubleConsumer increment = doubleVal -> {       // lambda expression          System.out.println("Incrementing " + doubleVal + " by one");          System.out.println("Current Value : ... Read More

Convert from any base to decimal and vice versa in C++

Ayush Gupta
Updated on 16-Jan-2020 07:26:39

1K+ Views

In this tutorial, we will be discussing a program to convert from any base to a decimal and vice versa.For this we will be provided with an integer and its base. Our task is to convert the number to its decimal equivalent. Further we will also be performing the reverse of this procedure as well.Example Live Demo#include #include //returning values of a character int val(char c) {    if (c >= '0' && c = 0; i--) {       if (val(str[i]) >= base) {          printf("Invalid Number");          return -1;   ... Read More

Convert decimal fraction to binary number in C++

Ayush Gupta
Updated on 16-Jan-2020 07:21:31

624 Views

In this tutorial, we will be discussing a program to convert decimal fraction to a binary number.For this we will be provided with a decimal fraction and integer ‘k’. Our task is to convert the given decimal fraction into its binary equivalent upto the given ‘k’ digits of decimal precision.Example Live Demo#include using namespace std; //converting decimal to binary number string convert_tobinary(double num, int k_prec) {    string binary = "";    //getting the integer part    int Integral = num;    //getting the fractional part    double fractional = num - Integral;    //converting integer to binary    while (Integral) ... Read More

Convert BST to Min Heap in C++

Ayush Gupta
Updated on 16-Jan-2020 07:12:06

386 Views

In this tutorial, we will be discussing a program to convert a binary search tree to a min heap.For this we will be provided with a binary search tree. Our task is to convert the given binary search tree into a min heap such that following the condition of the binary search tree when elements are compared with themselves.Example Live Demo#include using namespace std; //node structure of BST struct Node {    int data;    Node *left, *right; }; //node creation struct Node* getNode(int data) {    struct Node *newNode = new Node;    newNode->data = data;    newNode->left = ... Read More

Advertisements