`{"id":"f87d6f41-65d4-4322-a1ee-717ff409ed61","name":"Majority Element General","description":"1. Given an array of size 'N' and an element K.\r\n2. Task is to find all elements that appears more than N/K times in array.\r\n3. Return these elements in an ArrayList in sorted order.\r\n","inputFormat":"N = 8\r\narr[] = [3, 1, 2, 2, 1, 2, 3, 3]\r\nk = 4\r\nNote : Input is managed for you.\r\n","outputFormat":"[2, 3]\r\nNote : Output is managed for you.","constraints":"1 &lt;= N &lt;= 10^4\r\n1 &lt;= a[i] &lt;= 10^6\r\n1 &lt;= k &lt;= N\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid majorityElement(vector<int>& arr, int k, vector<int>& res){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int k;\n cin>>k;\n vector<int> res;\n majorityElement(arr, k, res);\n cout<<\"[\";\n for(int i=0; i<res.size(); i++){\n cout<<res[i];\n if(i<res.size()-1){\n cout<<\", \";\n }\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> majorityElement(int[] arr, int k) {\r\n // write yout 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 int[] arr = new int[n];\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n int k = scn.nextInt();\r\n ArrayList<Integer> res = majorityElement(arr, k);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n3 1 2 2 1 2 3 3\r\n4","sampleOutput":"[2, 3]","questionVideo":"","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":"97456301-d349-40b5-8ad4-d1bd2027c248","name":"Majority Element General","slug":"majority-element-general","type":1}],"next":{"id":"739ea9af-839d-43e2-8caf-28a731e7dace","name":"Majority Element General","type":3,"slug":"majority-element-general"},"prev":{"id":"d899c25e-f471-4575-aacd-ebf012a749b5","name":"Wiggle Sort 2 MCQ","type":0,"slug":"wiggle-sort-2-mcq"}}}`

# Majority Element General

1. Given an array of size 'N' and an element K. 2. Task is to find all elements that appears more than N/K times in array. 3. Return these elements in an ArrayList in sorted order.

`{"id":"f87d6f41-65d4-4322-a1ee-717ff409ed61","name":"Majority Element General","description":"1. Given an array of size 'N' and an element K.\r\n2. Task is to find all elements that appears more than N/K times in array.\r\n3. Return these elements in an ArrayList in sorted order.\r\n","inputFormat":"N = 8\r\narr[] = [3, 1, 2, 2, 1, 2, 3, 3]\r\nk = 4\r\nNote : Input is managed for you.\r\n","outputFormat":"[2, 3]\r\nNote : Output is managed for you.","constraints":"1 &lt;= N &lt;= 10^4\r\n1 &lt;= a[i] &lt;= 10^6\r\n1 &lt;= k &lt;= N\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid majorityElement(vector<int>& arr, int k, vector<int>& res){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int k;\n cin>>k;\n vector<int> res;\n majorityElement(arr, k, res);\n cout<<\"[\";\n for(int i=0; i<res.size(); i++){\n cout<<res[i];\n if(i<res.size()-1){\n cout<<\", \";\n }\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> majorityElement(int[] arr, int k) {\r\n // write yout 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 int[] arr = new int[n];\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n int k = scn.nextInt();\r\n ArrayList<Integer> res = majorityElement(arr, k);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n3 1 2 2 1 2 3 3\r\n4","sampleOutput":"[2, 3]","questionVideo":"","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":"97456301-d349-40b5-8ad4-d1bd2027c248","name":"Majority Element General","slug":"majority-element-general","type":1}],"next":{"id":"739ea9af-839d-43e2-8caf-28a731e7dace","name":"Majority Element General","type":3,"slug":"majority-element-general"},"prev":{"id":"d899c25e-f471-4575-aacd-ebf012a749b5","name":"Wiggle Sort 2 MCQ","type":0,"slug":"wiggle-sort-2-mcq"}}}`

Editor

# Majority Element General

easy

1. Given an array of size 'N' and an element K. 2. Task is to find all elements that appears more than N/K times in array. 3. Return these elements in an ArrayList in sorted order.

## Constraints

1 <= N <= 10^4 1 <= a[i] <= 10^6 1 <= k <= N

## Format

### Input

N = 8 arr[] = [3, 1, 2, 2, 1, 2, 3, 3] k = 4 Note : Input is managed for you.

### Output

[2, 3] Note : 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;}8 3 1 2 2 1 2 3 3 4```

### 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;}[2, 3]`

Discussions

Show Discussion

Related Resources