Programming Articles - Page 2181 of 3366

Print consecutive characters together in a line in C++

sudhir sharma
Updated on 03-Jan-2020 07:47:02

311 Views

In this problem, we are given a string of characters and we have to print the same string in such a way that if two or more characters are consecutive then print them together in a single line otherwise print them in different lines i.e. with a line break.Let’s take an example to understand the concept better, Input : abcxstk Output : abc x st kExplanation − Since abc are in a sequence they are printed in a line. Then comes x which is not in sequence, so we add a linebreak here. Next is s which is not a ... Read More

How to implement PropertyChangeListener using lambda expression in Java?

raja
Updated on 13-Jul-2020 08:13:49

5K+ Views

A PropertyChangeListener is a functional interface from java.beans package. It has one abstract method propertyChange() and gets called when a bound property is changed. This method takes a PropertyChangeEvent argument that has details about an event source and the property that has changed. A PropertyChangeSupport can be used by beans that support bound properties. It can manage a list of listeners and dispatches property change events. The PropertyChangeListener's implementer does the role of an Observer and bean wrapping the PropertyChangeSupport is Observable.Syntaxvoid propertyChange(PropertyChangeEvent evt)Exampleimport java.beans.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class PropertyChangeListenerLambdaTest {    public static void main(String [] args) {   ... Read More

Print cousins of a given node in Binary Treein C++

sudhir sharma
Updated on 03-Jan-2020 07:42:50

260 Views

Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.Example, In this problem, we are given a binary tree and we have a node of the tree and we have to find the cousin nodes of the node. No sibling nodes are to be printed for the binary tree.Let’s take an example, For the above binary tree, the cousin node is 5.To make the concept more clear let’s describe cousin node. In a binary tree, two nodes are said to ... Read More

Print digit’s position to be removed to make a number divisible by 6 in C++

sudhir sharma
Updated on 03-Jan-2020 07:32:06

271 Views

In this problem, we are given number and we have to remove more digit from the number. So that the new number formed after removal is divisible by 6.Let’s take an example to learn the concept better −Input : 1324 Output : 4Explanation − on removing the 4th number we will get 132 which is divisible by 6.Here, we are given a number and we have to return the position from where the number is removed to make it divisible by 6.For solving this problem, we will try to create a logic that solves the problem. For this, we will ... Read More

Print direction of moves such that you stay within the [-k, +k] boundary in C++

sudhir sharma
Updated on 03-Jan-2020 07:27:50

206 Views

In this problem, we have to find a valid way to move positive direction or negative direction in such a way that we stay within a certain limit that is provided by the user.Here, We are given a certain maximum limit K, which is the maximum value to which we can move and an array of n positive values to move. We have to return the sequence i.e. positive or negative directions to move so that it never crosses K value.Let’s take an example to understand the topic better, Input : K = 56 and the array is [25 , ... Read More

Print distinct sorted permutations with duplicates allowed in input in C++

sudhir sharma
Updated on 03-Jan-2020 07:24:47

775 Views

In this programming problem, we are given a string and we have to print the distinct sorted permutations of the string elements that can be formed. The condition with this problem is that the string may contain a character that will arise more than one time. Also, the given string is inputted in sorted order.Let’s take an example to understand the concept better, Input : ABD Output : ABD , ADB , BAD , BDA , DAB , DBA INPUT : RSTU OUTPUT : RSTU , RSUT , RTSU , RTUS , RUST , RUTS , SRTU , SRUT , ... Read More

Binary Search (Recursive and Iterative) in C Program

sudhir sharma
Updated on 26-Jun-2024 23:40:25

85K+ Views

Binary Search is a search algorithm that is used to find the position of an element (target value ) in a sorted array. The array should be sorted prior to applying a binary search. Binary search is also known by these names, logarithmic search, binary chop, half interval search. Working of Binary Search The binary search algorithm works by comparing the element to be searched by the middle element of the array and based on this comparison follows the required procedure. Case 1 − element = middle, the element is found return the index. Case 2 − element > middle, ... Read More

C Program for Activity Selection Problem

sudhir sharma
Updated on 03-Jan-2020 07:08:44

6K+ Views

The activity selection problem is a problem in which we are given a set of activities with their starting and finishing times. And we need to find all those activities that a person can do performing the single activity at a time.The greedy algorithm is appointed in this problem to select the next activity that is to be performed. Let’s first understand the greedy algorithm.Greedy Algorithm is an algorithm that tries to find the solution to a problem by finding the solution step by step. For selecting the next step, the algorithm also selected the step that seems to be ... Read More

C / C++ Program for Subset Sum (Backtracking)

sudhir sharma
Updated on 03-Jan-2020 07:05:07

12K+ Views

Backtracking is a technique to solve dynamic programming problems. It works by going step by step and rejects those paths that do not lead to a solution and trackback (moves back ) to the previous position.In the subset sum problem, we have to find the subset of a set is such a way that the element of this subset-sum up to a given number K. All the elements of the set are positive and unique (no duplicate elements are present).For this, we will create subsets and check if their sum is equal to the given number k. Let's see a ... Read More

Binomial Random Variables in C++

sudhir sharma
Updated on 03-Jan-2020 07:01:33

454 Views

Random variables are those variables that are an outcome of the outcomes of a process that has the probability of giving rise to multiple outcomes. For example, The variable denoting head or tail as an outcome on tossing a coin is a random variable.A binomial random variable is a special type of random variable whose value is related to an event that has a fixed probability of an outcome in an event.There are certain properties that are possessed by a binomial random variable that makes it special. These are a must for a variable to become a binomial random variable ... Read More

Advertisements