Skip to main content

Posts

Showing posts with the label java example

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 );   ...

How to Calculate median in Java? | With Array and List | Programming Blog

Find median of Java Array and List values using 2 ways What is Median? Median is mid value of particular data set. This data set must be sorted.  In simple word, Median is mid value of sorted data. Median will be middle, if total number of elements is odd or average of middle elements when total number of elements is even. Example : When total number of elements are odd 1, 2, 3, 4, 5, 6, 7 Median will be : 4 2, 3, 5, 7, 8 Median will be : 5 When total number of elements are even 1, 2, 3, 4 Median will be : (2+3) / 2 : 2.5 2, 4, 6, 8, 10, 20, 25, 30 Median will be : (8+10) / 2 : 9 So the formula become for our code : For odd elements : Median = (Total length / 2) For even elements : Median = (Total length / 2) + (Total length / 2 - 1) / 2 So lets jump on code. We will take elements form user. Method 1 : Using code logic In this approach, we does not use any library function for find median. Example 1 : Find median from Java array import java.util.Arrays; import java.util.Scanner; pu...

Write a program in Java to check if a character is an uppercase vowel or lowercase vowel or not

Java program to check given alphabet is an uppercase or lowercase vowel or not In this program, we just have to find given user input character is uppercase or lowercase vowel or not. We will seen two solution approach for this problem Using List Using Switch Case Example : Input : 'A' Output : Character is UpperCase Vowel  Input : 'e' Output : Character is LowerCase Vowel Input : 'j' Output : Character is Not a Vowel Solution Approach : We will use ArrayList for store vowels and check if given character is uppercase or lowercase vowel or not.  So lets jump on code. Solution 1 : Using List import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CheckVowelsCase {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         System.out.println("Enter Character");         char ch = sc.next().charAt(0); ...

Find Numbers with Even Number of Digits in Java | Programming Tutorial

Java Solution for Find Numbers with Even Number of Digits Given an array nums of integers, return how many of them contain an even number of digits.  Example 1 :- Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits).  345 contains 3 digits (odd number of digits).  2 contains 1 digit (odd number of digits).  6 contains 1 digit (odd number of digits).  7896 contains 4 digits (even number of digits).  Therefore only 12 and 7896 contain an even number of digits. Example 2 :- Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits. So lets see solution in Java :- Solution 1 :- import java.util.Scanner; public class FindNumberswithEvenNumberofDigits {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);         System...

Armstrong Number In Java | Programming Blog

Armstrong Number in Java With N number   Armstrong Number in Java With N number What is Armstrong Number? When any positive number is equal to the sum of its own digits raised to the power of the number of digits.  In simple, Armstrong number is the sum of power of all digits in given number. 0, 1, 153, 370, 371, 407, 1634, 8208, 9474, 54748 are some example of Armstrong number. Lets understand with example so you understand properly. Example :- 370 = (3*3*3) + (7*7*7) + (0*0*0) = 370 1634 = (1*1*1*1) + (6*6*6*6) + (3*3*3*3) + (4*4*4*4) Example 1 :- Armstrong number for only 3 digits import java.util.Scanner; public class ArmstrongExample {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);                 System.out.println("Enter Number");         int number = sc.ne...

Sort List of Object property in Java with check null values

Sort java List by Object property with null check using Comparator Example 1 :- Sorting a list without null check using Comparator inline anonymous class Programming.java public class Programming {     private int id;     private String language;     public Programming(int id, String language) {         this.id = id;         this.language = language;     }          public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getLanguage() {         return language;     }     public void setLanguage(String language) {         this.language = language;     } ...

Find and Print all duplicate characters in given string | Java example

Java program for find all duplicate characters in given String  Or Java program to display only repeated letters in a integer string Solution 1 :- import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class StreamApi {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);         System.out.println("Enter String");         String str = sc.nextLine();                 char[] charArray = str.toCharArray();                 Map<Character, Integer> map = new HashMap<>();         for (char c : charArray) {             if (!map.containsKey(c)) {  ...