`{"id":"65319546-0b31-4647-a8fc-d25d4b2341b4","name":"Majority Elements - I I","description":"1. Given an integer array of size 'n'.\r\n2. Find all elements that appear more than n / 3 times and return it in an arraylist.\r\n3. Note : solve the problem in linear time and in O(1) space.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= arr.length &lt;= 10^4\r\n2. -10 ^ 9 &lt;= arr[i] &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> majorityElement2(vector<int> &arr) {\n //write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n);\n\n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n\n vector<int> res = majorityElement2(arr);\n \n cout<<\"[\";\n for(int i=0; i<res.size(); ++i){\n if(i!=res.size()-1){\n cout<<res[i]<<\", \";\n continue;\n }\n cout<<res[i];\n }\n cout<<\"]\";\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n public static ArrayList<Integer> majorityElement2(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[] arr = new int[n];\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n ArrayList<Integer> res = majorityElement2(arr);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n3 2 3","sampleOutput":"[3]","questionVideo":"https://www.youtube.com/embed/1QybAZMCYhA","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"3b397bc4-7434-4f59-8076-c64cdcf79f26","name":"Majority Elements - I I","slug":"majority-elements-i-i","type":1}],"next":{"id":"7127952e-5c22-4223-ad84-050ffa220bd8","name":"Majority Elements - I I","type":3,"slug":"majority-elements-i-i"},"prev":{"id":"528b18b0-45f9-4b3a-ae72-f6b21ff4e031","name":"Majority Elements I MCQ","type":0,"slug":"majority-elements-i-mcq"}}}`

Majority Elements - I I

1. Given an integer array of size 'n'. 2. Find all elements that appear more than n / 3 times and return it in an arraylist. 3. Note : solve the problem in linear time and in O(1) space.

`{"id":"65319546-0b31-4647-a8fc-d25d4b2341b4","name":"Majority Elements - I I","description":"1. Given an integer array of size 'n'.\r\n2. Find all elements that appear more than n / 3 times and return it in an arraylist.\r\n3. Note : solve the problem in linear time and in O(1) space.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= arr.length &lt;= 10^4\r\n2. -10 ^ 9 &lt;= arr[i] &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> majorityElement2(vector<int> &arr) {\n //write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n);\n\n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n\n vector<int> res = majorityElement2(arr);\n \n cout<<\"[\";\n for(int i=0; i<res.size(); ++i){\n if(i!=res.size()-1){\n cout<<res[i]<<\", \";\n continue;\n }\n cout<<res[i];\n }\n cout<<\"]\";\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n public static ArrayList<Integer> majorityElement2(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[] arr = new int[n];\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n ArrayList<Integer> res = majorityElement2(arr);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n3 2 3","sampleOutput":"[3]","questionVideo":"https://www.youtube.com/embed/1QybAZMCYhA","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"3b397bc4-7434-4f59-8076-c64cdcf79f26","name":"Majority Elements - I I","slug":"majority-elements-i-i","type":1}],"next":{"id":"7127952e-5c22-4223-ad84-050ffa220bd8","name":"Majority Elements - I I","type":3,"slug":"majority-elements-i-i"},"prev":{"id":"528b18b0-45f9-4b3a-ae72-f6b21ff4e031","name":"Majority Elements I MCQ","type":0,"slug":"majority-elements-i-mcq"}}}`

Editor

Majority Elements - I I

easy

1. Given an integer array of size 'n'. 2. Find all elements that appear more than n / 3 times and return it in an arraylist. 3. Note : solve the problem in linear time and in O(1) space.

Constraints

1. 1 <= arr.length <= 10^4 2. -10 ^ 9 <= arr[i] <= 10^9

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 3 2 3```

Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}[3]`

Question Video

Discussions

Show Discussion

Related Resources