Found 7197 Articles for C++

Find set of m-elements with difference of any two elements is divisible by k in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:46:30

233 Views

Suppose we have an array with N positive integers, and another variable K. We have to find the exactly m-elements, such that difference between any two elements is equal to k. So if the array is A = [4, 7, 10, 6, 9], and k = 3 and m = 3, then output will be “yes”. As we can find three elements like 4, 7, 10.To solve this, we have to keep track of the remainders, when an element is divided by k. Now create a multi-dimensional array rem[][] of size k, its index is showing the remainder, and elements ... Read More

Find missing number in another array which is shuffled copy in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:43:43

325 Views

Suppose, we have two arrays A and B, the array A has n elements. The second array B has all the elements of A, but they are shuffled and one element is removed. We have to find the missing elements. So if A = [4, 8, 1, 3, 7], and B = [7, 4, 3, 1], the output is 8.This can be solved using XOR trick. The combined occurrence of each element is twice, one in A, and other in B, except one element which only has a single occurrence in A. As we know that x XOR x = ... Read More

Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:40:12

272 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as possible number of times. We have to find the repeating numbers without taking any extra space. If the value of n = 7, and list is like [5, 2, 3, 5, 1, 6, 2, 3, 4, 5]. The answer will be 5, 2, 3.To solve this, we have to follow these steps −for each element e in the list, do the following steps −sign := A[absolute value of e]if the sign is positive, then make it negativeOtherwise it is a repetition.Example Live ... Read More

Find minimum value to assign all array elements so that array product becomes greater in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:38:18

216 Views

Suppose we have an array of n elements. Update all elements of the given array to some min value x, such that arr[i] = x. Such that product of all elements in the new array is strictly greater than the product of all elements of the initial array, where i

Find duplicates in a given array when elements are not limited to a range in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:36:54

1K+ Views

Suppose we have an array of N integers. Here we will print the duplicates of the given array. If no such duplicates are present, then return -1. So if the array is like [12, 15, 12, 3, 6, 12, 3, 48, 56, 8, 48], then duplicates are: [12, 3, 48]Here we will use the unordered map in C++. So at first when one element is taken, check whether that is present in the map or not, if this is present, then simply print that as duplicate, otherwise just add that into map.Example Live Demo#include #include using namespace std; void displayDuplicates(int arr[], ... Read More

Find minimum sum such that one of every three consecutive elements is taken in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:35:48

375 Views

Suppose we have an array of n elements. The task is to find the minimum sum of elements from the array. Such that at least one element one element is picked out of every three consecutive elements in that array. So if the array is like [1, 2, 3, 6, 7, 1]. The output is 4. So if we pick 3 and 1, then (3 + 1 = 4). So there are some subarrays of consecutive elements like [1, 2, 3], [2, 3, 6], [3, 6, 7], [6, 7, 1], we have picked one element from every subarray.Consider sum(i) will ... Read More

Find count of Almost Prime numbers from 1 to N in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:34:28

393 Views

Suppose we have a number N. We have to find almost prime numbers in range 1 to N. A number is called almost prime when it has exactly two distinct factors. The numbers can have any number of non-prime factors, but should be two prime factors. So if N is 2, then output will be 2. There are two numbers 6 and 10.Here we will use the Sieve of Eratosthenes approach. Please check the following implementation to get better idea.Example Live Demo#include #define N 100005 using namespace std; bool prime[N]; void SieveOfEratosthenes() {    for(int i = 0; i

Find minimum shift for longest common prefix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:32:58

522 Views

Consider we have two strings A and B. The length of A and B are same. In a single shift we can rotate the string B one element. We have to find minimum required shift to get common prefix of maximum length from A and B. So if A = “computerprogramming”, and B = “programminglanguage” So the minimum shift is 8, and prefix is “programming”.Suppose we add the string B at the end of B, so B = B + B, then there is no-need to find prefix of each shift separately. So we have to find the longest prefix ... Read More

Find all elements in array which have at-least two greater elements in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:30:50

358 Views

Suppose, we have an array of n numbers. We have to find all elements in array, which have at least two greater elements. If the array is like A = [2, 8, 7, 1, 5], then the result will be [2, 1, 5]To solve this, we will find second max element, then print all elements which is less than or equal to second max value.Example#include using namespace std; void searchElements(int arr[], int n) {    int first_max = INT_MIN, second_max = INT_MIN;    for (int i = 0; i < n; i++) {       if (arr[i] > first_max) ... Read More

Find minimum cost to buy all books in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:28:22

279 Views

Suppose we have an array of n elements. These are the ratings of them. Find the minimum cost to buy all books, with the following condition −Cost of each book would be at-least 1 dollarA book has higher cost than an adjacent (left or right) if rating is more than the adjacent.So for example, if the rating array is like [1, 3, 4, 3, 7, 1], Then the output is 10, As 1 + 2 + 3 + 1 + 2 + 1 = 10To solve this, we have to make two arrays called LtoR, and RtoL, and fill them ... Read More

Advertisements