Posts

Showing posts from April, 2021

Count of Smaller Numbers After Self

You are given an integer array  nums  and you have to return a new  counts  array. The  counts  array has the property where  counts[i]  is the number of smaller elements to the right of  nums[i] . Input: nums = [5,2,6,1] Output: [2,1,1,0] Explanation: To the right of 5 there are 2 smaller elements (2 and 1). To the right of 2 there is only 1 smaller element (1). To the right of 6 there is 1 smaller element (1). To the right of 1 there is 0 smaller element. Solution :  class Solution { public List<Integer> countSmaller ( int [] nums) { List<Integer> intList = new ArrayList<Integer>(nums. length ) ; int i , j ; int n = nums. length ; int low[] = new int [n] ; // initialize all the counts in countSmaller array as 0 for (i = 0 ; i < n ; i++) low[i] = 0 ; for (i = 0 ; i < n ; i++) { for (j = i + 1 ; j < n ; j++) { ...

Remove duplicate characters from a string using LinkedHashSet

Given a string  s , remove duplicate letters so that every letter appears once and only once. You must make sure your result is  the smallest in lexicographical order  among all possible results. Input: s = "bcabc" Output: "abc" Convert the string to an array of char, and store it in a  LinkedHashSet . This will preserve your ordering, and remove duplicates. Something like: class Solution { public String removeDuplicateLetters(String s) { String string = "bcabc"; char[] chars = string.toCharArray(); Arrays.sort(chars); Set<Character> charSet = new LinkedHashSet<Character>(); for (char c : chars) { charSet.add(c); } StringBuilder sb = new StringBuilder(); for (Character character : charSet) { sb.append(character); } System.out.println(sb.toString()); return sb.toString(); } }

Merge 2 Arrays

How to Merge Two Arrays in Java There are following ways to merge two arrays: Java  arraycopy()  method Without using  arraycopy()  method Java  Collections Java arraycopy() method public   static   void  arraycopy(Object source,  int  source_position,    Object destination,  int  destination_position,  int  length)   Parameters source : It is a source array. source_position : Starting point in the source array. destination : It is a destination array. destination_position : Starting position in the destination array. length : The number of array elements to be copied import  java.util.Arrays;   public   class  MergeArrayExample1  {   public   static   void  main(String[] args)   {   int [] firstArray = { 23 , 45 , 12 , 78 , 4 , 90 , 1 };         //so...

Location

PERMISIONS : ACCESS_COARSE_LOCATION - Network  ACCESS_FINE_LOCATION - GPS ACCESS_BACKGROUND_LOCATION FusedLocationProviderClient - Main entry point for accessing location for the app and interacts with google play services behind the scenes GooglePlayServices 11.6.0 and above for 8.0(Oreo) and Above LocationRequest - contains quality of service parameters for requests to FusedLocationProviderClient  Location Callback - onLocationAvailability, onLocationResult onSuccesListener and onFailureListener with onSuccess and onFailure callback Location - gives lat and long Dependancy(App) : com.google.android.gms:play-services-location:17.0.0 Classpath :  com.google.gms:google-services locationProviderClient = LocationServices.getFusedLocationProviderClient(this) locationRequest =  LocationRequest.create() locationRequest.setPriority(LocationRequest.Priority_High_Accuracy) locationRequest.setInterval(5000); locationCallback = new LocationCallback(){ onLocationAvailabi...

Foreground and Background services

Foreground : The process relies on onPause() and onResume()...i.e you play music player and pressing pause and play Background : The process which runs without user interaction i.e receiving a message, incoming call, receiving mails, or setting alarms. The method used here is onStart() and onStop(). Foreground Service  is used when User is interaction with application and when  Service  is doing something visible to user.  Background Service   is used when even user close application (discard from recents) and when  Service  is doing something not visible to user like downloading data from server, load data from a  ContentProvider  etc.. And  Foreground Service  is less likely to be killed by system on low memory.

Kotlin and Java difference

  No need of findViewByIds:  It is used to find the first descendant view with the given ID. Java TextView text = (TextView) findViewById(R.id.textView); text.setText("Hello World"); Kotlin textView.setText("Hello World") Free from Null Pointer Exception NullPointerExceptions  are a tremendous source of disappointment for Java designers. In Kotlin, all sorts are non-nullable (incapable to hold null value) by default. If the code tries to use or return null in Kotlin, compile time error is shown. var a: String = "abc" // compilation error a = null Here, are differences between Kotlin vs Java Kotlin Java Kotlin allows users to create an extension function. Java doesn't offer any extension functions. Kotlin doesn't require too much work for data classes. Java developers write and construct a lot of elements to develop classes Kotlin doesn't offer implicit conversions. Java supports implicit conversions. There are no null variables or objects in Ko...

Coroutines

Coroutines are  lightweight threads &  creating coroutines doesn’t allocate new threads .   They use predefined thread pools.  It will enable writing asynchronous code in a synchronous way and  makes the multitasking very easy.  coroutines can run in parallel, wait for each other and communicate. function  fetchUser  will look like below: suspend fun fetchUser () : User { return GlobalScope.async(Dispatchers.IO) { // make network call // return user }.await() } fetchAndShowUser like below: suspend fun fetchAndShowUser () { val user = fetchUser() // fetch on IO thread showUser(user) // back on UI thread } And the showUser function : fun showUser (user: User) { // show user } Dispatchers : Dispatchers help coroutines in deciding the thread on which the work has to be done.  There are majorly three types of Dispatchers which are as  IO, Default, and Main .  IO dispatcher is used to do th...