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
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp"
}
}
76 changes: 32 additions & 44 deletions Leetcode_1.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,45 @@
// Copy only the CLASS block to sumbit code on Leetcode !!

#include <iostream>
#include<vector>
#include <vector>
#include <unordered_map>
using namespace std;

// Define a class named Solution
class Solution
{
public:
// Declare variables
int arr[10]; // Array to hold a maximum of 10 elements
int target, n, i, j; // Variables for target, size of array, and iterators

public:
// Function to find two indices in the array whose values sum up to the target
int twoSum()
{
// Prompt user to enter the size of the array
cout << "Enter the size of array < 10\n";
cin >> n; // Read the size of the array

// Prompt user to enter the elements of the array
cout << "Enter the array elements\n";
for (i = 0; i < n; i++) { // Loop to read 'n' elements
cin >> arr[i];
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m; // Map number -> index
vector<int> ans; // Vector to store the answer indices

for (int i = 0; i < nums.size(); i++) {

// Prompt user to enter the target sum
cout << "Enter the target element\n";
cin >> target;

// Nested loop to find two indices whose elements add up to the target
for (i = 0; i < n; i++) { // Outer loop
for (j = 0; j < n; j++) { // Inner loop
// Check if the sum of elements at indices i and j equals the target
if (arr[i] + arr[j] == target) {
// Print the indices and values of the two elements
cout << "The indexes are " << i << " and " << j << endl;
cout << "The value at those indexes are " << arr[i] << " and " << arr[j] << endl;
cout << arr[i] << " + " << arr[j] << " = " << target;

return 0; // Exit the function once a pair is found
}
if (m.find(target - nums[i]) != m.end()) {
ans.push_back(m[target - nums[i]]); // index of target - nums
ans.push_back(i); // current index
return ans; // return the vector
}
}

return 0; // Return 0 if no pair is found
m[nums[i]] = i;
}

return ans; // will be empty if no solution found
}
};

// Main function to execute the program
int main() {
Solution a; // Create an object of the Solution class
a.twoSum(); // Call the twoSum function
return 0; // Return 0 to indicate successful execution
Solution sol;
//You test more cases by changing inputs
// Simple test case
vector<int> nums = {2, 7, 11, 15};
int target = 9;

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

if (!result.empty()) {
cout << "Indices: " << result[0] << ", " << result[1] << endl;
} else {
cout << "No two sum solution found." << endl;
}

return 0;
}
16 changes: 5 additions & 11 deletions Leetcode_1051.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 1051. Height Checker
class Solution {
public:
int heightChecker(vector<int>& heights) {
int res = 0;
int size = heights.size();
vector<int> exp;
for(int val : heights) {
exp.push_back(val);
}
for(int i=0;i<size;i++) {
for(int j=0;j<size-1;j++){
if(exp[j] > exp[j+1]){
swap(exp[j],exp[j+1]);
}
}
}
vector<int> exp(heights); // insert values of heightS in exp

sort(exp.begin() , exp.end()); //inbuilt func in c++ to sort vec

for(int i=0;i<exp.size();i++){
if(exp[i] != heights[i]){
res++;
Expand Down