Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions contiguosarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int n = nums.size();
unordered_map<int , int> map;
map[0] = -1;//added in order to handle subarrays that start from index 0
int rSum = 0;//aka prefix sum

int max = 0;//stores the maximum length of a balanced subarray found so far

for (int i = 0; i < n; i++) {
int num = nums[i];

if (num == 0) rSum--;//-1 if 0
else rSum++;//+1 if 1

if (map.find(rSum) != map.end()){//search if rSum was seen in the hashmap
max = std::max(max, i - map[rSum]);//computes the length of the max arraay length so far
}
else{
map[rSum]= i;//rSum was not seen before

}
}
return max;
}

};
35 changes: 35 additions & 0 deletions longestpalindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char, int> freq;

// count frequency of each character in the string
for (char c : s) {
freq[c]++; // increment count of character c
}
int length = 0;
bool hasOdd = false; // flag to check if any character has odd frequency

// iterate over each character frequency
for (auto it : freq) {
// it.second -> frequency of that character

// if frequency is even, we can use all characters
if (it.second % 2 == 0) {
length += it.second;
}
else {
length += it.second - 1;

// mark that at least one odd-frequency character exists
hasOdd = true;
}
}

// we can place exactly one character in the center of the palindrome
if (hasOdd) {
length += 1;
}
return length;
}
};
Empty file added subarraySum.cpp
Empty file.