# Posts published in “Array”

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

Example 1:

Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]

Constraints:

• 1 <= arr.length <= 10^4
• 1 <= arr[i] <= 10^5

## Solution: Scan from right to left

Time complexity: O(n)
Space complexity: O(1)

## C++

You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.

Return the number of servers that communicate with any other server.

Example 1:

Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.

Example 2:

Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.


Example 3:

Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.


Constraints:

• m == grid.length
• n == grid[i].length
• 1 <= m <= 250
• 1 <= n <= 250
• grid[i][j] == 0 or 1

## Solution: Counting

Two passes:
First pass, count number of computers for each row and each column.
Second pass, count grid[i][j] if rows[i] or cols[j] has more than 1 computer.

Time complexity: O(m*n)
Space complexity: O(m + n)

## C++

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]


Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],

rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

## Solution: 2 Passes

First pass: mirror around diagonal
Second pass: mirror around y axis

Time complexity: O(n^2)
Space complexity: O(1)

## C++

Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: You are not suppose to use the library’s sort function for this problem.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

• A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.
• Could you come up with a one-pass algorithm using only constant space?

## Solution 1: Counting sort

Time complexity: O(n)
Space complexity: O(1)

## Solution 2: Two pointers

Time complexity: O(n)
Space complexity: O(1)

## C++

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,1,2,3,3],

Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.

It doesn't matter what values are set beyond the returned length.

## Solution:

Time complexity: O(n)
Space complexity: O(1)

## Related Problems

Mission News Theme by Compete Themes.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5795384,"math_prob":0.9893693,"size":612,"snap":"2019-51-2020-05","text_gpt3_token_len":210,"char_repetition_ratio":0.10526316,"word_repetition_ratio":0.0,"special_character_ratio":0.4019608,"punctuation_ratio":0.2236842,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98440135,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-27T09:26:58Z\",\"WARC-Record-ID\":\"<urn:uuid:7df849c6-8d65-4e37-9269-2533374cfc6d>\",\"Content-Length\":\"118366\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0ddebc25-efb6-4a9a-acda-83d48a966319>\",\"WARC-Concurrent-To\":\"<urn:uuid:cc2030f3-e917-4835-8774-9fca61940d93>\",\"WARC-IP-Address\":\"107.180.12.40\",\"WARC-Target-URI\":\"https://zxi.mytechroad.com/blog/category/algorithms/array/\",\"WARC-Payload-Digest\":\"sha1:7BAJBKTTNO5U7RM2IDNP5SJPE3AFSSJ6\",\"WARC-Block-Digest\":\"sha1:GGSNZP3TMOZKLWHD3NHOGKV5J35LNCZO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251696046.73_warc_CC-MAIN-20200127081933-20200127111933-00518.warc.gz\"}"}