diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..27def72 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp" + } +} \ No newline at end of file diff --git a/Leetcode_1.cpp b/Leetcode_1.cpp index 17f7113..7690b41 100644 --- a/Leetcode_1.cpp +++ b/Leetcode_1.cpp @@ -1,57 +1,45 @@ // Copy only the CLASS block to sumbit code on Leetcode !! #include -#include +#include +#include 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 twoSum(vector& nums, int target) { + unordered_map m; // Map number -> index + vector 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 nums = {2, 7, 11, 15}; + int target = 9; + + vector 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; } diff --git a/Leetcode_1051.cpp b/Leetcode_1051.cpp index 672d209..6da923e 100644 --- a/Leetcode_1051.cpp +++ b/Leetcode_1051.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace std; // 1051. Height Checker class Solution { @@ -7,17 +8,10 @@ class Solution { int heightChecker(vector& heights) { int res = 0; int size = heights.size(); - vector exp; - for(int val : heights) { - exp.push_back(val); - } - for(int i=0;i exp[j+1]){ - swap(exp[j],exp[j+1]); - } - } - } + vector 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