This example demonstrates how to listen for volume buttons in Android background service 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. Step 3 − Add the following code to src/MainActivity.ktpackage app.com.kotlinapp import android.os.Bundle import android.os.Handler import android.provider.Settings import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var settingsContentObserver: SettingsContentObserver override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) settingsContentObserver ... Read More
This example demonstrates how to play sound using SoundPool in 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. Step 3 − Create a new Android Resource folder (raw) and copy-paste your sound file in the res/raw/filename.mp3 as shown below −Step 4 − Add the following code to src/MainActivity.ktpackage app.com.myapplication import android.media.AudioManager import android.media.SoundPool import android.os.Bundle import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @Suppress("DEPRECATION") class MainActivity : AppCompatActivity() { private var ... Read More
In this problem, we are given an array of n elements. Our task is to print xor of all prime numbers of the array.Let’s take an example to understand the problem,Input − {2, 6, 8, 9, 11}Output −To solve this problem, we will find all the prime numbers of the array and the xor them to find the result. To check if the element is prime or not, we will use sieve’s algorithm and then xor all elements that are prime.ExampleProgram to show the implementation of our solution, Live Demo#include
In this problem, we are given an array of n elements. Our task is to print XOR of XORs all possible subarrays (taken in order) created from elements of the array.Let’s take an example to understand the problem, Input − array = {1, 3, 6, 8}Output − 0Explanation −(1) ^ (3) ^ (6) ^ (8) ^ (1^3) ^ (3^6)^ (6^8) ^ (1^3^6) ^ (3^6^8) ^ (1^3^6^8)To solve this problem, a simple solution could be iterating over all the subarray and find xors. But this is an inefficient approach. A better approach could be counting the frequency of each element of ... Read More
In this problem, we are given two integer L and R denoting a range. Our task is to find xor of all elements within the range [L, R].Let’s take an example to understand the problem, Input − L=3, R = 6Explanation − 3^4^5^6 =To solve this problem, we will find the MSB of R. the MSB of the answer will not be greater than R. Now, we will find the parity of count of the number of bits from 0 to MSB.Now, to find the parity count for an ith bit, we can see that the state of an ith ... Read More
In this problem, we are given an n tree and there are some queries that are nodes of the tree. Our task is to print the XOR of all nodes of the sub-tree formed by the given node.Let’s take an example to understand the problem, Queries − {1, 6, 5}Output −0 0 5Explanation −1^6^3^2^4^7^5 6^2^4 5To solve this problem, we will compute the xor of all nodes of the sub-tree by traversing the tree once and store it. Now, we will compute the xor of all nodes of the sub-tree if the child nodes and then computing for all given ... Read More
In this problem, we are given an array of n elements and there are some queries which are range from a start point to endpoint in the array. Our task is two finds the XOR of elements that appeared even a number of times in the range.Let’s take an example to understand the problem, Input −array = {1, 2, 3, 1, 1, 2, 2, 3} querries = 2 R = 4 L = 2, R = 5 L = 2, R = 7Output −0 1 0The solution to this problem is quite easy, we will find the sum of XOR ... Read More
In this problem, we are given a binary tree and two nodes of the binary tree. Our task is to print the XOR of all nodes that come in the path between the two nodes.Let’s take an example to understand the problem, We need to find the xor of all nodes between 2 and 3.The path from 2 to 3, 2 → 6 → 1 → 3.We will find 2^3^1^3.Output −To solve this problem, we need to find the paths from one to another node. For this, we will find the XOR of all nodes in the path from the ... Read More
In this problem, we are given a string of characters, our task is to print the XOR of frequencies of characters of the string whose frequency of occurrence is a prime number.Let’s take an example to understand the problem, Input − TutorialsPointOutput −Here, we will check the frequency of occurrence of each character of the string and then find XOR of all the characters whose frequency is a prime number. For this will create an array of prime frequencies. Then we will store frequencies of characters of a string in a map and then matching with prime frequency array. If ... Read More
In this problem, we are given an array of n elements. Our task is to generate a sequence of size n*n whose elements are the sum of a pair of all elements of A with itself. And print the xor elements of this sum array formed.Let’s take an example to understand the problem, Input − A (1, 4, 5)Output − 0Explanation −B (1+1, 1+4, 1+5, 4+1, 4+4, 4+5, 5+1, 5+4, 5+5) B(2, 5, 6, 5, 8, 9, 6, 9, 10) Xor of all values = 2^5^6^5^8^9^6^9^10 = 0.To solve this problem, we need to know some properties of Xor. The ... Read More