0% found this document useful (0 votes)
9 views7 pages

Array Example Questions: 1. Remove Duplicates From Sorted Array

The document provides a series of coding problems related to arrays and strings, including removing duplicates from a sorted array, maximizing stock profits, rotating an array, checking for duplicates, finding a single number, and intersecting two arrays. Each problem includes examples, constraints, and solutions implemented in C or C++. The solutions emphasize efficiency, often requiring in-place modifications and linear runtime complexity.
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)
9 views7 pages

Array Example Questions: 1. Remove Duplicates From Sorted Array

The document provides a series of coding problems related to arrays and strings, including removing duplicates from a sorted array, maximizing stock profits, rotating an array, checking for duplicates, finding a single number, and intersecting two arrays. Each problem includes examples, constraints, and solutions implemented in C or C++. The solutions emphasize efficiency, often requiring in-place modifications and linear runtime complexity.
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

Array Example Questions

1. Remove duplicates from sorted array


Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique
element appears only once. The relative order of the elements should be kept the same.

Consider the number of unique elements in nums to be k. After removing duplicates, return the number of unique
elements k.

The first k elements of nums should contain the unique numbers in sorted order. The remaining elements beyond index k
- 1 can be ignored.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array

int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == [Link];

for (int i = 0; i < k; i++) {

assert nums[i] == expectedNums[i];

If all assertions pass, then your solution will be accepted.

Example 1:

Input: nums = [1,1,2]

Output: 2, nums = [1,2,_]

Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.

It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]

Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]

Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.

It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

 1 <= [Link] <= 3 * 104

 -100 <= nums[i] <= 100


 nums is sorted in non-decreasing order.

Solution:

int removeDuplicates(int* nums, int numsSize) {


if (numsSize == 0){
return 0;
}

int k=1; //index for placing the next unique element

for (int i=1;i<numsSize;i++){


if(nums[i] != nums[k-1]){
nums[k] = nums[i];
k++;
}
}
return k;
}

2. Best Time to Buy and Sell Stock II


3. You are given an integer array prices where prices[i] is the price of a given stock on
the i th day.
4. On each day, you may decide to buy and/or sell the stock. You can only hold at most
one share of the stock at any time. However, you can sell and buy the stock multiple times
on the same day, ensuring you never hold more than one share of the stock.
5. Find and return the maximum profit you can achieve.
6.
7. Example 1:
8. Input: prices = [7,1,5,3,6,4]
9. Output: 7
10. Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profi
t = 5-1 = 4.
11. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3
= 3.
12. Total profit is 4 + 3 = 7.

13. Example 2:

Soltion:

int maxProfit(int* prices, int pricesSize) {

int profit = 0;

for (int i =1; i<pricesSize ; i++)


{
if (prices[i]>prices[i-1]){
profit += prices[i]-prices[i-1];
}
}
return profit;
}

3. Rotate an array

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3

Output: [5,6,7,1,2,3,4]

Explanation:

rotate 1 steps to the right: [7,1,2,3,4,5,6]

rotate 2 steps to the right: [6,7,1,2,3,4,5]

rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2

Output: [3,99,-1,-100]

Explanation:

rotate 1 steps to the right: [99,-1,-100,3]

rotate 2 steps to the right: [3,99,-1,-100]

Constraints:

 1 <= [Link] <= 105

 -231 <= nums[i] <= 231 - 1

 0 <= k <= 105

Follow up:

 Try to come up with as many solutions as you can. There are at least three di erent ways to solve this problem.

 Could you do it in-place with O(1) extra space?

Solution:
void reverse(int* nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}

void rotate(int* nums, int numsSize, int k) {


if (numsSize == 0) return;

k = k % numsSize;

reverse(nums, 0, numsSize - 1);


reverse(nums, 0, k - 1);
reverse(nums, k, numsSize - 1);
}

4. Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element
is distinct.

Example 1:

Input: nums = [1,2,3,1]

Output: true

Explanation:

The element 1 occurs at the indices 0 and 3.

Example 2:

Input: nums = [1,2,3,4]

Output: false

Solution:

#include <stdlib.h> // For qsort function

//comparison function for qsort


int cmp(const void*a,const void*b)
{
return (*(int*)a-*(int*)b);
}

bool containsDuplicate(int* nums, int numsSize) {

if(numsSize <=1)
return false;
//Sort the array first
qsort(nums,numsSize,sizeof(int),cmp);

//check the consecutive elements for duplicates


for(int i = 0; i < numsSize -1;i++)
{
if(nums[i] == nums[i+1])
{
return true;
}
}
return false;
}

5. Single Number
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

Example 1:

Input: nums = [2,2,1]

Output: 1

Example 2:

Input: nums = [4,1,2,1,2]

Output: 4

Solution:

int singleNumber(int* nums, int numsSize) {

int result = 0;
for(int i = 0; i < numsSize; i++){
result ^= nums[i]; //XOR GATE

}
return result;
}

6. Intersect
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear
as many times as it shows in both arrays and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]

Output: [2,2]
Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output: [4,9]

Explanation: [9,4] is also accepted.

Solution: (C++)

class Solution {

public:

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {

vector<int> result;

// Step 1: Sort both arrays

sort([Link](), [Link]());

sort([Link](), [Link]());

int i = 0, j = 0;

// Step 2: Use two pointers to find intersection

while (i < [Link]() && j < [Link]()) {

if (nums1[i] < nums2[j]) {

i++;

else if (nums1[i] > nums2[j]) {

j++;

else {

// nums1[i] == nums2[j]

result.push_back(nums1[i]);

i++;

j++;

return result;

}
};

STRING
1. Reverse a string

 Write a function that reverses a string. The input string is given as an array of characters s .

 You must do this by modifying the input array in-place with O(1) extra memory.

 Example 1:

 Input: s = ["h","e","l","l","o"]

 Output: ["o","l","l","e","h"]

Solution:
void reverseString(char* s, int sSize) {
int left = 0;
int right = sSize - 1;

while (left < right) {


char temp = s[left];
s[left] = s[right];
s[right] = temp;

left++;
right--;
}
}

You might also like