0% found this document useful (0 votes)
6 views21 pages

Notes Arrays

The document contains solutions and notes for various programming questions, focusing on algorithms for detecting duplicates, finding missing numbers, and manipulating arrays. Each question outlines a specific approach, such as using sets, mathematical formulas, and bitwise operations. The solutions emphasize efficiency and clarity in handling data structures and algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

Notes Arrays

The document contains solutions and notes for various programming questions, focusing on algorithms for detecting duplicates, finding missing numbers, and manipulating arrays. Each question outlines a specific approach, such as using sets, mathematical formulas, and bitwise operations. The solutions emphasize efficiency and clarity in handling data structures and algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1

Solution

Notes:

Just see if the length of set of nums is less than the length of the array nums. That
means there are duplicates.
Question 2

Solution

Notes:

Use the formula of sum of series. Sum = n(n+1) / 2. This will give me the sum of all
the numbers in the array. This sum will include the missing number as well. Now I
can subtract the numbers in the array from the total – this is done in the for loop. The
number that remains in the end will be the missing number.
Question 3

Solution

Notes:

Since the range is [1,n], I can simply create a set that has all values from 1 to n. I
can then subtract the set of nums from this set. This will give me the missing
numbers.
Question 4

Solution

Notes:

The idea is simple: x ^ x = 0 and x ^ 0 = x.

Using this, I can xor all values in nums with each other. The ones that are present
twice will get to be 0. And the number that appears once will be the one left.
Question 5
Solution

Notes:

If the length of the 2d array is not equal to m*n, the conversion is not possible so
return the empty array.

I did this question the hard way in my first try:

The key is indexing and knowing that you can take a ‘step’ in the forward for loop as
well. Basically, I make small arrays of length ‘n’ from the original and append them
into the array I must return.
Question 6

Solution

Notes:

The instinct is to pop all zeros, keep a counter and then append the 0s popped.
However, a much better way is to see if the number is a 0, if it’s not, it is sent to the
front of the array. What this does is that all the non-zeros numbers of the array are
now in the front of the array, in the same order as the original nums. Now I can
simply add the 0s at the end. len(nums) – index, tells me how many zeros to add.
Question 7

Solution
Notes:

I must create two arrays – left and right. These will have the product of the numbers
in the array both ways – once as I go left, and once as I go right. Then I will multiply
these two arrays to get my answer

For example, the array nums is [1,2,3,4]. I make left and right as follows:

Left = [1,1,1,1] and Right = [1,1,1,1]

Now starting from the 1st index of left, I begin to multiply the number in the left array
with the number in the nums.

This makes Left as [1,1,2,6]. Now Right is tricky, as in the indexing. Recall the
concept of step, especially in decrementing for loop.

Right then becomes [24,12,4,1].

Why the first index of left and the last index of right must be 1?

left[i] stores the product of all elements to the left of the current element nums[i].
Since there are no elements to the left of the first element nums[0], left[0] is set to 1
(a neutral value for multiplication). This way, when we calculate the products of the
elements to the left of each index, left[0] * nums[0] will correctly yield nums[0].
Question 8

Solution1:
Notes:

This is the concept of slow and fast pointer – aka tortoise and hare, used to detect
cycle in linked lists. We set two pointers, fast and slow. Slow moves at pace 1, and
fast moves at pace 2. When they meet, we break the loop. Now we start again, first
set the fast pointer to 0, and then the pace of both fast and slow is the same. When
they meet now, they will meet at the number that is duplicate.

There are many cases where this method can fail if the question is changing a little
bit. I found the other solution much better.

Solution 2:

Notes:

The code uses the cyclic sort algorithm to find the duplicate number. This algorithm
leverages the property of the input list, where each number in the list is in the range
[1, n] (where n is the length of the list).

We do abs(i) – 1, to cater to zero index arrays. My goal is to get the appropriate


index to access nums.

If nums[temp] < 0, this checks if number in nums at index temp is negative. What this
means is that we have found our duplicate. If the number is negative, convert to
positive and return. Else, return it is as it is. However, if nums[temp] is not negative,
we make it negative, so that it can be identified if its repeated.
Question 9

Solution1:

This was my first instinct, simple but not constant space as I am using a
hashmap.
Solution2:

Concept is same as the previous question. I get the appropriate index by subtracting
one from the absolute value of the current number. I then check if it’s less than 0. If it
is negative, it means it is duplicate. I however, append index + 1 to counter the -1 we
did to calculate the index. If the number is positive, I simply make it negative
(basically marking it as visited).
Question 10

Solution:
Notes:

I first find all places where there is 0 in the 2d array. I intend to append the ‘j’ index of
these places in an array.

If I encounter 0 in a row, it is easy, as I will just set the entire row as 0. However,
setting the values in column as 0 is difficult, so I keep track of these values in my
‘indexwhereZero’ array.
Question 11
Solution:

Notes:

See video online man, this is intuitive to me now, but nuances are to be
understood by watching video.
Question 12

Solution:

Notes: Transpose and then reverse rows. The condition if j < i: is necessary in the matrix
rotation algorithm to avoid unnecessary swaps and duplications during the transposition
step. When rotating a matrix 90 degrees clockwise (or 270 degrees counterclockwise) by
swapping elements across the main diagonal, we only need to swap elements in the upper
triangular part of the matrix (elements above the main diagonal). Swapping elements in the
lower triangular part (elements below the main diagonal) would essentially undo the
swapping we just did, resulting in the same matrix as the original.
Question 13

Solution:

Notes:

Remove duplicates via set. Then check if (i-1) is in nums, if so, it is not the start of the
sequence. ‘Continue’ will move to the next iteration of the loop. If we find a number that can
be start of a sequence, we set ‘diff’ as 1, and then continue to increment it as the sequence
continues. We then continue to do this, there will be many sequences in an array, so we take
max we get and store it in ‘longest’. This variable will be updated if a longer sequence comes
along.
Question 14

Solution:
Notes:

If number is less than 1 or out of range, set it as 0.

Then check if the number we have is 0, or some other number within the range [1,n].

If it is in the range, “ ind = nums[i] % (n + 1) - 1” is used to calculate the index where


we will mark the presence of the current number.

We set the value of visited index as “nums[ind] += n + 1”. This is done to avoid
conflicts with other numbers in the array. Since we are adding (n+1) to the current
value, we ensure that we don’t give a value that is already present in the array.

Now since all visited indexes are out of the range [1,n], if any index has value less
than or equal to n, we know that it is missing. We can return it.

However, in the case where all numbers are present, we can simply return n+1 as
the missing number is not in the array.

You might also like