Suppose we have a circular Queue. Moreover, let us also say it is array based. We would have a front index, rear index, and a size. However, when given the following equation to find rear index, I get rather confused:
rear_index = (rear_index+1) % arraySize;
Can someone please explain to me EXACTLY what is happening? How can this work for enqueueing?
Read up on Modulo operation. Operator % returns the remainder after division.
E.g. 10 % 7 == 3 (7 divides into 10 once with 3 left over), 10 % 3 == 1 (3 divides into 10 3 times remainder 1) etc.
If size == 10 and rear_index == 9 (the last element in the array), rear_index = (rear_index + 1) % 10; assigns rear_index = (9 + 1) % 10; // == 0. rear_index has "looped around" from 9 to 0, the next increment will be to 1, then 2, then...9,0,1,2...
When implementing a queue using a Circular buffer (often also referred to as "ring buffer"), both head and tail indexes are incremented this way and code enforces rules similar to the following (comment from some old code of my own)...
/* _head (presumably front_index) => index of oldest entry (next to be returned)
* _tail (presumably rear_index) => index of next entry to be inserted
* _head == _tail && queue[tail] != null => queue full
* _head == _tail && queue[tail] == null => queue empty
Related
I AM TRYING TO FIND THE ERROR
The code is supposed to find out if a positive integer entered by a user is exactly divisible by the number 3.
n = userinput
WHILE n ≥ 0
n = n - 3
ENDWHILE
You're using greater than OR EQUAL TO so you won't break out of the loop on n = 0, only n = -3 which then triggers your ELSE statement. The EQUAL TO aspect takes you a step too far.
Answering the comment:
Use > instead of >=. Basically the code as written will never allow n to equal 0 at the time the condition is evaluated. Trace each step of the loop using a number like 3.
N = 3
//first pass
WHILE (3 >= 0) // true
n = 3-3 //n now 0
//second pass
WHILE (0 >= 0) //True, 0 is equal to 0
n = 0-3 //n now -3
//third pass
WHILE(-3 >= 0) //False break out of loop
IF(-3 == 0) // false so we jump to the else
ELSE: 3 is not divisible by 3.
One quick way to easily spot check your loops that aren't performing as expected is to just manually run through them with an easy input.
I am trying to solve a problem from codility
"Even sums"
but am unable to do so. Here is the question below.
Even sums is a game for two players. Players are given a sequence of N positive integers and take turns alternately. In each turn, a player chooses a non-empty slice (a subsequence of consecutive elements) such that the sum of values in this slice is even, then removes the slice and concatenates the remaining parts of the sequence. The first player who is unable to make a legal move loses the game.
You play this game against your opponent and you want to know if you can win, assuming both you and your opponent play optimally. You move first.
Write a function:
string solution(vector< int>& A);
that, given a zero-indexed array A consisting of N integers, returns a string of format "X,Y" where X and Y are, respectively, the first and last positions (inclusive) of the slice that you should remove on your first move in order to win, assuming you have a winning strategy. If there is more than one such winning slice, the function should return the one with the smallest value of X. If there is more than one slice with the smallest value of X, the function should return the shortest. If you do not have a winning strategy, the function should return "NO SOLUTION".
For example, given the following array:
A[0] = 4 A[1] = 5 A[2] = 3 A[3] = 7 A[4] = 2
the function should return "1,2". After removing a slice from positions 1 to 2 (with an even sum of 5 + 3 = 8), the remaining array is [4, 7, 2]. Then the opponent will be able to remove the first element (of even sum 4) or the last element (of even sum 2). Afterwards you can make a move that leaves the array containing just [7], so your opponent will not have a legal move and will lose. One of possible games is shown on the following picture
Note that removing slice "2,3" (with an even sum of 3 + 7 = 10) is also a winning move, but slice "1,2" has a smaller value of X.
For the following array:
A[0] = 2 A[ 1 ] = 5 A[2] = 4
the function should return "NO SOLUTION", since there is no strategy that guarantees you a win.
Assume that:
N is an integer within the range [1..100,000]; each element of array A is an integer within the range [1..1,000,000,000]. Complexity:
expected worst-case time complexity is O(N); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified.
I have found a solution online in python.
def check(start, end):
if start>end:
res = 'NO SOLUTION'
else:
res = str(start) + ',' + str(end)
return res
def trans( strr ):
if strr =='NO SOLUTION':
return (-1, -1)
else:
a, b = strr.split(',')
return ( int(a), int(b) )
def solution(A):
# write your code in Python 2.7
odd_list = [ ind for ind in range(len(A)) if A[ind]%2==1 ]
if len(odd_list)%2==0:
return check(0, len(A)-1)
odd_list = [-1] + odd_list + [len(A)]
res_cand = []
# the numbers at the either end of A are even
count = odd_list[1]
second_count = len(A)-1-odd_list[-2]
first_count = odd_list[2]-odd_list[1]-1
if second_count >= count:
res_cand.append( trans(check( odd_list[1]+1, len(A)-1-count )))
if first_count >= count:
res_cand.append( trans(check( odd_list[1]+count+1, len(A)-1 )))
twosum = first_count + second_count
if second_count < count <= twosum:
res_cand.append( trans(check( odd_list[1]+(first_count-(count-second_count))+1, odd_list[-2] )))
###########################################
count = len(A)-1-odd_list[-2]
first_count = odd_list[1]
second_count = odd_list[-2]-odd_list[-3]-1
if first_count >= count:
res_cand.append( trans(check( count, odd_list[-2]-1 )))
if second_count >= count:
res_cand.append( trans(check( 0, odd_list[-2]-count-1)) )
twosum = first_count + second_count
if second_count < count <= twosum:
res_cand.append( trans(check( count-second_count, odd_list[-3])) )
res_cand = sorted( res_cand, key=lambda x: (-x[0],-x[1]) )
cur = (-1, -2)
for item in res_cand:
if item[0]!=-1:
cur = item
return check( cur[0], cur[1] )
This code works and I am unable to understand the code and flow of one function to the the other. However I don't understand the logic of the algorithm. How it has approached the problem and solved it. This might be a long task but can anybody please care enough to explain me the algorithm. Thanks in advance.
So far I have figured out that the number of odd numbers are crucial to find out the result. Especially the index of the first odd number and the last odd number is needed to calculate the important values.
Now I need to understand the logic behind the comparison such as "if first_count >= count" and if "second_count < count <= twosum".
Update:
Hey guys I found out the solution to my question and finally understood the logic of the algorithm.
The idea lies behind the symmetry of the array. We can never win the game if the array is symmetrical. Here symmetrical is defined as the array where there is only one odd in the middle and equal number of evens on the either side of that one odd.
If there are even number of odds we can directly win the game.
If there are odd number of odds we should always try to make the array symmetrical. That is what the algorithm is trying to do.
Now there are two cases to it. Either the last odd will remain or the first odd will remain. I will be happy to explain more if you guys didn't understand it. Thanks.
I have a list of numbers. Instead of painting them all in one row I am painting the list in rows of 5.
Now I can select one number and from there move left, right, up or down.
In this list of 15 numbers (indexed 0 to 14) I have selected index 11, coloured red.
If I move left I have to subtract 1 from the index of my selection. If I move right I add 1. Down means I add 5 and up means I subtract 5.
However, if I go down when I am in the bottom-most row, I want to end up in the first row, as such:
The math / algorithm for that is simple:
index += 5;
if (index > list.size() ) index = index % 5; // % is modulo
//So, since I start with index 11: (11 + 5) % 5 = 1, which is the index of 01.
However, I cannot seem to figure out what to do when I am going from the top-most row up, which takes me to the bottom-most row. (From 01 I would end at 11)
If I have a list of exactly 15 items, then I could simply do:
index -= 5;
if (index < 0) index += index.size();
//So: 1 - 5 = -4
// -4 + 15 = 11.
But if my list is not divisible by 5, then this does not work.
So I am looking for an algorithm that would solve this problem in all cases, including when the size of a list is not divisble by the length of its rows.
This can probably be optimized further, but here's one approach:
var fullRows = list.Length / NUM_COLUMNS; //using integer division
var maxPos = fullRows * NUM_COLUMNS + currentIndex;
return maxPos < list.Length ? maxPos : maxPos - NUM_COLUMNS;
What this does is gets the number of full rows then starts by assuming there is another row after it. It then checks if that position really exists, and if not it backs off a row to be inside the final full row.
given an array:
array = [16 16 16 22 23 23 23 25 52 52 52]
I want return a list of indices that point to the elements of three repeating numbers.
In this case that would be :
indices = find_sequence(nbr_repeats = 3)
print indices
[0 1 2 4 5 6 8 9 10]
what is the fastest and most elegant algorithm to use in order to implement find_sequence?
Simplest way i know of...keep a track of the first place you saw a number. Keep on going til you find a different number, then if the sequence is long enough, add all the numbers from the start of the sequence til just before the end.
(Of course, you'll have to check the sequence length after you're done checking elements, too. I did it by iterating one past the end and just skipping the element check on the last iteration.)
To find_repeats (input : list, minimum : integer):
start := 0
result := []
for each x from 0 to (input length):
' "*or*" here is a short-circuit or
' so we don't go checking an element that doesn't exist
if x == (input length) *or* array[x] != array[start]:
if (x - start) >= minimum:
append [start...(x - 1)] to result
start := x
return result
Based on OP's assumption:
the list is sorted
the largest frequency is nbr_repeats
This might work:
def find_sequence(nbr_repeats, l):
res = []
current = -1
count = 0
idx = 0
for i in l:
if i == current:
count += 1
if count == nbr_repeats:
for k in reversed(range(nbr_repeats)):
res.append(idx-k)
else:
current = i
count = 1
idx += 1
return res
This looks to me like a special case of the Boyer-Moore string search algorithm, and since any language you use will contain optimisations for string search, perhaps the most elegant answer is to treat your data as a character array (i.e. a string) and use your language's built in string search functions... Note that this only works if your numbers fit into your language's supported character set (e.g. no numbers bigger than 128 in ASCII)
Since you did not specify a language, here is a pseudocode:
find_sequence(array: array of int, nbr_repeats: int) : array of int
retVal = emty array of int // the return'd array
last = empty array of int // collection of last seen same elements
i = -1
for each element e in array
++i
if (isempty(last))
add(last, e) // just starting
else if (count(last, e) >= nbr_repeats)
add(retVal, i-nbr_repeats) // found an index
else if (e == first(last))
add(last, e) // we have encountered this element before
else
if (count(last, e) >= nbr_repeats)
for (j=nbr_repeats-1; j>0; --j)
add(retVal, i-j) // catching up to i with indices
last = [e] // new element
if (count(last, e) >= nbr_repeats)
for (j=nbr_repeats-1; j>0; --j)
add(retVal, i-j) // handle end of array properly
return retVal
Edit: removed comment about sorting as it would mangle the original indices.
Note: you could also just keep the last element and its seen-count instead of maintaining a list of last same elements
Saw this question recently:
Given 2 arrays, the 2nd array containing some of the elements of the 1st array, return the minimum window in the 1st array which contains all the elements of the 2nd array.
Eg :
Given A={1,3,5,2,3,1} and B={1,3,2}
Output : 3 , 5 (where 3 and 5 are indices in the array A)
Even though the range 1 to 4 also contains the elements of A, the range 3 to 5 is returned Since it contains since its length is lesser than the previous range ( ( 5 - 3 ) < ( 4 - 1 ) )
I had devised a solution but I am not sure if it works correctly and also not efficient.
Give an Efficient Solution for the problem. Thanks in Advance
A simple solution of iterating through the list.
Have a left and right pointer, initially both at zero
Move the right pointer forwards until [L..R] contains all the elements (or quit if right reaches the end).
Move the left pointer forwards until [L..R] doesn't contain all the elements. See if [L-1..R] is shorter than the current best.
This is obviously linear time. You'll simply need to keep track of how many of each element of B is in the subarray for checking whether the subarray is a potential solution.
Pseudocode of this algorithm.
size = bestL = A.length;
needed = B.length-1;
found = 0; left=0; right=0;
counts = {}; //counts is a map of (number, count)
for(i in B) counts.put(i, 0);
//Increase right bound
while(right < size) {
if(!counts.contains(right)) continue;
amt = count.get(right);
count.set(right, amt+1);
if(amt == 0) found++;
if(found == needed) {
while(found == needed) {
//Increase left bound
if(counts.contains(left)) {
amt = count.get(left);
count.set(left, amt-1);
if(amt == 1) found--;
}
left++;
}
if(right - left + 2 >= bestL) continue;
bestL = right - left + 2;
bestRange = [left-1, right] //inclusive
}
}