Find Maximum XOR Value of a Subarray of Size K in C++

sudhir sharma
Updated on 12-Mar-2021 06:38:36

547 Views

In this problem, we are given an array arr[] consisting of n elements and an integer k. Our task is to find the Maximum XOR value of a sub-array of size k.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 2 ,7, 9} k = 3Output12ExplanationAll subarray and the xor of all element of size k, {3, 1, 6} = 4 {1, 6, 2} = 5 {6, 2, 7} = 3 {2, 7, 9} = 12Solution ApproachA simple solution to the problem is by using two loops. One to iterate over the array and other to ... Read More

Find Maximum XOR of Given Integer in a Stream of Integers in C++

sudhir sharma
Updated on 12-Mar-2021 06:36:47

187 Views

In this problem, we are given Q queries each of which is one of the following type, Type 1 − insertion (1, i) to add the element with value i, in your data structure.Type 2 − findXOR (2, i), to find the XOR of all elements of the data structure with the element i.The data structure should contain only 1 element initially which will be 0.Let’s take an example to understand the problem, InputQueries: (1, 9), (1, 3), (1, 7), (2, 8), (1, 5), (2, 12)Output15 15ExplanationSolving each query, (1, 9) => data structure => {9} (1, 3) => data ... Read More

Find Maximum Value of Sum using Array Rotations in C++

sudhir sharma
Updated on 12-Mar-2021 06:32:48

178 Views

In this problem, we are given an array arr[] consisting of n elements. We need to Find maximum value of Sum( i*arr[i]) with only rotations on a given array allowed. For find the maximum sum of (i*arr[i]), we can perform any number of rotations.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 7, 2}Output43ExplanationWe will rotate the array once to get the maximum value, After rotation array will be {2, 4, 1, 3, 7}Sum = 0*2 + 1*4 + 2*1 + 3*3 + 4*7 = 0 + 4 + 2 + 9 + 28 = 43Solution ... Read More

Find Maximum Value of |i - j| - min(arr[i], arr[j]) in an Array in C++

sudhir sharma
Updated on 12-Mar-2021 06:30:15

475 Views

In this problem, we are given an array arr[] insisting of N integer values.Our task is to Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[].Problem description − we need to find the maximum product value of the minimum value of two elements and the absolute differences between their indexes. i.e. for two values i and j, we need to maximise, abs(i - j) * min(arr[i] , arr[j]).Inputarr[] = {5, 7, 3, 6, 4}Output16ExplanationThe maximum value is 16, between index 0 and 4 => abs(0 - 4)*min(arr[0], arr[4]) => 4*min(5, 4) => 4*4 = 16Solution ... Read More

Find Maximum Sum Taking Every Kth Element in the Array in C++

sudhir sharma
Updated on 12-Mar-2021 06:27:09

273 Views

In this problem, we are given an array arr[] and an integer k. Our task is to Find the maximum sum taking every Kth element in the array.Problem description: We need to find the maximum sum of the elements of the array such that they are k indexes apart. i.e. we need to maximize the sum, sum = arr[i] + arr[i+k] + arr[i + 2*k] + …. arr[i + p*k], such that (i + p*k) < nLet’s take an example to understand the problem, Inputarr[] = {5, 3, −1, 2, 4, −5, 6}, k = 4Output9ExplanationAll sums of every kth ... Read More

Find Maximum Sum Array of Length Less Than or Equal to M in C++

sudhir sharma
Updated on 12-Mar-2021 06:23:44

159 Views

In the problem, we are given n arrays of different length. Our task is to Find the maximum sum array of length less than or equal to m.We need to find sub-arrays from the arrays to maximise the sum and make the length of all subarrays combined equal to m.Let’s take an example to understand the problem, Inputn = 3, m = 4 arrOfArr[][] = {    {5, 2, -1, 4, -3}    {3, -2, 1, 6}    {-2, 0, 5} }Output20ExplanationSubArrays are {5, 4}, {6}, {5}, length = 2 + 1 + 1 = 4 Sum = 5 + ... Read More

Remove Duplicate Tuples from List of Tuples in Python

AmitDiwan
Updated on 12-Mar-2021 06:11:22

1K+ Views

When it is required to remove duplicate tuples from a list of tuples, the loop, the 'any' method and the enumerate method can be used.The 'any' method checks to see if any value in the iterable is True, i.e atleast one single value is True. If yes, it returns 'True', else returns 'False'A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The enumerate method adds a counter to the given iterable, and returns it.Below is a demonstration ... Read More

Initialize Tuples with Parameters in Python

AmitDiwan
Updated on 12-Mar-2021 06:10:45

478 Views

When it is required to initialize the tuples with certain parameters, the 'tuple' method and the '*' operator can be used.The 'tuple' method will convert the iterable passed to it as parameter into a tuple class type.The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.Below is a demonstration for the same −ExampleLive DemoN = 6 print("The value of N has been initialized to "+str(N)) default_val = 2 print("The default value has been initialized to " +str(default_val)) ... Read More

Convert Tuple to Integer in Python

AmitDiwan
Updated on 12-Mar-2021 06:09:30

2K+ Views

When it is required to convert a tuple into an integer, the lambda function and the 'reduce' function can be used.Anonymous function is a function which is defined without a name. The reduce function takes two parameters- a function and a sequence, where it applies the function to all the elements of the list/sequence. It is present in the 'functools' module.In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the ... Read More

Implement Shell Sort in Python

AmitDiwan
Updated on 12-Mar-2021 06:08:43

983 Views

When it is required to implement shell sort, a function is defined, and this takes a list and the length of the list as arguments. This list is sorted up to a specific number of elements, wherein the number of elements is the largest value. This is done until the number of elements has the smallest value.This is done for all sub-lists in the list, and all these sub-lists are sorted.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration of the same −ExampleLive ... Read More

Advertisements