diff --git a/2160. Minimum Sum of Four Digit Number After Splitting Digits b/2160. Minimum Sum of Four Digit Number After Splitting Digits new file mode 100644 index 0000000..6876265 --- /dev/null +++ b/2160. Minimum Sum of Four Digit Number After Splitting Digits @@ -0,0 +1,61 @@ +/* +Get all digits of num. +Sort them. +To get the minimum sum. We need to choose 1st and 2nd digits as tens digits and 3rd and 4th as ones digits. +As when the tens digits will be minimum, then only we will get minimum sum. +For example: +5643=>3,4,5,6 +30+40+5+6=86(min) + +Other possibilities: +30+50+4+6=90 +30+60+4+5=101 +40+50+4+6=100 and so on. + +Method 1: +Time complexity: O(nlogn) +Use array to store the digits of num. +*/ + +class Solution { + public int minimumSum(int num) { + int[] res=new int[4]; + int i=0; + while(num>0){ + res[i]=num%10; + num/=10; + i++; + } + Arrays.sort(res) + return res[0]*10+res[2]+res[1]*10+res[3]; + } +} + + + + + +//method 2 +/* +Method 2: +Use priorityqueue to store the digits of num. +Time complexity: O(n) +*/ + + +class Solution { + public int minimumSum(int num) { + PriorityQueuepq=new PriorityQueue<>(); + //complexity to insert: O(1) + while(num>0){ + pq.offer(num%10); + num/=10; + } + //complexity to remove: O(n) + int a=10*pq.remove(); + int b=10*pq.remove(); + int c=pq.remove(); + int d=pq.remove(); + return a+b+c+d; + } +} diff --git a/BFS.cpp b/BFS.cpp new file mode 100644 index 0000000..f0e309b --- /dev/null +++ b/BFS.cpp @@ -0,0 +1,88 @@ +// Program to print BFS traversal from a given +// source vertex. BFS(int s) traverses vertices +// reachable from s. +#include +using namespace std; + +// This class represents a directed graph using +// adjacency list representation +class Graph +{ + int V; // No. of vertices + + // Pointer to an array containing adjacency + // lists + vector> adj; +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int v, int w); + + // prints BFS traversal from a given source s + void BFS(int s); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj.resize(V); +} + +void Graph::addEdge(int v, int w) +{ + adj[v].push_back(w); // Add w to v’s list. +} + +void Graph::BFS(int s) +{ + // Mark all the vertices as not visited + vector visited; + visited.resize(V,false); + + // Create a queue for BFS + list queue; + + // Mark the current node as visited and enqueue it + visited[s] = true; + queue.push_back(s); + + while(!queue.empty()) + { + // Dequeue a vertex from queue and print it + s = queue.front(); + cout << s << " "; + queue.pop_front(); + + // Get all adjacent vertices of the dequeued + // vertex s. If a adjacent has not been visited, + // then mark it visited and enqueue it + for (auto adjecent: adj[s]) + { + if (!visited[adjecent]) + { + visited[adjecent] = true; + queue.push_back(adjecent); + } + } + } +} + +// Driver program to test methods of graph class +int main() +{ + // Create a graph given in the above diagram + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + + cout << "Following is Breadth First Traversal " + << "(starting from vertex 2) \n"; + g.BFS(2); + + return 0; +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..30b9f91 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,53 @@ +# Contributing to Programming_HacktoberFest-2023 + +Welcome to Programming_Hactober23 ! We appreciate your interest in contributing. Before you get started, please read and follow these guidelines. + +## Prerequisites + +Prior to beginning your contributions, please ensure the following: + +- Possess a GitHub Account. +- Complete your contributor registration on the [Hacktoberfest 2023 website.](https://hacktoberfest.com/) + +
+ +## How to Contribute + +1. **Look for the issue you wish to solve** on the project's issue tracker. + +2. **Comment on the issue**: Please comment on the issue, indicating your interest in working on it. You can use a comment like: "Please assign me this issue." + + + **Note**: + + - If the assigned contributor takes longer than expected to work on the issue, the maintainer may reassign the issue to the next person in the comment queue. + +
+ +## Commit your changes: + +- Make your changes or additions to the code, documentation, or any other improvements. + +- Test your changes thoroughly. + +## Contribution Guidelines + +- Please ensure your code and contributions align with the project's purpose and goals. + +- Follow the coding style and guidelines of the project. + +- Keep your commits and pull requests concise and clearly describe the changes you've made. + +- Do not submit spammy, trivial, or duplicate pull requests. Quality contributions are highly encouraged. + +
+ +## Questions and Support + +- If you have questions or need support related to your contributions or the project, feel free to tag and ask your doubts to the repository maintainer. + +- Thank you for contributing to Programming_HacktoberFest-2023! Your contributions are greatly appreciated. + +
+ + Happy contributing! πŸš€ diff --git a/FindUniqueElement.cpp b/FindUniqueElement.cpp new file mode 100644 index 0000000..e8ae77e --- /dev/null +++ b/FindUniqueElement.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +int main() { + int n; + std::cout << "Enter the number of elements: "; + std::cin >> n; + + std::vector arr(n); + + std::cout << "Enter " << n << " elements: "; + for (int i = 0; i < n; ++i) { + std::cin >> arr[i]; + } + + std::set uniqueElements; + for (int i : arr) { + uniqueElements.insert(i); + } + + std::cout << "Unique elements in the array: "; + for (int element : uniqueElements) { + std::cout << element << " "; + } + std::cout << std::endl; + + return 0; +} diff --git a/Gr8stIntInArr.cpp b/Gr8stIntInArr.cpp new file mode 100644 index 0000000..8c0b0d8 --- /dev/null +++ b/Gr8stIntInArr.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +int findLargestElement(int arr[], int size) { + if (size <= 0) { + // Ha the case of an empty array or invalid size + return -1; + } + + int largest = arr[0]; // Assuming the first element is the largest + + for (int i = 1; i < size; i++) { + if (arr[i] > largest) { + largest = arr[i]; // Updating largest if current element is greater + } + } + + return largest; +} + +int main() { + int size; + + cout << "Enter the size of the array: "; + cin >> size; + + if (size <= 0) { + cout << "Invalid array size." <> arr[i]; + } + + int largest = findLargestElement(arr, size); + + if (largest != -1) { + cout << "The largest element in the array is: " << largest < +#include + +int kadanesAlgorithm(const std::vector& arr) { + int maxEndingHere = arr[0]; + int maxSoFar = arr[0]; + + for (int i = 1; i < arr.size(); ++i) { + maxEndingHere = std::max(arr[i], maxEndingHere + arr[i]); + maxSoFar = std::max(maxSoFar, maxEndingHere); + } + + return maxSoFar; +} + +int main() { + std::vector arr = {1, -3, 2, 1, -1}; + + int maxSubarraySum = kadanesAlgorithm(arr); + + std::cout << "Maximum subarray sum: " << maxSubarraySum << std::endl; + + return 0; +} diff --git a/MaxSubarray.cpp b/MaxSubarray.cpp new file mode 100644 index 0000000..0280476 --- /dev/null +++ b/MaxSubarray.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +// function for kadane's algorithm +static int kadane(int Array[], int n) { + int max_sum = 0; + int current_sum = 0; + + for(int i=0; i 496. Next Greater Element I +import java.util.*; +class Hactober { + public static int[] nextGreaterElement(int[] nums1, int[] nums2) { + HashMap map = new HashMap<>(); + Stack s = new Stack<>(); + for(int i:nums2){ + while(!s.isEmpty()&&s.peek()