Welcome to Day 6 of my 100 Days of DSA challenge! Today, I solved five problems focused on arrays. Here's a quick look at the questions I tackled and how I approached them 🤖✨ Check out my GitHub repository for all my solutions and progress. Let’s keep learning!
1. Given an array containing n-1 distinct integers in the range of 1 to n, write a program to find the missing number in O(n) time using a mathematical formula.
The approach is in the code below:
// To find the missing number in an array of n-1 elements
#include <iostream>
#include <vector>
using namespace std;
int missingNo(vector<int> &arr, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return (n * (n + 1) / 2) - sum;
}
int main(){
vector<int> arr = {1, 2, 3, 4, 6, 7, 8};
int n = arr.size() + 1;
cout << "The missing number is: " << missingNo(arr, n) << endl;
return 0;
}
Output:
2. Write a program to rotate an array of size n by k steps to the right in O(n) time without using extra space.
This can be done by reversing the array in steps by using jugglig algorithm, this is achieved in O(n) time.
Code:
// To rotate an array to the right by k elements
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void rightRotate(vector<int> &arr, int k) {
int n = arr.size();
k = k % n;
reverse(arr.begin(), arr.end());
reverse(arr.begin(), arr.begin() + k);
reverse(arr.begin() + k, arr.end());
}
int main(){
vector<int> arr = {1, 2, 3, 4, 5, 6, 7};
int k = 3;
rightRotate(arr, k);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
3. Rearrange an array such that the first element is the largest, the second is the smallest, the third is the second largest, and so on. Solve this in O(n) time using no extra space.
Code:
// Rearrange an array such that the first element is the largest, the second is the smallest,
//the third is the second largest, and so on. Solve this in O(n) time using no extra space.
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void rearrange(vector<int> &arr) {
int n = arr.size();
int max_idx = n - 1;
int min_idx = 0;
int max_elem = arr[n - 1] + 1;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
arr[i] += (arr[max_idx] % max_elem) * max_elem;
max_idx--;
} else {
arr[i] += (arr[min_idx] % max_elem) * max_elem;
min_idx++;
}
}
for (int i = 0; i < n; i++) {
arr[i] = arr[i] / max_elem;
}
}
int main(){
vector<int> arr = {1, 2, 3, 4, 5, 6, 7};
rearrange(arr);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
4. Write a program to find the contiguous subarray with the maximum sum in O(n) time.
Code:
// Write a program to find the contiguous subarray with the maximum sum in O(n) time.
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int maxSubarraySum(vector<int> &arr) {
int n = arr.size();
int max_sum = arr[0];
int curr_sum = arr[0];
for (int i = 1; i < n; i++) {
curr_sum = max(arr[i], curr_sum + arr[i]);
max_sum = max(max_sum, curr_sum);
}
return max_sum;
}
int main(){
vector<int> arr = {-2, -3, 4, -1, -2, 1, 5, -3};
cout << "The maximum sum of a contiguous subarray is: " << maxSubarraySum(arr) << endl;
return 0;
}
Output:
5. Write a program to count the frequency of each element in an array in O(n) time without using extra space (modify the array temporarily if needed)
The code calculates the frequency of each integer (from 0
to n-1
) in an array using modular arithmetic to track counts within the array itself, avoiding extra space. It modifies each element to store cumulative frequency information, then extracts and displays the count for each integer.
Code:
// Write a program to count the frequency of each element in an array in O(n) time without using extra space (modify the array temporarily if needed)
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void countFreq(vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
arr[arr[i] % n] += n;
}
for (int i = 0; i < n; i++) {
cout << i << " occurs " << arr[i] / n << " times" << endl;
}
}
int main(){
vector<int> arr = {1, 2, 3, 4, 2, 1, 3, 2, 3, 4, 1, 2, 3, 4};
countFreq(arr);
return 0;
}
Output:
Happy Coding! 🍀✨