Find Consecutive Subsequences Whose Sum is Divisible by K in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:41:10

849 Views

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

Find Number of Pairs with Same Elements in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:37:43

312 Views

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

Sum of Differences Between Max and Min Elements from Randomly Selected K Balls in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:35:10

173 Views

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

Multiply Any Number Using Bitwise Operator in C++

Hafeezul Kareem
Updated on 25-Oct-2021 06:31:30

11K+ Views

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

Merge Two Lists Without Changing Order in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:30:58

217 Views

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

Find All Possible Combinations of Letters in a String in Python

Arnab Chakraborty
Updated on 25-Oct-2021 06:27:49

7K+ Views

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

Multiply a Given Integer with 3.5 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:48:06

413 Views

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

Multiples of 3 or 7 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:40:28

1K+ Views

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

Move Last Element to Front of a Given Linked List in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:21:53

878 Views

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

Advertisements