Techno Blender
Digitally Yours.
Browsing Tag

Subarray

Program to reverse a Subarray in a given Array.

Given an array Arr of N integers, the task is to reverse a subarray of that array. The range of this subarray is given by L and R.Examples:Input: arr = , L = 1, R = 5Output: Input: arr = , L = 0, R = 2Output: Approach: Follow the steps below to solve the problem:If the size of the sub-list is 1, then return the array.If the size of the sub list is 2, then swap if the range lies between them.Else, Start swapping from L and R up to the mid of (L + R).Below is the implementation of the above approach:Python3  def…

Minimum cost to make parity of elements same by removing Subarray

Given an arr of length N, the task is to make parity of arr the same by using the below-provided operation: Select a subarray containing… Read More The post Minimum cost to make parity of elements same by removing Subarray appeared first on GeeksforGeeks. Given an arr of length N, the task is to make parity of arr the same by using the below-provided operation: Select a subarray containing… Read More The post Minimum cost to make parity of elements same by removing Subarray appeared first on GeeksforGeeks. FOLLOW US…

Count Subarray of size K in given Array with given LCM

Given an array arr of length N, the task is to find the number of subarrays where the least common multiple (LCM) of the subarray is equal to K and the size of that subarray is S.Examples: Input: arr = {1, 2, 3, 4, 5, 6}, K = 6, S = 2Output: 1Explanation: {1, 2, 3 }, {2, 3}, {6}There are 3 subarrays that can be generated from the main array with each having its LCM as 6. Out of which only {2, 3} is the length of 2. Input: arr = {3, 6, 2, 8, 4}, K = 6, S = 2Output: 2Explanation: {3, 6}, {6, 2}There are only 2 subarrays…

Count Subarrays with product of sum and subarray length less than K

Given an array of positive elements arr of length N, the task is to count all the subarrays such that the product of the subarray… Read More The post Count Subarrays with product of sum and subarray length less than K appeared first on GeeksforGeeks. Given an array of positive elements arr of length N, the task is to count all the subarrays such that the product of the subarray… Read More The post Count Subarrays with product of sum and subarray length less than K appeared first on GeeksforGeeks. FOLLOW US ON GOOGLE…

Longest Subarray whose bitwise AND of every pair of elements is 0

Improve Article Save Article Like Article Improve Article Save Article Given a positive integer array arr of size N, the task is to find the longest subarray such that the bitwise AND of every pair of elements in the subarray is equal to 0.Examples: Input: arr = {1, 3, 8, 48, 10}Output: 3Explanation: The longest valid subarray is {3, 8, 48}, So, the length of a valid subarray is 3=> 3 AND 8 = 0.=> 3 AND 48 = 0.=> 8 AND 48 = 0.Input: arr = {3, 1, 5, 11, 13}Output: 1An approach using Bit Manipulation:The…

Maximize point to reduce Array by replacing Subarray with its sum

Given an array arr of size, N, the task is to maximize the score to reduce the array to a single element by replacing any… Read More The post Maximize point to reduce Array by replacing Subarray with its sum appeared first on GeeksforGeeks. Given an array arr of size, N, the task is to maximize the score to reduce the array to a single element by replacing any… Read More The post Maximize point to reduce Array by replacing Subarray with its sum appeared first on GeeksforGeeks. FOLLOW US ON GOOGLE NEWS Read…

Maximize non decreasing Array size by replacing Subarray with sum

Given an array A of size N. In one operation only one subarray can be selected and replaced with the sum of the subarray. The task is to find the maximum size of the array after making it non-decreasing.Examples:Input: N = 5, A = {5, 1, 6, 6, 6}Output: 4Explanation: maximum size non-decreasing array, in this case, is {6, 6, 6, 6} which is obtained by replacing subarray(0, 1) = A + A = 6Input: N = 9, A = {5, 1, 6, 7, 7, 1, 6, 4, 5 }Output: 6Explanation: maximum size non-decreasing array, in this case, is {5, 7, 7, 7, 7, 9}…

Check if Binary Array can be split into X Subarray with same bitwise XOR

  import java.io.*;import java.util.*;  public class GFG {                  public static String check(int arr, int n, int x)    {        int xor = 0;        int count0 = 0, count1 = 0;          for (int i = 0; i < n; i++) {            xor ^= arr;            if (xor == 0)                count0++;        }        if (count0 >= x && xor != 1) {            return "Yes";        }        xor = 0;        for (int i = 0; i < n; i++) {            xor ^= arr;            if (xor == 1) {                count1++;…

Maximum difference between sum of even and odd indexed elements of a Subarray

  #include <bits/stdc++.h>using namespace std;  int pre(vector<int>& arr, int i, int j){    if (i == 0)        return arr;    return arr - arr;}  int maximumDiff(vector<int>& nums){    int n = nums.size(), val = 0, sum1, sum2;    vector<int> odd(n, 0), even(n, 0);    even = nums;          for (int i = 1; i < n; i++) {        if (i % 2 == 0)            even = nums;        else            odd = nums;        odd += odd;        even += even;    }              for (int i = 0; i < n; i++) {…

Maximum Subarray sum of A[] by adding any element of B[] at any end

Given two arrays A and B having lengths N and M respectively, Where A and B can contain both positive and negative elements, the task… Read More The post Maximum Subarray sum of A by adding any element of B at any end appeared first on GeeksforGeeks. Given two arrays A and B having lengths N and M respectively, Where A and B can contain both positive and negative elements, the task… Read More The post Maximum Subarray sum of A by adding any element of B at any end appeared first on GeeksforGeeks. FOLLOW US ON GOOGLE…