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

TCS Prime Array String Questions CPP

The document presents solutions to common problems involving arrays and strings in C++, including finding the second largest element, removing duplicates from a sorted array, finding two numbers that sum to a target, reversing a string, and checking if two strings are anagrams. Each problem is addressed with both a normal and an optimized approach, showcasing different coding techniques and algorithms. The optimized solutions generally involve more efficient algorithms, such as using hash maps or two-pointer techniques.
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 views4 pages

TCS Prime Array String Questions CPP

The document presents solutions to common problems involving arrays and strings in C++, including finding the second largest element, removing duplicates from a sorted array, finding two numbers that sum to a target, reversing a string, and checking if two strings are anagrams. Each problem is addressed with both a normal and an optimized approach, showcasing different coding techniques and algorithms. The optimized solutions generally involve more efficient algorithms, such as using hash maps or two-pointer techniques.
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

TCS Prime Arrays & Strings Questions with C++

Answers

1. Find Second Largest Element

Problem: Find the second largest element in an array.

Normal Approach:
// Normal Approach
#include <bits/stdc++.h>
using namespace std;

int main() {

vector<int> arr = {1,5,3,9,7};

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

cout << arr[[Link]()-2];

return 0;
}

Most Optimized Approach:


// Optimized Approach
#include <bits/stdc++.h>
using namespace std;

int main() {

vector<int> arr = {1,5,3,9,7};

int largest = INT_MIN;


int secondLargest = INT_MIN;

for(int x : arr) {

if(x > largest) {


secondLargest = largest;
largest = x;
}
else if(x > secondLargest && x != largest) {
secondLargest = x;
}
}

cout << secondLargest;

return 0;
}

2. Remove Duplicates from Sorted Array

Problem: Remove duplicates from sorted array using two pointers.

Normal Approach:
// Normal Approach
#include <bits/stdc++.h>
using namespace std;
int main() {

vector<int> arr = {1,1,2,2,3};

set<int> s;

for(int x : arr) {
[Link](x);
}

for(int x : s) {
cout << x << " ";
}

return 0;
}

Most Optimized Approach:


// Optimized Approach
#include <bits/stdc++.h>
using namespace std;

int removeDuplicates(vector<int>& arr) {

int i = 0;

for(int j = 1; j < [Link](); j++) {

if(arr[i] != arr[j]) {
i++;
arr[i] = arr[j];
}
}

return i + 1;
}

3. Two Sum

Problem: Find two numbers whose sum equals target.

Normal Approach:
// Brute Force
#include <bits/stdc++.h>
using namespace std;

int main() {

vector<int> arr = {2,7,11,15};


int target = 9;

for(int i=0;i<[Link]();i++) {
for(int j=i+1;j<[Link]();j++) {

if(arr[i] + arr[j] == target) {


cout << i << " " << j;
}
}
}

return 0;
}

Most Optimized Approach:


// Optimized Approach using HashMap
#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> arr = {2,7,11,15};
int target = 9;

unordered_map<int,int> mp;

for(int i=0;i<[Link]();i++) {

int complement = target - arr[i];

if([Link](complement) != [Link]()) {
cout << mp[complement] << " " << i;
}

mp[arr[i]] = i;
}

return 0;
}

4. Reverse String

Problem: Reverse a string.

Normal Approach:
// Using reverse()
#include <bits/stdc++.h>
using namespace std;

int main() {

string s = "hello";

reverse([Link](), [Link]());

cout << s;

return 0;
}

Most Optimized Approach:


// Two Pointer Approach
#include <bits/stdc++.h>
using namespace std;

int main() {

string s = "hello";

int start = 0;
int end = [Link]() - 1;

while(start < end) {


swap(s[start], s[end]);
start++;
end--;
}

cout << s;

return 0;
}

5. Check Anagram

Problem: Check whether two strings are anagrams.


Normal Approach:
// Sorting Approach
#include <bits/stdc++.h>
using namespace std;

int main() {

string s1 = "listen";
string s2 = "silent";

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

if(s1 == s2)
cout << "Anagram";
else
cout << "Not Anagram";

return 0;
}

Most Optimized Approach:


// Frequency Count Approach
#include <bits/stdc++.h>
using namespace std;

int main() {

string s1 = "listen";
string s2 = "silent";

vector<int> freq(26,0);

for(char ch : s1) {
freq[ch - 'a']++;
}

for(char ch : s2) {
freq[ch - 'a']--;
}

bool flag = true;

for(int x : freq) {
if(x != 0) {
flag = false;
}
}

if(flag)
cout << "Anagram";
else
cout << "Not Anagram";

return 0;
}

You might also like