From 815373a32b064da7dcb6a170d3436a6c87b68391 Mon Sep 17 00:00:00 2001 From: Pratyusha Thumiki Date: Fri, 23 Oct 2020 09:46:05 +0530 Subject: [PATCH] Added C++ Programming Files --- leetcode/BFS.cpp | 48 ++++++++++++++++++++++++ leetcode/DFS.cpp | 43 +++++++++++++++++++++ leetcode/LengthOfSubarrayWithZeroSum.cpp | 44 ++++++++++++++++++++++ leetcode/SortArrayAccToSetBits.cpp | 38 +++++++++++++++++++ leetcode/dijkstra.cpp | 46 +++++++++++++++++++++++ 5 files changed, 219 insertions(+) create mode 100644 leetcode/BFS.cpp create mode 100644 leetcode/DFS.cpp create mode 100644 leetcode/LengthOfSubarrayWithZeroSum.cpp create mode 100644 leetcode/SortArrayAccToSetBits.cpp create mode 100644 leetcode/dijkstra.cpp diff --git a/leetcode/BFS.cpp b/leetcode/BFS.cpp new file mode 100644 index 0000000..c023151 --- /dev/null +++ b/leetcode/BFS.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; +class Graph{ + int v; + list *adj; + public: Graph(int v); + void add_edge(int v,int num); + void bfs(int source); +}; +Graph::Graph(int v){ + this->v=v; + adj=new list[v]; +} +void Graph::add_edge(int v,int num){ + adj[v].push_back(num); +} +void Graph::bfs(int source){ + bool *visited=new bool[v]; + for(int i=0;i queue; + visited[source]=true; + queue.push_back(source); + list::iterator it; + while(!queue.empty()){ + source=queue.front(); +cout< +#include +using namespace std; +class Graph{ + int n; + list *adj; + public: Graph(int n); + void dfsUtil(int v,bool visited[]); + void add_edge(int v,int num); + void dfs(int v); +}; +Graph::Graph(int n){ + this->n=n; + adj=new list[n]; +} +void Graph::add_edge(int v,int num){ + adj[v].push_back(num); +} +void Graph::dfsUtil(int v,bool visited[]){ + visited[v]=true; + cout<::iterator it; + for(it=adj[v].begin();it!=adj[v].end();it++) + if(!visited[*it]) + dfsUtil(*it,visited); +} +void Graph::dfs(int v){ + bool *visited=new bool[n]; + for(int i=0;i +using namespace std; + +// Returns Length of the required subarray +int maxLen(int arr[], int n) +{ + // Map to store the previous sums + unordered_map presum; + + int sum = 0; // Initialize the sum of elements + int max_len = 0; // Initialize result + + // Traverse through the given array + for (int i = 0; i < n; i++) { + // Add current element to sum + sum += arr[i]; + + if (arr[i] == 0 && max_len == 0) + max_len = 1; + if (sum == 0) + max_len = i + 1; + + // Look for this sum in Hash table + if (presum.find(sum) != presum.end()) { + // If this sum is seen before, then update max_len + max_len = max(max_len, i - presum[sum]); + } + else { + // Else insert this sum with index in hash table + presum[sum] = i; + } + } + + return max_len; +} +int main() +{ + int arr[] = { 15, -2, 2, -8, 1, 7, 10, 23 }; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Length of the longest 0 sum subarray is " + << maxLen(arr, n); + + return 0; +} \ No newline at end of file diff --git a/leetcode/SortArrayAccToSetBits.cpp b/leetcode/SortArrayAccToSetBits.cpp new file mode 100644 index 0000000..d336d5c --- /dev/null +++ b/leetcode/SortArrayAccToSetBits.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +void sortArr(int arr[], int n) +{ + multimap map; + + for (int i = 0; i < n; i++) { + int count = 0; + int k = arr[i]; + + // Counting no of setBits in arr[i] + while (k) { + k = k & k - 1; + count++; + } + + // The count is subtracted from 32 + // because the result needs + // to be in descending order + map.insert(make_pair(32 - count, arr[i])); + } + + // Printing the numbers in descending + // order of set bit count + for (auto it = map.begin(); it != map.end(); it++) { + cout << (*it).second << " "; + } +} +int main() +{ + int arr[] = { 5, 2, 3, 9, 4, 6, 7, 15, 32 }; + int n = sizeof(arr) / sizeof(arr[0]); + + sortArr(arr, n); + + return 0; +} \ No newline at end of file diff --git a/leetcode/dijkstra.cpp b/leetcode/dijkstra.cpp new file mode 100644 index 0000000..ef8e9f3 --- /dev/null +++ b/leetcode/dijkstra.cpp @@ -0,0 +1,46 @@ +#define v 9 +void min_distance(int dist[v],bool trset[v]){ + int min_dis=INT_MAX, min_index; + for(int i=0;i