In this tutorial, we will be discussing a program to find maximum subarray size, such that all subarrays of that size have sum less than k.For this we will be provided with an array of size N and an integer k. Our task is to find the length of subarray such that all the sub-arrays of that length in the given array sum to be less than or equal to k.Example Live Demo#include using namespace std; //finding maximum length subarray int bsearch(int prefixsum[], int n, int k) { int ans = -1; //performing binary search int left = ... Read More
In this tutorial, we will be discussing a program to find maximum students to pass after giving bonus to everybody and not exceeding 100 marks.For this we will be provided with an array containing marks of N students. Our task is to get more student pass the exam (50 marks required) by giving each student the same amount of bonus marks without any student exceeding 100 marks.Example Live Demo#include #include using namespace std; int check(int n, int marks[]) { int* x = std::max_element(marks, marks+5); int bonus = 100-(int)(*x); int c = 0; for(int i=0; i= 50) ... Read More
The java.util.function package defines several in-built functional interfaces that can be used when creating lambda expressions or method references.Inbuilt functional interfaces:1) Function InterfaceThe Function interface has only one single method apply(). It can accept an object of any data type and returns a result of any datatype.Exampleimport java.util.*; import java.util.function.*; public class FunctionTest { public static void main(String args[]) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"}; Function converter = (all) -> { // lambda expression String names = ""; ... Read More
A constructor reference is just like a method reference except that the name of the method is "new". It can be created by using the "class name" and the keyword "new" with the following syntax.Syntax :: newIn the below example, we are using java.util.function.Function. It is a functional interface whose single abstract method is the apply(). The Function interface represents an operation that takes single argument T and returns a result R.Exampleimport java.util.function.*; @FunctionalInterface interface MyFunctionalInterface { Employee getEmployee(String name); } class Employee { private String name; public Employee(String name) { this.name = name; } public String ... Read More
When we are using a lambda expression for java listener, we do not have to explicitly implement the ActionListener interface. Instead, we can use the below syntax.Syntaxbutton.addActionListener(e -> { // some statements });An ActionListener interface defines only one method actionPerformed(). It is a functional interface which means that there's a place to use lambda expressions to replace the code.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaListenerTest extends JFrame { public static void main(String args[]) { new LambdaListenerTest(); } private JButton button; public ClickMeLambdaTest() { setTitle("Lambda Expression Test"); button = ... Read More
A lambda expression passed in a method that has an argument of type of functional interface. If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type.In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface". Exampleinterface TestInterface { boolean test(int a); } class Test { // lambda expression can be passed as first argument in the check() method static boolean check(TestInterface ti, int b) { return ti.test(b); } } public class ... Read More
Java supports lambda expressions but not the Closures. A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.Syntax([comma seperated parameter-list]) -> {body}In the below example, the create() method has a local variable "value" with a short life and disappears when we exit the create() method. This method returns the closure to the caller in the main() method after that ... Read More
The lambda expressions were introduced in Java 8 and facilitate functional programming. A lambda expression works nicely together only with functional interfaces and we cannot use lambda expressions with more than one abstract method.Characteristics of Lambda ExpressionOptional Type Declaration − There is no need to declare the type of a parameter. The compiler inferences the same from the value of the parameter.Optional Parenthesis around Parameter − There is no need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.Optional Curly Braces − There is no need to use curly braces in the expression body if the body contains a ... Read More
A lambda expression body can't throw any exceptions that haven't specified in a functional interface. If the lambda expression can throw an exception then the "throws" clause of a functional interface must declare the same exception or one of its subtype.Exampleinterface Student { void studentData(String name) throws Exception; } public class LambdaExceptionTest { public static void main(String[] args) { // lamba expression Student student = name -> { System.out.println("The Student name is: " + name); throw new Exception(); }; try { student.studentData("Adithya"); } catch(Exception e) { } } }OutputThe Student name is: Adithya
The effectively final variables refer to local variables that are not declared final explicitly and can't be changed once initialized. A lambda expression can use a local variable in outer scopes only if they are effectively final.Syntax(optional) (Arguments) -> bodyIn the below example, the "size" variable is not declared as final but it's effective final because we are not modifying the value of the "size" variable.Exampleinterface Employee { void empData(String empName); } public class LambdaEffectivelyFinalTest { public static void main(String[] args) { int size = 100; Employee emp = name -> { // lambda ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP