Skip to content
Merged
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
49 changes: 49 additions & 0 deletions CPP/Array/firstandlastpositionofelement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
int k = nums.size() - 1;
vector<int> v(2, 0);
int f = 0, l = 0;

for (int i = 0; i < nums.size(); i++)
{
if (f == 0 && nums[i] == target)
{
v[0] = i;
f = 1;
}
if (l == 0 && nums[k] == target)
{
v[1] = k;
l = 1;
}
if (f == 1 && l == 1)
return v;
k--;
}

// If not found
v = {-1, -1};
return v;
}
};

int main()
{
Solution s;
vector<int> nums = {5, 7, 7, 8, 8, 10};
int target = 8;

vector<int> result = s.searchRange(nums, target);

cout << "Target " << target << " found at range: ["
<< result[0] << ", " << result[1] << "]" << endl;

return 0;
}
74 changes: 74 additions & 0 deletions CPP/Array/nextpermutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
void nextPermutation(vector<int> &nums)
{
int y = -1;
if (nums.size() == 1)
return;

// Step 1: find the first decreasing element from the end
for (int i = nums.size() - 2; i >= 0; i--)
{
if (nums[i] < nums[i + 1])
{
y = i;
break;
}
}

// Step 2: if found, swap it with the next larger element from the end
if (y != -1)
{
for (int j = nums.size() - 1; j > y; j--)
{
if (nums[y] < nums[j])
{
swap(nums[y], nums[j]);
break;
}
}
// Step 3: reverse the part after index y
rev(nums, y + 1, nums.size() - 1);
}
else
{
// If no such index found, reverse the whole array
rev(nums, 0, nums.size() - 1);
}
}

void rev(vector<int> &nums, int l, int r)
{
while (l < r)
{
swap(nums[l], nums[r]);
l++;
r--;
}
}
};

int main()
{
Solution s;
vector<int> nums = {1, 2, 3};

cout << "Original permutation: ";
for (int n : nums)
cout << n << " ";
cout << endl;

s.nextPermutation(nums);

cout << "Next permutation: ";
for (int n : nums)
cout << n << " ";
cout << endl;

return 0;
}
42 changes: 42 additions & 0 deletions CPP/Array/removeduplicatefromsortedarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
if (nums.empty())
return 0; // Handle empty vector case

int r = 1;
for (int i = 1; i < nums.size(); i++)
{
if (nums[i] != nums[r - 1])
{
nums[r] = nums[i];
r++;
}
}
return r;
}
};

int main()
{
Solution s;
vector<int> nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};

int newLength = s.removeDuplicates(nums);

cout << "New length: " << newLength << endl;
cout << "Array after removing duplicates: ";
for (int i = 0; i < newLength; i++)
{
cout << nums[i] << " ";
}
cout << endl;

return 0;
}
40 changes: 40 additions & 0 deletions CPP/Array/removeelement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
int removeElement(vector<int> &nums, int val)
{
int k = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] != val)
{
nums[k] = nums[i];
k++;
}
}
return k;
}
};

int main()
{
Solution s;
vector<int> nums = {3, 2, 2, 3};
int val = 3;

int newLength = s.removeElement(nums, val);

cout << "New length: " << newLength << endl;
cout << "Array after removing " << val << ": ";
for (int i = 0; i < newLength; i++)
{
cout << nums[i] << " ";
}
cout << endl;

return 0;
}
49 changes: 49 additions & 0 deletions CPP/Array/twosum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

// You may assume that each input would have exactly one solution, and you may not use the same element twice.

// You can return the answer in any order.
#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> v;
for (int i = 0; i < nums.size(); i++)
{
for (int j = i + 1; j < nums.size(); j++)
{
if (nums[j] + nums[i] == target)
{
v = {j, i};
return v;
}
}
}
return v;
}
};

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

vector<int> result = s.twoSum(nums, target);

if (!result.empty())
{
cout << "Indices: " << result[0] << " and " << result[1] << endl;
}
else
{
cout << "No two numbers found that add up to target." << endl;
}

return 0;
}