From ddca3fc732946a7ecbe90fb026b36caa4c35b003 Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:09:11 +0530 Subject: [PATCH 1/6] Print Adjacency List. --- .vscode/settings.json | 3 +++ Answer55.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Answer55.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..691a8f6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "Disabled" +} \ No newline at end of file diff --git a/Answer55.cpp b/Answer55.cpp new file mode 100644 index 0000000..ecf5597 --- /dev/null +++ b/Answer55.cpp @@ -0,0 +1,10 @@ +// It is the same structure but by using the in-built list STL data structures of C++ + +class Graph{ + int numVertices; + list *adjLists; + + public: + Graph(int V); + void addEdge(int src, int dest); +}; \ No newline at end of file From 8540958780fc40d595b187f53fdd3a128ca36aac Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:14:25 +0530 Subject: [PATCH 2/6] Delete settings.json --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 691a8f6..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "C_Cpp.errorSquiggles": "Disabled" -} \ No newline at end of file From c93f0fa59a983c44616f4d953b56fc1e9526dbb8 Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:27:42 +0530 Subject: [PATCH 3/6] Answer --- Answer15.cpp | 22 ++++++++++++++++++++++ Answer16.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Answer15.cpp create mode 100644 Answer16.cpp diff --git a/Answer15.cpp b/Answer15.cpp new file mode 100644 index 0000000..c1f7dc9 --- /dev/null +++ b/Answer15.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int majorityElement(vector& nums) { + int count = 0; + int candidate = 0; + + for (int num : nums) { + if (count == 0) { + candidate = num; + } + if(num==candidate) count += 1; + else count -= 1; + } + + return candidate; + } +}; + +//Input Format: N = 3, nums[] = {3,2,3} +//Result: 3 + +// When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution. \ No newline at end of file diff --git a/Answer16.cpp b/Answer16.cpp new file mode 100644 index 0000000..00753fa --- /dev/null +++ b/Answer16.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; +vector < int > majorityElement(int arr[], int n) { + vector < int > ans; + for (int i = 0; i < n; i++) { + int cnt = 1; + for (int j = i + 1; j < n; j++) { + if (arr[j] == arr[i]) + cnt++; + } + + if (cnt > (n / 3)) + ans.push_back(arr[i]); + } + + return ans; +} + +int main() { + int arr[] = {1,2,2,3,2}; + vector majority; + majority = majorityElement(arr, 5); + cout << "The majority element is" << endl; + set < int > s(majority.begin(), majority.end()); + for (auto it: s) { + cout << it << " "; + } +} + +//Output: The majority element is 2 \ No newline at end of file From c88b9ae6153189846e4e9196eb53817d8b5c746e Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:49:39 +0530 Subject: [PATCH 4/6] Delete Answer15.cpp --- Answer15.cpp | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Answer15.cpp diff --git a/Answer15.cpp b/Answer15.cpp deleted file mode 100644 index c1f7dc9..0000000 --- a/Answer15.cpp +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - int majorityElement(vector& nums) { - int count = 0; - int candidate = 0; - - for (int num : nums) { - if (count == 0) { - candidate = num; - } - if(num==candidate) count += 1; - else count -= 1; - } - - return candidate; - } -}; - -//Input Format: N = 3, nums[] = {3,2,3} -//Result: 3 - -// When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution. \ No newline at end of file From 9de90b41dafffcb6bcbe6ba29cc9449ec74cd361 Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:51:12 +0530 Subject: [PATCH 5/6] Delete Answer16.cpp --- Answer16.cpp | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 Answer16.cpp diff --git a/Answer16.cpp b/Answer16.cpp deleted file mode 100644 index 00753fa..0000000 --- a/Answer16.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include - -using namespace std; -vector < int > majorityElement(int arr[], int n) { - vector < int > ans; - for (int i = 0; i < n; i++) { - int cnt = 1; - for (int j = i + 1; j < n; j++) { - if (arr[j] == arr[i]) - cnt++; - } - - if (cnt > (n / 3)) - ans.push_back(arr[i]); - } - - return ans; -} - -int main() { - int arr[] = {1,2,2,3,2}; - vector majority; - majority = majorityElement(arr, 5); - cout << "The majority element is" << endl; - set < int > s(majority.begin(), majority.end()); - for (auto it: s) { - cout << it << " "; - } -} - -//Output: The majority element is 2 \ No newline at end of file From 040c2d3c42175f4ca5b2dbe1dbf35f85bd57f55f Mon Sep 17 00:00:00 2001 From: Yash Bhujbal <100800234+FsocietyVoid@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:54:33 +0530 Subject: [PATCH 6/6] Update Answer15.cpp --- Answer15.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Answer15.cpp b/Answer15.cpp index c1f7dc9..036bdec 100644 --- a/Answer15.cpp +++ b/Answer15.cpp @@ -17,6 +17,6 @@ class Solution { }; //Input Format: N = 3, nums[] = {3,2,3} -//Result: 3 +//Result: 3 // When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution. \ No newline at end of file