0% found this document useful (0 votes)
5 views9 pages

Dynamic Array Problems

The document contains various C++ programming problems that focus on dynamic memory management and algorithmic challenges. Each problem includes a code snippet, time and space complexity analysis, and a brief explanation of the approach used to solve it. Topics covered include palindrome checking, array reversal, valid parenthesis strings, longest palindromic substrings, linked list loop detection, and more.
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)
5 views9 pages

Dynamic Array Problems

The document contains various C++ programming problems that focus on dynamic memory management and algorithmic challenges. Each problem includes a code snippet, time and space complexity analysis, and a brief explanation of the approach used to solve it. Topics covered include palindrome checking, array reversal, valid parenthesis strings, longest palindromic substrings, linked list loop detection, and more.
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
You are on page 1/ 9

Dynamic Memory Based C++ Problems (All Braces

Style)

1. Check whether the given string is palindrome


#include
using namespace std;
int main() {
string s;
cin >> s;
int l = 0;
int r = s.length() - 1;
bool flag = true;
while (l < r) {
if (s[l] != s[r]) {
flag = false;
break;
}
l++;
r--;
}
if (flag) {
cout << "Palindrome";
} else {
cout << "Not Palindrome";
}
return 0;
}
Time Complexity: O(n)
Space Complexity: O(1)
Explanation: Two pointers check characters from both ends toward the center.

2. Reverse the array


#include
using namespace std;
int main() {
int n;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int l = 0;
int r = n - 1;
while (l < r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
l++;
r--;
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
delete[] a;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
Explanation: Swap first and last elements iteratively until the array is reversed.

3. Valid parenthesis string


#include
using namespace std;
int main() {
string s;
cin >> s;
int n = s.length();
int* stack1 = new int[n];
int* stack2 = new int[n];
int top1 = -1;
int top2 = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
top1++;
stack1[top1] = i;
} else if (s[i] == '*') {
top2++;
stack2[top2] = i;
} else {
if (top1 >= 0) {
top1--;
} else if (top2 >= 0) {
top2--;
} else {
cout << "Invalid";
delete[] stack1;
delete[] stack2;
return 0;
}
}
}
while (top1 >= 0 && top2 >= 0) {
if (stack1[top1] > stack2[top2]) {
cout << "Invalid";
delete[] stack1;
delete[] stack2;
return 0;
}
top1--;
top2--;
}
if (top1 == -1) {
cout << "Valid";
} else {
cout << "Invalid";
}
delete[] stack1;
delete[] stack2;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
Explanation: Track '(' and '*' positions; ensure proper pairing order.

4. Longest palindromic substring


#include
using namespace std;
int main() {
string s;
cin >> s;
int n = s.length();
int start = 0;
int maxLen = 1;
for (int i = 0; i < n; i++) {
int l = i;
int r = i;
while (l >= 0 && r < n && s[l] == s[r]) {
if (r - l + 1 > maxLen) {
maxLen = r - l + 1;
start = l;
}
l--;
r++;
}
l = i;
r = i + 1;
while (l >= 0 && r < n && s[l] == s[r]) {
if (r - l + 1 > maxLen) {
maxLen = r - l + 1;
start = l;
}
l--;
r++;
}
}
for (int i = start; i < start + maxLen; i++) {
cout << s[i];
}
return 0;
}
Time Complexity: O(n²)
Space Complexity: O(1)
Explanation: Expand around each character to find the longest mirrored substring.

5. Find length of loop in a linked list


#include
using namespace std;
struct Node {
int val;
Node* next;
Node(int x) { val = x; next = NULL; }
};
int main() {
int n, pos;
cin >> n >> pos;
Node** nodes = new Node*[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node(i + 1);
}
for (int i = 0; i < n - 1; i++) {
nodes[i]->next = nodes[i + 1];
}
if (pos != -1) {
nodes[n - 1]->next = nodes[pos];
}
Node* slow = nodes[0];
Node* fast = nodes[0];
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
int count = 1;
Node* temp = slow->next;
while (temp != slow) {
count++;
temp = temp->next;
}
cout << count;
for (int i = 0; i < n; i++) delete nodes[i];
delete[] nodes;
return 0;
}
}
cout << 0;
for (int i = 0; i < n; i++) delete nodes[i];
delete[] nodes;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
Explanation: Uses Floyd’s cycle detection; counts nodes in the loop.

6. First missing positive integer


#include
using namespace std;
int main() {
int n;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
while (a[i] > 0 && a[i] <= n && a[a[i] - 1] != a[i]) {
int temp = a[i];
a[i] = a[temp - 1];
a[temp - 1] = temp;
}
}
for (int i = 0; i < n; i++) {
if (a[i] != i + 1) {
cout << i + 1;
delete[] a;
return 0;
}
}
cout << n + 1;
delete[] a;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
Explanation: Places each number at its correct index; the first mismatch index gives the
missing positive.

7. Floating point to string conversion


#include
using namespace std;
int main() {
double num;
int p;
cin >> num >> p;
if (num == 0.0) {
cout << "0.0";
return 0;
}
if (num < 0) {
cout << "-";
num = -num;
}
long long integerPart = (long long)num;
double fractionPart = num - integerPart;
cout << integerPart << ".";
for (int i = 0; i < p; i++) {
fractionPart = fractionPart * 10;
int digit = (int)fractionPart;
cout << digit;
fractionPart = fractionPart - digit;
}
return 0;
}
Time Complexity: O(p + log■■(num))
Space Complexity: O(1)
Explanation: Separates integer and fractional parts, prints up to p digits manually.

8. Sentence pattern match


#include
using namespace std;
int main() {
string s, t;
cin >> s;
cin >> t;
string words1[100], words2[100];
int c1 = 0, c2 = 0;
string temp = "";
for (int i = 0; i <= s.length(); i++) {
if (i == s.length() || s[i] == ' ') {
words1[c1++] = temp;
temp = "";
} else {
temp = temp + s[i];
}
}
temp = "";
for (int i = 0; i <= t.length(); i++) {
if (i == t.length() || t[i] == ' ') {
words2[c2++] = temp;
temp = "";
} else {
temp = temp + t[i];
}
}
int j = 0;
for (int i = 0; i < c1 && j < c2; i++) {
if (words1[i].find(words2[j]) != string::npos) {
j++;
}
}
if (j == c2) {
cout << "Match";
} else {
cout << "No Match";
}
return 0;
}
Time Complexity: O(n * m)
Space Complexity: O(n)
Explanation: Splits sentences manually into words; checks if second sentence’s words
appear in order in the first.

9. Combo offer selection


#include
using namespace std;
struct Item {
string name;
double price;
string date;
};
bool oneYear(string date) {
int y, m, d;
char c1, c2;
sscanf(date.c_str(), "%d%c%d%c%d", &y;, &c1;, &m;, &c2;, &d;);
time_t t = time(NULL);
tm* l = localtime(&t;);
int cy = l->tm_year + 1900;
int cm = l->tm_mon + 1;
int cd = l->tm_mday;
return (cy - y == 1 && cm == m && cd == d);
}
int main() {
int n;
double X;
cin >> n >> X;
Item* items = new Item[n];
for (int i = 0; i < n; i++) {
cin >> items[i].name >> items[i].price >> items[i].date;
}
bool* used = new bool[n];
for (int i = 0; i < n; i++) used[i] = false;
for (int i = 0; i < n; i++) {
if (items[i].price < X && oneYear(items[i].date) && !used[i]) {
cout << items[i].name << " " << items[i].price << " " << items[i].date << "\n";
used[i] = true;
}
}
delete[] items;
delete[] used;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(n)
Explanation: Filters items based on price, age (one-year), and uniqueness.

You might also like