Suppose we have a 2D matrix called matrix, we have to calculate the sum of the elements inside the rectangle defined by its upper left corner and lower right corner.So, if the input is like3014256321120154101710305So will be methods to find sum, update value, if we call them likesumRegion(2, 1, 4, 3)update(3, 2, 2)sumRegion(2, 1, 4, 3) ,then the output will be 8 and 10, as the above rectangle (with the green color) is defined by (2, 1) and (4, 3), which contains sum = 8.To solve this, we will follow these steps −Define one 2D array treeDefine one 2D array ... Read More
Suppose we have an image and that image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. Here the black pixels are connected, so there is only one black region. Pixels are connected horizontally and vertically. If we have a location (x, y) of one of the black pixels, we have to find the area of the smallest (axis-aligned) rectangle that encloses all black pixels.So, if the input is like001001100100and x = 0, y = 2, then the output will be 6To solve this, we will follow these steps −Define one ... Read More
This example demonstrates how to format date and time in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.Example Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.text.SimpleDateFormat import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" val textView: TextView = findViewById(R.id.textView) ... Read More
Suppose we have one binary tree and we have to serialize and deserialize them. As we know that the serialization is the process of converting a data structure or object into a sequence of bits so we can store them in a file or memory buffer, and that can be reconstructed later in the same or another computer environment.Here we have to devise an algorithm to serialize and deserialize binary tree. The binary tree is a rooted tree in which each node has no more than 2 children.So, if the input is likethen the output will be Serialize − 1 ... Read More
Suppose there is a group of two or more people and they wants to meet and minimize the total travel distance. We have a 2D grid of values 0 or 1, where each 1 mark the home of someone in the group. The distance is calculated using the formula of Manhattan Distance, so distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.So, if the input is like100010000000100then the output will be 6 as from the matrix we can understand that three people living at (0, 0), (0, 4), and (2, 2): The point (0, 2) is an ideal meeting ... Read More
This example demonstrates how to get the screen DPI programmatically in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.Example Step 3 − Add the following code to src/MainActivity.ktimport android.app.Activity import android.os.Bundle import android.util.DisplayMetrics import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var button: Button lateinit var textView: TextView var value = 0f lateinit var displayMetrics: DisplayMetrics var activity: Activity? ... Read More
Suppose we have a pattern and a string called str, we have to check whether str follows the same pattern or not. Here pattern follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.So, if the input is like pattern is "abaa", str is "orangegreenorangeorange", then the output will be trueTo solve this, we will follow these steps −Define a function solve(), this will take i, j, ptr, s, a map m, one set called used, if i >= size of s and j >= size of ptr, ... Read More
Suppose we have a binary search tree and a target value; we have to find k values in that BST that are closest to the target. We have to keep in mind that the target value is a floating-point number. We can assume k is always valid, and k ≤ total nodes.So, if the input is liketarget = 3.714286, and k = 2, then the output will be [4, 3]To solve this, we will follow these steps −Define a function pushSmaller(), this will take node, stack st, and target, while node is not present, do −if val of node < ... Read More
This example demonstrates how to detect if a user is using an Android tablet or a phone using Kotlin.Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.Example Step 3 − Add the following code to src/MainActivity.ktimport android.content.Context import android.os.Bundle import android.telephony.TelephonyManager import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" ... Read More
This example demonstrates how to change the font face of Webview in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.Example Step 3 − Create an assets folder. Right click asset folder >> Create a new file (webView.html) and the following code − WebView9 @font-face { font-family: 'Font'; src:url("file:///android_asset/Font.otf") } body { font-family: 'Font', serif; font-size: medium; text-align: justify; color:#ffffff } ... Read More