Getting less number of elements while printing set - set

In maximum length Subs-string question, I am getting correct output length but while printing set it gives less elements. any help ?

Related

Calculate the number of distinct possible values of the array after the repeated process of absolute difference between any 2 elements

This was a question asked in a mock test. So, I could not find any online evaluator for this.
Basically, you are provided with an array of elements. You can take any 2 elements and add their absolute difference back to the array.
The solution should be the number of distinct values in the array after infinite number of above given step.
Example 1-
Input [2,3,4,5]
Output - 5
Explanation: Since 3-2=1, this can be added to the set. So the total number of distinct values will be 5.
Example 2-
Input [1,100]
Output - 100
Explanation - 100-1 = 99 -> add this back to the set
Then, 99-1 = 98 -> add this back to the set.
After repeating the process, all the numbers from 1 to 100 will be present in the set.
I used a hash set to store the distinct elements to store the initial array elements and used 2 for loops to subtract the store the distinct result to the same array-
But only after submitting my code, i realized, the distinct result should be again used for further subtraction.
Hence my answer was wrong.
Can anyone help me solve this ? Thanks in advance.
Edit : correcting the solution of first example to 5 instead of 6.
Adding explanation for maximum(array)/gcd(array) as pointed out in the comments.
gcd(array) - the greatest common divisor for all the numbers in the array is calculated i.e. the largest number that divides all the values in the array. It is known that division is nothing but repeated subtractions. So, no matter how many times we were to find absolute difference of the numbers in the array, GCD of the numbers is the maximum we can reduce the difference to. For an instance, let's say the array is [2, 10], the array can only be become [2, 4, 6, 8, 10]. Other numbers in this range will never get added, that is, an absolute difference of 1 never occurs.
maximum(array) - we are finding difference here, so at any point, the difference cannot exceed the maximum value.
Therefore, maximum(array)/gcd(array) formula gives the right output for the question.
Hope this helps!

Divide binary array into three parts such that each part represent same decimal

Given a binary array, divide array into three parts such that each part represent same decimal.
Eg arr[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1}
Above array can be divided in the following way:
{1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1}. Now each part represent same decimal.
One simple approach would be to iterate starting from 1 and check each decimal can divide the array into three equal parts.
Is there any efficient algorithm for this.
Count the number of set bits. There's no solution if this number isn't divisible by three. Say it is, and that there are 3k set bits. Find the positions of the first, k+1, and 2k+1 set bits. Keep incrementing all three positions and comparing until you get to the last set bit or get a disagreement. If you get to the last set bit with all three agreeing then you have a solution.

Pseudo-code questions on setting values to a large minus or large positive number

In a program where you input 10 positive numbers and output the smallest number input, if I created a variable called "Small", why do I need to set that to a large positive number. In other programs, i've seen it being set up as a large minus number. Why is this?
I think what you mean by calling your variable 'small' is that you're gonna compare it to each input value and use it to store the smallest value then return it.
But if the default value of your variable 'small' is already smaller than any input value then you will return a wrong result.
You can either set it to an arbitrary large number or use the first value in your input set to avoid the problem.

Maximum number of equal elements in array

I was solving the problems from codeforces practice problem achieve.
I am not able to find efficient solution.
How to solve the following problem?
I can only think of a brute force solution
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
he chooses two elements of the array ai, aj (i ≠ j);
he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
Output
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
Sample test(s)
input
2
2 1
output
1
input
3
1 4 1
output
3
find the sum of all the elements.
If the sum%n==0 then n else n-1
EDIT: Explanations :
First of all it is very easy to spot that the answer is minimum n-1.It cannot be lesser .
Proof: Choose any number that you wish to make as your target.And suppose the last index n.Now you make a1=target by applying operation on a1 and an.Similarly on a2 and an and so on.So all numbers except the last one are equal to target.
Now we need to see that if sum%n==0 then all numbers are possible.Clearly you can choose your target as the mean of all the numbers here.You can apply operation by choosing a index with value less than mean and other with value greater than mean and make one of them (possibly both) equal to mean.

Design a divide and conquer algorithm that can determine the missing integer

Given an array, A, containing, in some order, all but one of the k-bit non-negative integers find the missing integer with the following constraint:
You are allowed to do only two things with one of these integers:
Obtain the value of its ith bit
Swap them with one another in the array
For example: if k is 3 the array will contain all but one the integers 0 through 7 inclusive, the task is to find the missing integer.
You should check that each bit (for example, if k = 3 you need to check that all 3 bits) are present 2^k-1 times as 0 & 1.
If you are sure of the invariant of the list that you outlined (only one number is missing and there are no duplicates) - sure that it always holds - then you could simply check only one, arbitrary bit holds the condition outlined above.
For example, say that the invariant holds, k = 3, and you have the list [0-7], excluding 6.
For each number in the list, get its first bit (least significant) and do the following:
if bit value = 0 then zeroValues++;
if bit value = 1 then oneValues++;
zeroValues should equal oneValues and both should equal 2^k-1, in this case 4.
Edit: Rereading your question, you're looking for the entire number. In order to find the whole integer value that you are looking for, just do the procedure for all bits. For each bit you do it, you will find either a 0 value or 1 value missing. The missing value is the bit value in the result integer. Doing this for all bits will find the entire bit representation of your integer.
Hint: Can you find out if the missing number is even or odd?
For a hint for a totally different approach, can you sort the array?

Resources