Skip to main content

Posts

Showing posts from December, 2022

Java Program for Find Minimum Element in Rotated Sorted Array

Search Minimum number in Rotated and Sorted Array in Java using Binary Search Problem Description : Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [5, 6, 7, 0, 1, 2, 4] if it was rotated 3 times. [2, 4, 5, 6, 7, 0, 1] if it was rotated 5 times. Example 1 : Input : [5, 6, 7, 0, 1, 2, 4] Output : 0 Example 2 : Input : [5, 8, 10, 15, 20, 50, 100, 200] Output : 5 Example 3 : Input : [100, 50, 25, 5, 1] Output : 1 We will use Binary Search for finding solution. Solution 1 : Find Minimum Element in Rotated Sorted Array in Java   import   java . util . Scanner ;   public   class   FindMinimumInRotatedSortedArray   {      public   static   void   main ( String []   args )   {                   Scanner   sc   =   new   Scanner ( System . in );          System . out . println ( "Enter array size : " );          int   size   =   sc . nextInt ();          int []   array   =   new  

How to Implement One to Many and Many to One Mapping in Spring Boot using JPA

Spring Boot CRUD example using One-to-Many and Many to One mapping | With Thymeleaf User Interface In this tutorial, we will learn how to use @OneToMany and @ManyToOne annotation using JPA (Java Persistent API) in Spring Boot. We also attach Thymeleaf for User Interface. In past tutorial, we already created Spring Boot CRUD with Rest API, JPA and MySql. Please refer that one first, we will continue from there. Spring Boot application with Thymeleaf, Rest API, JPA and MySql Database    For applying One to Many relationship, we need another POJO class. In past we already created Book class, now we will create new class Author . As we know Author have multiple Books, so we can easily apply One to Many operation. Lets create POJO class for Author and apply @OneToMany on Book .  Define List of Book and apply @OneToMany annotation on field. We are using mappedBy property, so Author table does not create new column.  We already learn about mappedBy property in One-to-One annotation. Please r

How to solve Forming a Magic Square Problem? | Java Solution

Java Solution for Forming a Magic Square Problem | HackerRank Problem Problem Description : We define a to be an n * n matrix of distinct positive integers from 1 to n^2 where the sum of any row, column, or diagonal of length n is always equal to the same number: the magic constant . You will be given a 3 * 3 matrix of integers in the inclusive range [1, 9]. We can convert any digit 'a' to any other digit 'b' in the range [1, 9] at cost of |a - b|. Given s, convert it into a magic square at minimal cost. Print this cost on a new line. In recreational mathematics, a square array of numbers, usually positive integers, is called a magic square if the sums of the numbers in each row, each column, and both main diagonals are the same. There are 8 ways to make a 3×3 magic square . See below image for reference. Example 1 : Input : 5 3 4 1 5 8 6 4 2 Output : 7  8 3 4 1 5 9 6 7 2 This took three replacements at a cost of |5 - 8| + |8 - 9| + |4 - 7| = 3 + 1 + 3 = 7. Exa

Guide to create One to One Mapping in Spring Boot using JPA

One to One Relationship using JPA in Spring Boot Application | Unidirectional and Bidirectional JPA(Java Persistence API) is specification that used object relation mapping to manage relational data in Java application. In this tutorial, we will see how to create Spring Boot application that uses One to One mapping relation.  In past tutorial, We had already seen Spring Boot CRUD operation So please check out first for better understanding of one-to-one mapping. Spring Boot Crud Operation with Thymeleaf, JPA and MySql Database We will use tow entities, User and UserDetails for our tutorial.  Unidirectional One to One Mapping : If one persistence object uses other and in back if other is not using the first persistence object then it becomes unidirectional. @OneToOne mapping uses FetchType.Lazy loading strategy.  In Unidirectional mapping, we add @OneToOne mapping in only one class. So in User class we annotate UserProfile field with @OneToOne mapping. User.java import javax.persistenc

Mark and Toys Hacker Rank In Java | Java Solution

Hacker Rank Mark and Toys Solution In Java Problem Statement :- Mark and Jane are very happy after having their first child. Their son loves toys, so Mark wants to buy some. There are a number of different toys lying in front of him, tagged with their prices. Mark has only a certain amount to spend, and he wants to maximize the number of toys he buys with this money. Given a list of toy prices and an amount to spend, determine the maximum number of gifts he can buy. Note : Each toy can be purchased only once.  prices = [1,2,3,4] k = 7 The budget is 7 units of currency. He can buy items that cost [1,2,3] for 6, or [3,4] for 7 units. The maximum is 3 items. Example 1 : Input : prices = [1, 2, 3, 4] k = 7 Output : 3 Explanation : The budget is 7 units of currency. He can buy items that cost [1,2,3] for 6, or [3,4] for 7 units. The maximum is 3 items. Example 2 : Input : prices = [20, 3, 10, 8, 1, 12, 5, 6] k = 30 Output : 5 Explanation : The budget is 30 units of currency. He c

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 and at last pr