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;
}