Suppose we have an array nums and a value k. We have to find number of consecutive subsequences whose sum is divisible by k.So, if the input is like k = 3 nums = [1, 2, 3, 4, 1], then the output will be 4 because the subsequences are [3], [1, 2], [1, 2, 3] and [2, 3, 4].To solve this, we will follow these steps −x := An array of size k and fill with 0x[0] := 1r:= 0, s:= 0for each elem in nums, dos :=(s + elem) mod kr := r + x[s]x[s] := x[s] + 1return ... Read More
Suppose we have an array nums. We have to find number of pairs (i, j) are there such that nums[i] = nums[j] but i is not same as j.So, if the input is like nums = [1, 3, 1, 3, 5], then the output will be 4, because the pairs are (0, 2), (2, 0), (1, 3) and (3, 1)To solve this, we will follow these steps −d := a new mapfor each c in nums, dod[c] := (d[c] + 1) when c is present in d otherwise 1res := 0for each c is in the list of elements (x ... Read More
Suppose we have n balls which are numbered by an array nums, whose size is n and nums[i] represents the number of ball i. Now we have another value k. In each turn we pick k balls from n different balls and find the difference of maximum and minimum values of k balls and store the difference in a table. Then put these k balls again into that pot and pick again until we have selected all possible selections. Finally find the sum of all differences from the table. If the answer is too large, then return result mod 10^9+7.So, ... Read More
In this tutorial, we are going write a program that multiplies the given two numbers using bitwise operators.The left shift () is used for the division.The multiplication of two numbers x, y can be written as x * y = (x * 2) * (y / 2) if y is even else it's equal to x * y = (x * y) * (y / 2) + x.So whenever the second number becomes odd, add the first number to the result. Let's see the steps to solve the problem.AlgorithmInitialise two numbers.Write a loop that iterates till the second number becomes ... Read More
Suppose we have two lists nums1 and nums2. Now the constraint is when we merge the order of elements in each list does not change, for example, if the elements are [1, 2, 3] and [4, 5, 6], then some valid merged lists are [1, 4, 2, 3, 5, 6] and [1, 2, 3, 4, 5, 6], there may be some other valid merge sequence. So if we have the size of lists N and M. We have to find number of ways we can merge them to get valid list. If the answer is too large the return result ... Read More
Suppose we have a string s. We have to find all possible combinations of letters of s. If there are two strings with same set of characters, then show the lexicographically smallest of them. And one constraint is each character in s are unique.So, if the input is like s = "pqr", then the output will be ['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']To solve this, we will follow these steps −st_arr := a new listfor i in range size of s - 1 to 0, decrease by 1, dofor j in range 0 to size of st_arr - 1, ... Read More
We can using left shift (
To get the result of n * 3.5 we need to calculate (n * 2) + n + (n / 2). Moving the bits to left by 1 will give you n * 2 and moving the bits to right by will you n / 2. Add those to get the result.n * 3.5 = (n * 2) + n + (n / 2)You can submit different values of n to verify the above equation. Let's see some examples.Input2 7 10Output7 24 35AlgorithmInitialise the number n.Find the n * 2 using left shift bitwise operatorFind the n / 2 using ... Read More
Given a number n, we need to find the count of multiples of 3 or 7 till n. Let's see an example.Input100Output43There are total of 43 multiples of 3 or 7 till 100.AlgorithmInitialise the number n.Initialise the count to 0.Write a loop that iterates from 3 to n.Increment the count if the current number is divisible by 3 or 7.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getMultiplesCount(int n) { int count = 0; for (int i = 3; i
Given a linked list, we have to move the last element to the front. Let's see an example.Input1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput5 -> 1 -> 2 -> 3 -> 4 -> NULLAlgorithmInitialise the linked list.Return if the linked list is empty or it has single node.Find the last node and second last nodes of the linked list.Make the last node as new head.Update the link of second last node.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; struct Node { int data; struct Node* next; }; void ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP