Skip to main content

Posts

Showing posts from November, 2022

Java Program for Search an element in Rotated Sorted Array

Search target element in Rotated and Sorted Array in Java with explanation Problem Description : Given a sorted and rotated array nums[] of size N and a target, the task is to find the target in the nums[] array. Array is sorted but it is also rotated. For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Example 1 : Input:  nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2 : Input: nums = [4,5,6,7,0,1,2], target = 8 Output: -1 Example 3 : Input: nums = [1,2,3,4,6,8,12,15], target = 2 Output : 1 Example 4 : Input: nums = [2, 4, 8, 16, 32, 0, 1], target = 5 Output: -1 Here we can use binary search, but given array is rotated so we have to apply extra logic with binary search. Solution 1 : Searching target element from rotated sorted array in Java import   java . util . Scanner ; public   class   SearchInRotatedSortedArray   {      public   static   void   main ( String []   args )   {          Scanner   sc   =   new   Scanner ( System . in );

Find First and Last Position of Element in Sorted Array with Explanation | Java

Find First and Last Index Occurrence of given target from Java Array using Binary Search Problem Description : Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1] . Example 1 : Input: nums = [5, 7, 7, 8, 8, 8, 10], target = 8 Output: [3, 5] Example 2 : Input: nums = [5, 7, 7, 8, 8, 8, 10], target = 6 Output: [-1, -1] We can solve this problem using brute force approach but it will take more time than binary search. So lets see binary search approach. Solution 1 : Finding First and Last Position of Element in Array. import   java . util . Scanner ; public   class   FindFirstAndLastPosition   {      public   static   void   main ( String []   args )   {          Scanner   sc   =   new   Scanner ( System . in );          System . out . println ( "Enter array size : " );          int   size   =   sc . nextInt ();          int []   array   =  

Java 8 Stream API Filter() Method with Examples | Programming Blog

Java 8 Stream Filter with Lambda Expression Java 8 Stream Filter() method with Examples What is Filter in Java Stream?      Simply, as a name suggest it filters data based on given predicate (boolean) Condition. The condition is applied to all element of Stream, and those who satisfied the condition moves to next stage and those who does not pass filtered out. For example, If you want to filter list of Integer based on greater than some value or if you want to get even or odd data then you can easily filtered out. Stream Filter() method is Intermediate Operation. If you want to learn about difference between Intermediate and Terminal operation check out:- Difference between Intermediate and Terminal Operation The benefit of using filter() method is lazy evaluation. Means no data comparison is performed unless you call a terminal operation like forEach() or Collect() . We will use Lambda expression for filter elements based on given Predicate. Learn more about Lambda Expression : Lambd

Spring Boot Crud Operation with Thymeleaf, JPA and MySql Database | Step by Step

Spring Boot application with Thymeleaf, Rest API, JPA and MySql Database In this article, we will seen how to create simple Spring boot application with Create, Read, Update and Delete operations with MySql and Thymeleaf. But first checkout what is Thymeleaf? Thymeleaf :- Thymeleaf is modern server side Java template engine for web and standalone environment. Thymeleaf's main goal is to bring elegant natural templates to your development workflow — HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams. So lets start our Tutorial :- First learn how to create Spring Boot application with Rest API, JPA and Mysql.  Spring Boot CRUD operation with Rest API, JPA and MySql   Project Structure :- For Create Spring Boot project and Sql table click on Above given link. 1. Configure Maven Dependency For work with Thymeleaf we have to include " spring-boot-starter-thymeleaf " Dependen