Programming Articles - Page 2216 of 3366

Find smallest subarray that contains all elements in same order in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:07:10

372 Views

Suppose we have two arrays of size m and n, The task is to find minimum length subarray in the first array, that contains all the elements if the second array. Element in second array may be present in the large array in non-contiguous but order must be same. So if two arrays are like A = [2, 2, 4, 5, 8, 9], and B = [2, 5, 9], then the output will be 5. As the smallest subarray of A, will be [2, 4, 5, 8, 9]. Here all elements like [2, 5, 9] are in the same order. ... Read More

Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:42:16

133 Views

Suppose we have a series called f. Each term of f, follows this rule f[i] = f[i – 1] – f[i – 2], we have to find the Nth term of this sequence. f[0] = X and f[1] = Y. If X = 2 and Y = 3, and N = 3. The result will be -2.If we see this closely, there will be almost six terms before the sequence starts repeating itself. So we will find the first 6 terms of the series and then the Nth term will be the same as (N mod 6)th term.Example#include< iostream> using ... Read More

Find the last digit when factorial of A divides factorial of B in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:37:38

185 Views

If we have two integers A and B, and B >= A, we have to compute the last digit of the B! / A! When the value of A = 2 and B = 4, then result is 2, 2! = 2 and 4! = 24, so 24/2 = 12. the last digit is 2.As we know that the last digit of factorial will be in set {0, 1, 2, 4, 6}, then follow these steps to solve this problem −We will find the difference between A and Bif diff >=5, then answer is 0otherwise, iterate from (A + 1) ... Read More

Create a stopwatch using python

Pradeep Elance
Updated on 19-Dec-2019 11:40:26

1K+ Views

A stopwatch is used to measure the time interval between two events usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to creat a stopwatch by using its tkinter library.This library will have the GUI features to create a stopwatch showing the  Start, Stop and Reset  option. The key component of the program is using the lable.after()  module of tkinter.label.after(parent, ms, function = None) where parent: The object of the widget which is using this function. ms: Time in miliseconds. function: ... Read More

Count set bits using Python List comprehension

Pradeep Elance
Updated on 19-Dec-2019 11:34:21

188 Views

Set bits are the bits representing 1 in the binary form of a number. In this article we will see how to count the number of set bits in a given decimal number.#53 in binary is: 110101 The number of set bits is the number of ones. Here it is 4.In the below program we take the number and convert it to binary. As the binary conversion contains 0b as the first two characters, we remove it using string splitting technique. Then use a for loop to count each bit of the binary number if that value of the digit ... Read More

Find the K-th minimum element from an array concatenated M times in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:35:33

150 Views

Consider we have an array A, and another two integers K and M. We have to find Kth minimum element after concatenating the array to itself M number of times. Suppose the array is like A = [3, 1, 2], K = 4 and M = 3, so after concatenating A, 3 times, it will be [3, 1, 2, 3, 1, 2, 3, 1, 2], the 4th smallest element is 2 here.To solve this problem, we will sort the array A, then return the value, present at index ((K – 1)/M), of the array.Example Live Demo#include #include using namespace std; int ... Read More

Find the Diameter or Longest chord of a Circle in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:25:00

136 Views

Suppose we have radius r is given. We have to find the diameter or longest chord of the circle. If the radius is 9, and diameter will be 18. This task is extremely simple, we have to find the 2*r, that is the diameter of the circle.Example Live Demo#include using namespace std; int getDiameter(int r) {    return 2*r; } int main() {    int r = 9;    cout

Find the common nodes in two singly linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:23:36

454 Views

Suppose we have two singly-linked lists. We have to find the total number of common nodes in both the singly linked list. So if two lists are like [15, 16, 10, 9, 7, 17], and [15, 16, 40, 6, 9], there are three common nodes.Traverse both lists up to end of the list using two nested loops, for every node in the list, check if it is matched with any node of the second list or not. If a match is found, then increase the counter, and finally return the count.Example Live Demo#include using namespace std; class Node {    public: ... Read More

Binary list to integer in Python

Pradeep Elance
Updated on 19-Dec-2019 11:14:51

2K+ Views

We can convert a list of 0s and 1s representing a binary number to a decimal number in python using various approaches. In the below examples we use the int() method as well as bitwise left shift operator.Using int()The int() method takes in two arguments and changes the base of the input as per the below syntax.int(x, base=10) Return an integer object constructed from a number or string x.In the below example we use the int() method to take each element of the list as a string and join them to form a final string which gets converted to integer ... Read More

How to write a conditional expression in lambda expression in Java?

raja
Updated on 11-Jul-2020 07:48:12

3K+ Views

The conditional operator is used to make conditional expressions in Java. It is also called a Ternary operator because it has three operands such as boolean condition,  first expression, and second expression.We can also write a conditional expression in lambda expression in the below program.Exampleinterface Algebra {    int substraction(int a, int b); } public class ConditionalExpressionLambdaTest {    public static void main(String args[]) {       System.out.println("The value is: " + getAlgebra(false).substraction(20, 40));       System.out.println("The value is: " + getAlgebra(true).substraction(40, 10));    }    static Algebra getAlgebra(boolean reverse) {       Algebra alg = reverse ... Read More

Advertisements