Programming Articles - Page 2188 of 3366

Maximum and Minimum in a square matrix in C++

Narendra Kumar
Updated on 31-Dec-2019 11:58:50

236 Views

Problem statementGiven a square matrix of order n*n, find the maximum and minimum from the matrixExampleIf given matrix is −{{15, 17, 19}, {5, 1, 7}, {14, 5, 16}} then Minimum number is 1 and maximum number is 19AlgorithmSelect two elements from the matrix one from the start of a row of the matrix another from the end of the same row of the matrixCompare them and next compare smaller of them to the minimum of the matrix and larger of them to the maximum of the matrix.We can see that for two elements we need 3 compare so for traversing ... Read More

Maximum 0’s between two immediate 1’s in binary representation in C++

Narendra Kumar
Updated on 31-Dec-2019 11:53:54

875 Views

Problem statementGiven a number n, the task is to find the maximum 0’s between two immediate 1’s in binary representation of given n. Return -1 if binary representation contains less than two 1’sExampleIf input number is 35 then its binary representation is −00100011In above binary representation there are 3 0’s between two immediate 1’s. Hence answer is 3.AlgorithmWe can use bitwise shift operator to solve this problem. We need to find the position of two immediate 1’s in binary representation of n and maximize the difference of these position.If number is 0 or power of 2 then return -1IInitialize variable ... Read More

Maximum element in min heap in C++

Narendra Kumar
Updated on 31-Dec-2019 11:48:37

351 Views

Problem statementGiven a minimum heap find maximum element in that.ExampleIf input heap is −Then maximum element is 55AlgorithmIn minimum heap parent node will be lesser than its children. Hence we can conclude that a non-leaf node cannot be the maximum.Search maximum element in the leaf nodesExampleLet us now see an example − Live Demo#include using namespace std; int getMaxElement(int *heap, int n) {    int maxVal = heap[n / 2];    for (int i = n / 2 + 1; i < n; ++i) {       maxVal = max(maxVal, heap[i]);    }    return maxVal; } int main() {    int heap[] = {15, 27, 22, 35, 29, 55, 48}; int n = sizeof(heap) / sizeof(heap[0]);    cout

Maximizing Unique Pairs from two arrays in C++

Narendra Kumar
Updated on 31-Dec-2019 11:45:55

374 Views

Problem statementGiven two arrays of equal size N, form maximum number of pairs by using their elements, one from the first array and second from the second array, such that an element from each array is used at-most once and the absolute difference between the selected elements used for forming a pair is less than or equal to a given element K.ExampleIf input is −arr1[] = {3, 4, 5, 2, 1}arr2[] = {6, 5, 4, 7, 15}and k = 3 then we can form following 4 pairs whose absolute difference is less than or equal to 3 −(1, 4), (2, ... Read More

Maximizing the elements with a[i+1] > a[i] in C++

Narendra Kumar
Updated on 31-Dec-2019 11:41:55

379 Views

Problem statementGiven an array of N integers, rearrange the array elements such that the next array element is greater than the previous element arr[i+1] > arr[i]ExampleIf input array is {300, 400, 400, 300} then rearranged array will be −{300, 400, 300, 400}. In this solution we get 2 indices with condition arr[i+1] > arr[i]. Hence answer is 2.AlgorithmIf all elements are distinct, then answer is simply n-1 where n is the number of elements in the arrayIf there are repeating elements, then answer is n – maxFrequencyExampleLet us now see an example − Live Demo#include #define MAX 1000 using namespace ... Read More

How to use Function and BiFunction interfaces in lambda expression in Java?

raja
Updated on 13-Jul-2020 06:37:00

2K+ Views

The Function interface is a pre-defined functional interface that can be used as an assignment target for a lambda expression or method reference. It takes a single parameter and returns result by calling the apply() method. While the BiFunction interface is also a pre-defined functional interface that takes two parameters and returns a result. It is similar to the Function interface except it takes two parameters.Syntax@FunctionalInterface public interface Function @FunctionalInterface public interface BiFunctionExampleimport java.util.function.BiFunction; import java.util.function.Function; public class SampleFunctionBiFunctionTest {    public static void main(String[] args) {       Function printNumber = a -> a*10;       System.out.println("The number is: "+ printNumber.apply(10));   ... Read More

Maximizing array sum with given operation in C++

Narendra Kumar
Updated on 31-Dec-2019 11:38:48

181 Views

DescriptionThere is an array of (2 * n – 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array.ExampleIf input array is {-2, 100, -3} then we can obtain maximum changing sign of -2 and -3. After changing sign array becomes −{2, 100, 3} and maximum sum of this array is 105.AlgorithmCount negative numbersCalculate the sum of the array by taking absolute values of the numbers.Find the minimum number of the array by ... Read More

How to use Consumer and BiConsumer interfaces in lambda expression in Java?

raja
Updated on 13-Jul-2020 06:30:59

4K+ Views

A Consumer interface is a predefined functional interface that can be used when creating lambda expressions or method references. This interface represents an operation that accepts a single input parameter and doesn't return anything. It contains only one method named accept(). The BiConsumer interface is similar to a Consumer interface and accepts two input parameters and doesn’t return anything.Syntax@FunctionalInterface public interface Consumer @FunctionalInterface public interface BiConsumerExampleimport java.util.*; import java.util.function.*; public class ConsumerBiConsumerTest {    public static void main(String[] args) {       Consumer c = (x) -> System.out.println(x.toLowerCase());  // lambda expression       c.accept("Raja");         Consumer con = (x) -> {  // ... Read More

How to implement an instance method reference using a class name in Java?

raja
Updated on 13-Jul-2020 05:40:25

843 Views

Method reference is a simplified form of the lambda expression. It can specify a class name or instance name followed by the method name. The "::" symbol can separate a method name from the name of an object or class.An instance method reference refers to an instance method of any class. In the below example, we can implement an instance methods reference using the class name.Syntax::Exampleimport java.util.*;; import java.util.function.*; public class ClassNameRefInstanceMethodTest {    public static void main(String args[]) {       List empList = Arrays.asList(          new Employee("Raja", 15000),          new Employee("Adithya", 12000),          new Employee("Jai", 9000),   ... Read More

How can we write Callable as a lambda expression in Java?

raja
Updated on 13-Jul-2020 05:32:03

7K+ Views

A Callable interface defined in java.util.concurrent package. An object of Callable returns a computed result done by a thread in contrast to a Runnable interface that can only run the thread. The Callable object returns Future object that provides methods to monitor the progress of a task executed by a thread. An object of the Future used to check the status of a Callable interface and retrieves the result from Callable once the thread has done.In the below example, we can write a Callable interface as a Lambda Expression.Exampleimport java.util.concurrent.*; public class LambdaCallableTest {    public static void main(String args[]) throws InterruptedException {       ExecutorService executor = Executors.newSingleThreadExecutor();   ... Read More

Advertisements