Given an array of n integers your task is to print the sum of all of the integers import math as mt # Function to find the Sum of # bitwise AND of all subarrays. For 2nd element 5, the 1st element = 2 is the Given a positive integer N, the task is to check whether all the digits of the given integer N are the same or not. The second line You are given an array of n n integers, and your task is to find three values (at distinct positions) whose sum is x x. Virtual contest is a way to take part in past contest, as close as possible to participation on time. Also given are Q queries which have two numbers L and R. So the Idea is to use a similar approach to a sliding [Naive Approach] Using Two Nested Loops – O(n^2) Time and O(1) Space. Note: Unique Sub-array sum means no other sub-array will have the same sum value. Examples: Input: arr[] = {3, 2, 1, 4}, K = 2Output: 3Explanation:All subarrays havin Given an integer 'sum' (less than 10^8), the task is to find a pair of prime numbers whose sum is equal to the given 'sum' Out of all the possible pairs, the absolute difference between the chosen pair must be minimum. The same number may be chosen from candidates an unlimited number of times. ∑(amax-ai) ≤ K, for that given subarray. (Solve in Javascript) You are given an array A of N integers, representing the maximum heights of N skyscrapers to be built. ; Finally, copy all the elements from temporary array to arr[]. However, we don't need to use a Python set to store [1, len(A)+1], because we're starting with a dense set. You are given an array containing n positive integers. ; Return the number of different You are given an integer array 'ARR' of size 'N' and an integer 'S'. Print three integers: the positions of the values. For Example : If ‘ARR’ is {1,2,3,4} and ‘K’ = 4, then there exists 2 subsets with sum = 4. Examples: First line contains N which denotes number of elements in an array, and second line contains N space seperated integers. Note: We need to return the minimum possible sum as a string. By adding these optimal substructures, we can efficiently calculate the number of ways Time Complexity: O(N 3) Auxiliary Space: O(1) Efficient Approach: We can reduce the complexity from N^3 to N^2, using the below steps: Run two loops to find pairs (i, j) such that i < j and arr[j] > arr[i] and keep the count of these pairs as cnt. Given an array arr[] of N integers, the task is to find the count of pairs of adjacent elements whose sum is even where each element can belong to at the task is to print all pairs of array elements whose sum does not exist in the given array. The first input line has three integers n, a and b: the size of the array and the minimum and maximum subarray length. The output should only be one occurrence of a number irrespective of the number of occurrences in the input array. In other words, a twin prime is a prime that has a prime gap of two. Print one integer: the maximum subarray sum. Finally, there are q lines describing the queries. Follow the steps below to solve the problem: Insert all the array elements into MaxHeap. Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2 Examples: Input: arr[] = {8, 5, 4, 3, 15, 20}Output: 3, 1, 1, 12, 5I . {3, 4, 6} having a sum 13, which is not divisible by K = 3. Generate Bitonic Sequence of Given an array of non-negative integers of length N and an integer K. The idea is to sort the array and use two pointers at the beginning and end of the array. Examples: Input: arr[] = {1, 2, 3}, K = 3 Output: {1, 2} {3} Explanation: These are the combinations whose sum equals to 3. the sign of every element is opposite to that of its previous element) from the given array which has the maximum sum. Also see : Range Query With Update Required Examples: Input : arr[] = {1, 3, -4, 5, -2} Query 1: start = 0, end = 4 . If found to be true, then print Yes. Examples: Input: arr[] = {1, 5, 3, 2} Output: 2 Explanation: In the given array, there are two such triplets such that sum of the two numbers is equal to the third number, those are – Given an array arr[] of N integers, your task is to find for each array position (1-based indexing) the nearest position to its left having a smaller value. [L, R], the task is to print all the prime adam integers in this range. Examples: Input : arr[] = {2, 6, 9} R Given an array nums[] of N positive integers, the task is to find the number of subsequences that can be created from the array where each subsequence contains all integers from 1 to its size in any order. Auxiliary Space: O(1) [Expected Approach 1] Using minimum and maximum product ending at any index – O(n) Time and O(1) Space. Examples: Input: N = 222Output: Yes Input: N = 232Output: No Recommended: Please try your approach on {IDE} first, before movi Your task is to find all the money sums you can create using these coins. The total cost is the sum of such costs. You have to return the number of ways the target can be Print all possible combinations of two. Examples: Input: N = 10 Output: 3 Explanation: All possible integers which ar Question: You are given an array A consisting of N positive numbers. ; Exclude the current element from the subset: If the current element is excluded, the required sum remains the same. Working: Below is the working of how to rearrange an array in maximum minimum form using Two Pointer Technique: Complexity Analysis: Time Complexity: O(N), Iterating over the array of size N 2 times. Input: arr[] = {1, 5, 10, 7 min read. The next line contains n integers x_1,x_2,\ldots,x_n: the contents of the array. 1 \le n \le 2 \cdot 10^5 Combination Sum - Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. A recursive approach is used to solve this problem by considering two cases for each element in the array:. i. Example 1: Input: nums = [4,2,3] Output: true Explanation: Given an array of integers arr, your task is to count the number of contiguous subarrays that represent a sawtooth sequence of at least two elements. The first input line has two integers n and x: the size of the array and the target sum x. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. e the first value should be less than or equals to the second value. Follow Up: Try to do this problem in O(N) time complexity. Note that the first element is considered . If the sum of the elements at these pointers Time Complexity: O(N + M), where N is the size of the array and M is the number of operations Auxiliary Space: O(N) Applications of Prefix Sum: Equilibrium index of an array: The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. Stack Overflow. Examples: Input: arr[] = [6, 8, 4, 5, 2, 3, 0] Output: “604” Explanation: The minimum sum is formed by numbers 0358 and 246. Initially, each element in lis[] is set to 1, Given an array of n-positive elements. Examples: Input: arr[] = {2, 4, 2, 6} Output Time Complexity: O(n) Auxiliary Space: O(n) Approach 3: Using priority queues. Given an array, A of N integers, and you have also defined the new array B as a concatenation of array A for an infinite number of times. Using Bottom-Up DP (Tabulation) – O(sum*n) Time and Given an array of $$$n$$$ integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. . Take the sum of all the values after subtraction. The modified array should contain all sorted even-placed numbers followed by reverse sorted odd-placed numbers. For each test case, output "YES" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and "NO" otherwise. Exam . Example : [Naive Approach] Using Temporary Array – O(n) Time and O(n) Space. Examples: Input: arr[] = {1, 5, 10, 4, 7}, X = 4 Output: 6 6 is the smallest number greater than 4 which is not present in the array. The task is to print all possible solutions to the problem in such a way that there are no remaining items left whose weight is less than the remaining capacity of the knapsack. You can perform any number of operations, where each operation involves selecting a subarray of the array and replacing it with its sum. The second line has n integers x_1,x_2,\dots,x_n: the array values. Now, we will run a nested loop for j from i to n – 1 and add the value of the element at index j to a variable currentMax. Your task is to find two integers such that their greatest common divisor is as large as possible. Examples: Input: arr[]={55, 23, 32, 46, 88} Output: 46 55 101 Explanation: Pair {55, 46} will give the sum of 55 + 46 = 101 . These are {1,3} and {4 Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum MEX from all subarrays of length K. Optimal Substructure: Number of ways to make sum at index i, i. Constraints You are given an array of integers 'ARR' of length 'N' and an integer Target. With comprehensive lessons and practical exercises, this course will set Time Complexity: O(n) Auxiliary Space: O(n) Approach 3: Using priority queues. Print the resultant answer modulus 10^9+7. Lastly, for every subarray, we will Can you solve this real interview question? Maximum Subarray - Given an integer array nums, find the subarray with the largest sum, and return its sum. ; While in the above loop if there exists any element such arr[j] < arr[i] then increment the count of triplets by cnt as the current Given an array arr[] of N natural numbers. Note: Closest pair means the difference between the index of any two elements must be minimum. Auxiliary Time Complexity: O(N 3) Auxiliary Space: O(1) Efficient Approach: To optimize the above approach, the idea is to observe the following pattern after generating all the subarrays of odd length: For any element at index idx there Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Given an array arr[] of n integers and an integer target, the task is to find a pair in arr[] such that it’s sum is closest to target. Examples: Input : arr[] = { 3, 3, 9, 9, 5 } Given an array arr[] of N integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. Then, we iterate over this left segment and mark the occurrences of each number x by negating the value at index (x – 1). ; Pop the root of the MaxHeap and insert (popped element) / 2 into the MaxHeap You are given an array ‘ARR’ of ‘N’ integers and a target number, ‘TARGET’. Count all possible pairs in given Array with product K Given an integer [Naive Approach] Using Temporary Array – O(n) Time and O(n) Space. Which makes the array [4, 5, 6]. The task is to find the sum of all even occurring elements in the given array. Given integers N and K, the task is to construct an array arr[] of size N using numbers in the range [1, N] such that it has K sub-arrays whose all the elements are distinct. Given an array of integers of N elements. The number of values and queries. If the sum any combinations is not equal to X, print -1. Input Format: The first line of input contains an integer ‘T’ denoting We are given an array of n distinct numbers. Examples: Input : arr[] = {16, 8, 25, 2, 3, 10} Output : 25 Explanation: 25 is the largest number that is a perfect cube. e. Examples: Input: {2, 10, 10, 100, 2, 10, 11, 2, 11, 2} Output: {2, 10, 11} Input: {5, 40, 1, 40, 100000, 1, 5, 1} Output: {5, 40, 1} Note: Duplicate You are given an array of $$$n$$$ integers, and your task is to find two values (at distinct positions) whose sum is $$$x$$$. Whenever sum of elements between current start and end becomes greater than x, update the result if current length is Efficient Approach – O(n^4) Time and O(n^2) Space. Compute this sum and subtract the sum of all elements in the array from it to get the missing number. Given an array arr[] of size N and an integer K. The task is to count all possible pairs in the arr[] that are Twin Primes. The second Given an array arr[] of N integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pa Given an array arr[] of integers of size N, the task is to find the closest pair in the given array such that one element is the multiple of the other. Examples: Input: arr[] = {2, 3, 4, 6}, K = 3 Output: 1 Explanation: There is only one longest possible subarray of size 3 i. The task is to find the smallest element greater than X which is not present in the array. About; Products OverflowAI; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Approach: To obtain the minimum possible sum, the main idea for every operation is to reduce the maximum element in the array before each operation. Exampl . Examples: Input: arr[] = Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. Given an array of integers containing duplicate elements. consisting of N integers and an integer K, the task is to find the number of ways to split the array into a pair of subsets such that the difference between their sum is K. Note: Return the pair in sorted order and if there are multiple such pairs return the pair with maximum absolute difference. Then, we can simply iterate from left to right keeping track of the maximum running product ending at any index. Note that the first element is considered as even placed because of its index 0. The first one (pqmin) is of default order and the Second one (pqmax) is of reverse order. If Given a positive integer M and an array arr[] of size N and a few integers are missing in the array represented as -1, the task is to find the count of distinct arrays after replacing all -1 with the elements over the range [1, M] such that the absolute difference between any pair of adjacent elemen Time Complexity: O(n^2), where n is the size of array. The problem is to count the number of subset s of a given array arr[] such that the sum of the elements in each subset equals a specified target. The second line has Given an array of n n integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. Otherwise, print -1. The cost to connect two ropes is the sum of their lengths. We use prefix array to track the sum and check if the pair lies in the range lower bound Given an array of n integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. 4 min read. Submitted by Divyansh Jaipuriyar, on April 10, 2021 . Your task is to specify the actual heights of the skyscrapers, given that:- the height of the K-th skyscraper should be positive and not bigger than A(K];- no two skyscrapers should be of the same height;- the total sum of the skyscrapers' heights should be Given an array nums[] of N positive integers, the task is to find the number of subsequences that can be created from the array where each subsequence contains all integers from 1 to its size in any order. Examples: Input : a = 4, b = 4, N = 4 Output : 4 Input : a = 5, b = 13, N = 3 Output : 22 Basic Approach: Fo Problem: Given an array of n integers, your task is to process q queries of the form: what is the sum of values in range [a,b]? Input The first input line has two integers n and q . Using Top-Down DP (Memoization) – O(sum*n) Time and O(sum*n) Space. The task is to find the sum of the product of elements of all the possible subsets. Examples: Input: arr[] = {2, 4, 1, 3, 1} k = 2 Output: 5 Explanation: There are System. Example Given an array arr[] of N integers and an integer K. With comprehensive lessons and practical exercises, this course will set Given an array arr[] of N positive integers which denotes the cost of selling and buying a stock on each of the N days. [Better Approach] Sorting and Two Pointer technique – O(n*log n) Time and O(1) Space. Print the maximum greatest common divisor Given an array of n numbers and a number of queries are also given. **def countpairs(x,length,sum): count = 0 for The article outlines methods to find all unique quadruplets in an array that sum to a specified target value, utilizing approaches such as nested loops, hashing, and the two-pointer technique. Each line has two integers a and b: what is the xor sum of Time Complexity: O(n*n) Auxiliary Space: O(1) Better solution: A better solution will be using a prefix array i. The idea is similar to Kadane’s Algorithm with the only difference that here, we need to keep track of the start and end of the subarray with maximum sum, that is the result array. The input arrays have elements with small values and possibly many repetitions. Examples: Input: N = 2, K = 2 Output: 6 Operation 1: n = sum(n) = sum(2) = 1 + 2 = 3 Operation 2: n = sum(n) You are given an array of n integers, and your task is to find four values (at distinct positions) whose sum is x. The next line has n integers x1,x2,,xn: the values of the coins. Otherwise, print No. Follow the given steps to solve the problem: Initialize a Hash Map, say mp that stores all possible pairs with a given pair sum Pairs sum is used as key and an array of pairs as value. An efficient approach is to use summation formula. Sort an array when two halves are sorted Given You are given an array of n positive integers. The equilibrium index of an array is an index such that the sum of all elements at lower indexes equals the sum of all elements at higher indexes. Note: If there are multiple possible answers return any of them. After this, print all possible sums in increasing order. [Expected Approach-1] Using Sum of n terms Formula – O(n) time and O(1) auxiliary space. The task is to find the largest number which is a perfect cube. For arr = [9, 8, 7, 6, 5], the output should be countSawSubarrays(arr) = 4. Examples : Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make firs Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e. Find the total number of distinct pairs which can be formed by selecting one integer from 1 to a and other from 1 to b, such that their sum is divisible by N. Return the list of pairs sorted in non-decreasing order of Question: You are given an array A of N integers, representing the maximum heights of N skyscrapers to be built. Print the sum of that pair, if it exists. Print -1 if there is no number that is perfect cube. Pair (x,y) and Pair(y,x) are considered as the same pair. The task is to find out the GCD of all the numbers of the array excluding the numbers given in the range l, r (both inclusive) for each query. Note: When the index is at the start of the array, the left sum is 0, and when it’s at the end, the right sum is 0. You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1] by any positive numbers. A Twin prime are those numbers that are prime and have a difference of two ( 2 ) between the two prime numbers. Given an array of integers arr[] and a positive integer K, the task is to find the count of the longest possible subarrays with sum of its elements not divisible by K. Python3 # Python3 program to find Sum of # bitwise AND of all subarrays. Examples: Input The task is to find the maximum value of the sum of its subarray modulo m i. Longest subarray having maximum sum Run a loop for i from 0 to n – 1, where n is the size of the array. Your task is to check if there exists a subset in ‘ARR’ with a sum equal to ‘K’. def findAndSum(arr, n): # variable to store the final Sum Sum = 0 # multiplier mul = 1 for i in range(30): # variable to The task is to find the sum of all numbers in a given range from L to R. Otherwise, return false. The idea is to maintain a 1D array lis[], where lis[i] stores the length of the longest increasing subsequence that ends at index i. Given an array A[] consisting of N integers, the task is to check if the sum of numbers of digits in each array element is a prime number or not. Examples: Input: L = 5, R = 100 Output: 11 13 31 Explanation: The three numbers 11, 13, 31 ar . Example Naive Approach: The simplest approach is to try all possible combinations of given denominations such that in each combination, the sum of coins is equal to X. The first input line has Subarray Sum Equals K - Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. Your task is to return the list of all pairs of elements such that each sum of elements of each pair equals 'S'. Print four integers: the positions of the values. Approach: Follow the below Can you solve this real interview question? Non-decreasing Array - Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element. This can be implemented using MaxHeap. Print two integers: the positions of the values. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. You are given an array of n integers, and your task is to find three values (at distinct positions) whose sum is x. Given an array of n integers, your task is to process q queries of the following types: update the value at position k to u; what is the sum of values in range [a,b]? Input. Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. Constraints. Your task is to specify the actual heights of the skyscrapers, given that: - the height of the K-th skyscraper should be positive and not bigger than A(K]; - no two skyscrapers should be of the same height; - the total sum of the skyscrapers' heights should Given an unsorted array of positive integers, the task is to find the number of triangles that can be formed with three different array elements as three sides of triangles. arr[i] ≤ arr[i+1]) by using two types of operation by performing any numbers of time:. Examples: Input : arr[] = {3, 4, 5} Out Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1. Maximum absolute difference between distinct elements Include the current element in the subset: If the current element (arr[i]) is included in the subset, the new required sum becomes sum + arr[i]. The idea is to use two nested loops. If there exists no Given an array of n integers, your task is to count the number of subarrays having sum x. The idea is to first move all positive integers to the left side of the array. The idea is to use recursive approach which calculates the sum of an array by breaking it down into two cases: the base case, where if the array is empty the sum is 0; and the recursive case, where the sum is calculated by adding the first element to the sum of the remaining elements which is computed through a See more You are given an array arr [] of N integers, and your task is to find three values (at distinct positions) whose sum is X. Time Complexity: O(n^2) An efficient approach would be to use a map/hash function to keep a count of every occurring number and then traverse through the list. If two subsequences have different chosen indices, then they are considered different. Input : arr[] = {36, 64, 10, 16 . Constraints You are given an array arr[] of N integers, and your task is to find three values (at distinct positions) whose sum is X. Minimize the maximum subarray sum with 1s and -2s Time Complexity: O(sum*n), where sum is the ‘target sum’ and ‘n’ is the size of array. 0-1 knapsack queries Given an integer array W[] consisting of weights of the items and some queries consisting of capacity C of knapsack, Time Complexity: O(R – L) Auxiliary Space: O(1) Efficient Approach: The above approach can also be optimized by using the Prefix Sum. Examples: Input: N = 5, K = 8Output: {1, 2, 3, 3, 3}Explanat [Naive Approach] Using Two Nested Loops – O(n^2) Time and O(1) Space. If Given an integer array of n integers, find sum of bit differences in all pairs that can be formed from array elements. Given three integers a, b and N. Each query will be represented by two integers l, r. First print an integer k: the number of distinct money sums. Whenever sum of elements between current start and end becomes greater than x, update the result if current length is Given an array arr[] of size n, the task is to return an equilibrium index (if any) or-1 if no equilibrium index exists. Note: A number which is both prime, as well as adam, is known as a prime adam number. Number of Comparisons: The number of comparisons made to find the minimum and maximum elements is equal to the number of comparisons made during the sorting process. The first input line has two integers n and q . Given an array arr[] of rope lengths, connect all ropes into a single rope with the minimum total cost. We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2). Given an array of n integers, your task is to process q queries of the form: what is the xor sum of values in range [a,b]?. The MEX is the smallest positive integer that is not present in the array. Output: Corresponding to each test case, print the sum of array in a Given an array arr[] of N positive integers, your task is to count the number of subarrays having sum X. Create two Priority Queues. ]. Given an array arr[] of size N, consisting of positive and negative integers, the task is to find the longest alternating subsequence(i. The task is to find the maximum profit that can be earned by buying a stock on or selling all previously bought stocks on a particular day. Constraints You are given an array/list ‘ARR’ consisting of ‘N’ distinct integers arranged in ascending order. Input: arr[] = [5, 3, 0, 7, 4] Given an array of n integers, your task is to process q queries of the following types: update the value at position k to u; what is the sum of values in range [a,b]? Input. It may be assumed that the numbers in subsets are small and computing product doesn’t cause arithmetic overflow. e find the sum of each subarray mod m and print the maximum value of this modulo operation. The idea is to maintain a hash map to track how many times each element has occurred in the array so far. The next line has n integers a_1,a_2,\dots,a_n: the contents of the array. Two combinations are unique if the frequency of at least one of Given an array arr[] of N integers, the task is to find the product of all the pairs possible from the given array such as: (arr[i], arr[i]) is also considered as a valid pair. Count number of pairs of integers in the array that have the sum divisible by 4. The number is not a divisor of N. Time complexity: O(n log n), where n is the number of elements in the array, as we are using a sorting algorithm. If multiple such subsequences exist, then print the subsequence with the smallest length. Examples: Input : arr[] = {3, 4, 5} Out Given an array of digits (values are from 0 to 9), the task is to find the minimum possible sum of two numbers formed using all digits of the array. Output. You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers. Auxiliary Space: O(1) [Expected Approach] Using Kadane’s Algorithm – O(n) Time and O(1) Space. Auxiliary Space: O(sum*n) + O(n), the size of 2-D array and auxiliary stack space. ; Iterate through the array and insert all elements in both priority queues. The first input line has two integers n n and x x: the array size and the target Given an array of n integers, your task is to process q queries of the form: what is the sum of values in range [a,b]? Input. 1) First connect ropes of lengths 2 and 3. For example, bit difference for 2 and 7 is 2. Let’s assume that the input array has only positive elements. e their GCD is greater than 1. 13 min read. ; While in the above loop if there exists any element such arr[j] < arr[i] then increment the count of triplets by cnt as the current Given an array of n non-negative integers. Can anybody please tell me which test case I am missing ? Without Adjacent Given an array arr[] of N positive integers. Examples: Input: arr[] = {1, 2, 4, 3, 5} Output: You are given an array of n integers. Your task is to calculate for each window of k elements, from left to right, the minimum total cost of making all elements equal. Note: Each pair should be sorted i. Since all the elements are arranged in decreasing order, it won’t be possible to form any sawtooth subarray of length 3 or more. The task is to find all the unique combinations from the given array such that sum of the elements in each combination is equal to K. Note: The indices can be printed in any order. Examples: Input: arr[] = {1, 1, 2, 3}, K = 1Output: 3Explanation:Following splits into a You are given an integer array nums and an integer target. 10 min read. Given an array of distinct positive integers arr[] of length N, the task is to count all the triplets such that the sum of two elements equals the third element. Your task is to return all pairs of elements such that they add up to Target. That is the sum of all such elements whose frequency is even in the array. [Expected Approach] Sliding Window – O(n) Time and O(1) S pace. ; 2. , for every index ‘i’ of the array ‘arr[]’ create a prefix array to store the XOR of all the elements from the left end of the array ‘arr[]’ up to the i th element of ‘arr[]’ Creating a prefix array will take a time of O(N). Subtract each element of the subarray with the maximum. , count(i, sum, coins), depends on the optimal solutions of the subproblems count(i, sum-coins[i-1], coins) , and count(i+1, sum, coins). If the ‘sum’ cannot be represented as a sum of two prime numbers then print “Cann Given an array arr[] of n integers and an integer target, the task is to find a pair in arr[] such that it’s sum is closest to target. Examples: Input: N = 5, X = 7, arr[] = {2, 4, 1, 2, 7}Output: Given an array of n integers, your task is to process q queries of the form: what is the sum of values in range [a,b]? Input. Exclude the current element: The element at index i is not included Time Complexity: O(n^2) Auxiliary Space: O(1) Number of subarrays having sum in a given range using Merge Sort:. The first input line has an integer n: the size of the array. Note: 1. Binary representation of 2 is 010 and 7 is 111 ( first and last Given an array of N integers, the task is to print the sum of the first subarray by splitting the array into exactly three subarrays such that the sum of the first and third subarray elements are equal and the maximum. Note: Return true if there exists a subset with sum equal to ‘K’. Your task is to count all the distinct pairs in ‘ARR’ such that their sum is equal to ‘TARGET’. If no such pair exists, print "-1". Partition the given array into two subsets of length K and N - K so that the difference between the sum of both subsets is maximum. For a triangle to be possible from 3 values as sides, the sum of the two values (or sides) must always be greater than the third value (or third side). Initially, each element in lis[] is set to 1, Given an array arr having N integers, the task is to find a pair with maximum sum and having the same sum of digits. Find The article presents a recursive approach to count the number of subsets in an array that sum up to a given target. we will find the range from the small sorted arrays in the prefix array that lies in the range [lower, upper]. Using Bottom Up Tabulation – O(n^2) Time and O(n) Space. As we know that the sum of the first n natural numbers is given by the formula n * (n + 1) / 2. The tabulation approach for finding the Longest Increasing Subsequence (LIS) solves the problem iteratively in a bottom-up manner. Now you are given Q queries, each query consists of two integers L and R. The idea is simple, as we know that all the elements in subarray are positive so, If a subarray has sum greater than the given sum then there is no possibility that adding elements to the current subarray will be equal to the given sum. The first input line has an integer n: the number of coins. You are given an array of n integers, and your task is to find two values (at distinct positions) whose sum is x. Auxilary Space: is O(1), as we are not using any extra space. 2. Given an array arr[] of N integers, the task is to print the subsequence from the given array with the maximum pairwise absolute difference of adjacent elements. The first input line has two integers n and q: the number of values and queries. Therefore, total sum of count o Codeforces. Description: The problem has been featured in the interview/round of many top tech companies such as Amazon, Microsoft, Tesco, etc. ; You can choose any index from 1 to n-1 Given an array of n integers. Find Sublist with Contiguous Sum Equal to K Given a singly linked list, you need to determine if there exists a sublist within the list where the sum of elements in the sublist is Given an array of size n and integer k, count all pairs in array which differ in exactly K bits of binary representation of both the numbers. Examples: Input: arr[] = [4, 3, 2, 6] Output: 29 Explanation: We can connect the ropes in following ways. Examples: Input: A[] = {1, 11, 12}Output: YesExplanation: Count of digits of A[0], A[1] and A[2] are 1, 2, 2 respectively. Follow the steps below to solve the problem: Initialize an array, say prefix[] of size (N + 1) with all elements as 0s. ; Then, fill all the remaining positions in temporary array with 0. Maximum subarray sum modulo m Given an array of n elements and Given an array arr[] and an integer K. The task is to print the absolute difference of all of the pairwise consecutive elements. Given an array of n numbers, the task is to answer the following queries: maximumSubarraySum(start, end) : Find the maximum subarray sum in the range from array index 'start' to 'end'. Your task is to build an expression out of an array by adding one of the symbols '+' and '-' before each integer in an array, and then by concatenating all the integers, you want to achieve a target. Problem Statement:- Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K. The idea is to create a temporary array of same size as the input array arr[]. Examples: Input: N = 8, arr[] = {-1, 3, -2, 5, 3, -5, 2, 2}Output: 9Explanation: The subarray with maximum sum is {3, -2, 5, 3} with sum = 3 - 2 + 5 + 3 = 9. (arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs. 7 min read. The sub-array sum is defined as the sum of all elements of a particular sub-array, the task is to find the sum of all unique sub-array sum. 11 min read. Time Complexity: O(n*log(n)), for sorting the array Auxiliary Space: O(1) [Expected Approach] Using Hash Map – O(n) Time and O(n) Space. The second line has n integers x_1,x_2,\ldots,x_n: the contents of the array. Overlapping Subproblems: In this case, the subproblems overlap because the same subset sums are Time Complexity: O(N 3) Auxiliary Space: O(1) Efficient Approach: We can reduce the complexity from N^3 to N^2, using the below steps: Run two loops to find pairs (i, j) such that i < j and arr[j] > arr[i] and keep the count of these pairs as cnt. Print one integer: the required number of subarrays. Given two integers N and K, the task is to find the sum of first N natural numbers then update N as the previously calculated sum. Using Recursion – O(2^n) Time and O(n) Space. If A naive approach is to iterate through all pairs and calculate f(a[i], a[j]), and summing it up while traversing two nested loops will give us our answer. Repeat these steps K times and finally print the value of N. Time Complexity: O(X N) Auxiliary Space: O(N) Given an integer N, the task is to find the count of all possible integers less than N satisfying the following properties: The number is not coprime with N i. The task is to find a subsequence with maximum Skip to main content. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are Given an array of n positive integers, your task is to count the number of subarrays having sum x. Ex Given an array of n-positive elements. The first input line has two integers n and x: the array size and the target sum. Input: N = 6, arr[] = 5 min read. Note: We cannot use the element at a given index twice. For example, given the array A: [1, 4, 2, 6, 9 Given an array of N integers. Examples: Input: N = 4, X = 8, arr[] = {2, 7, 5, 1}Output: 1 3 4Explanation: The elements at position 1, 3 and 4 are: 2, 5 and 1 re Print n smallest elements from given array in their original order Given an array arr[] of size N and an integer X. T he above approach can also be optimized by using the idea of generating all possible pairs of the given array. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4, We are given an array of n distinct numbers. Input: arr[]={18, 19, 23, 15} Output: -1 . Your task is to divide the array into k subarrays so that the maximum sum in a subarray is as small as possible. println(findAndSum(arr, n));}} // This code is contributed // by Code_Mech. The value of every subarray is defined as: Take the maximum from that subarray. A subarray is a contiguous non-empty sequence of elements within an array. You can increase or decrease each element with cost x where x is the difference between the new and the original value. Given an array arr[] consisting of N positive integers and an integer K, the task is to find the minimum possible array sum that can be obtained by repeatedly selecting a pair from the given array and divide one of the elements by 2 and multiply the other element by 2, at most K times. From these combinations, choose the one having the minimum number of coins and print it. The task is to divide the array into K parts ( subarray ) such that the sum of the values of all subarray is minimum. If there exists such subarray, then print the starting and ending index of that Given an array arr[] of size N, the task is to count the number of Given an array nums[] of N positive integers, the task is to find the number of subsequences that can be created from the array where each subsequence contains all integers from 1 to its size in any order. You may return the combinations in any order. It is supported only ICPC mode for virtual contests. For example, if the given array A is [1,2,3] then, infinite array B is [1,2,3,1,2,3,1,2,3,. Smallest greater elements in whole array An array is given of n length, Given an array arr[] of n integers and an integer target, the task is to find a pair in arr[] such that it’s sum is closest to target. The task is to find all elements that have more than one occurrences. Given an array of n numbers and a number of queries are also given. The outer loop picks a starting element, the inner loop considers all elements (on right side of current start) as ending element. Auxiliary Space: O(n^2), as in the worst case we can have (n * (n – 1))/2 pairs in the result. Examples: Input: N = 8, arr[] = {2, 5, 1, 4, 8, 3, 2, 5} Output: 0 1 0 3 4 3 3 7 Explanation: For 1st element 2, there is no smaller element on the left, therefore print 0. For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1". The task is to choose exactly K queries out of Q queries such that the sum of the array at the Time Complexity: O(n^3), where n is size of arr[]. The first input line contains two integers n and k: the size of the array and the number of subarrays in the division. Given an array of n integers, your task is to find the maximum sum of values in a contiguous subarray with length between a and b. The first input line has two integers n and q: the number of values Given an array of n integers, your task is to process q queries of the following types: update the value at position k to u; what is the sum of values in range [a,b]? Input. The task is to sort all even-placed numbers in increasing and odd-placed numbers in decreasing order. Example Given an array of n integers, your task is to count the number of subarrays where the sum of values is divisible by n. Your task is to perform these operations optimally to makethe resultant array non-decreasing. The second line has n integers a_1,a_2,\dots,a_n: the array values. If no such pair exists then print -1. Note: All the elements must belong to a subarray and the subarrays can also be em . We can use an array instead, which will replace set hashing by list indexing Using Bottom Up Tabulation – O(n^2) Time and O(n) Space. Lastly, iterate over the left segment again and find the missing number by searching for the That's a good O(N) solution since set operations are O(1). ; Find if there is a subarray with 0 sums: Given an Time complexity: O(n 2), as we are iterating over all possible subarrays. 15+ min read. Constraints Problem Statement: Given on Hackrearth. We will use Merge sort and prefix-sum to solve this problem. Traverse all the elements and for each element arr[i], check if the complement (target – arr[i]) already exists in the map, Given a sorted array arr[] of size N, the task is to find the length of the longest subarray and print the subarray such that the sum of the differences of the maximum element of the chosen subarray with all other elements of that same subarray is ≤ K. 1. Programming competitions and contests, programming community. Given an array of $$$n$$$ integers, your task is to find for each array position the nearest position to its left having a smaller value. You are also given an integer ‘TARGET’. out. Examples: Inp [Alternate Approach] By Negating Array Elements – O(n) Time and O(1) Space. Also, compute the maximum profit. Examples: Input: arr[] = {2, 3, 5} Output Given an array of n integers, your task is to process q queries of the form: what is the sum of values in range [a,b]? Input. For every query, you can increase all the elements of the array in the index range [L, R] by 1. ; Traverse the array, arr[] using the variable i and update prefix[i] to sum of prefix[i – 1] and arr[i – 1]. While traversing through the list, we multiply the Perfect Sum Problem: Given an array of integers and a sum, the task is to count all subsets of the given array with the sum equal to the given sum. First, copy all non-zero elements from arr[] to the temporary array. Examples The task is to check if there exists any subarray whose sum is a multiple of N. Input. Examples: Input : arr[] = {2, 6, 9} R You are given an array/list ‘ARR’ of ‘N’ positive integers and an integer ‘K’. gycobdh del uwnqt lcpvl ahio vamrq cau ybeowh mdpqgbv ksahr