N/3 repeated number in an array with O(1) space
We are given a read only array of n integers. Find any element that appears more than n/3 times in the array in linear time and constant additional space. If no such element exists, return -1.
Examples:
Input : [10, 10, 20, 30, 10, 10] Output : 10 10 occurs 4 times which is more than 6/3. Input : [20, 30, 10, 10, 5, 4, 20, 1, 2] Output : -1
The idea is based on Moore’s Voting algorithm.
We first find two candidates. Then we check if any of these two candidates is actually a majority. Below is the solution for above approach.
C++
// CPP program to find if any element appears // more than n/3. #include <bits/stdc++.h> using namespace std; int appearsNBy3(int arr[], int n) { int count1 = 0, count2 = 0; int first=INT_MAX , second=INT_MAX ; for (int i = 0; i < n; i++) { // if this element is previously seen, // increment count1. if (first == arr[i]) count1++; // if this element is previously seen, // increment count2. else if (second == arr[i]) count2++; else if (count1 == 0) { count1++; first = arr[i]; } else if (count2 == 0) { count2++; second = arr[i]; } // if current element is different from // both the previously seen variables, // decrement both the counts. else { count1--; count2--; } } count1 = 0; count2 = 0; // Again traverse the array and find the // actual counts. for (int i = 0; i < n; i++) { if (arr[i] == first) count1++; else if (arr[i] == second) count2++; } if (count1 > n / 3) return first; if (count2 > n / 3) return second; return -1; } int main() { int arr[] = { 1, 2, 3, 1, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << appearsNBy3(arr, n) << endl; return 0; } |
Java
// Java program to find if any element appears // more than n/3. class GFG { static int appearsNBy3(int arr[], int n) { int count1 = 0, count2 = 0; // take the integers as the maximum // value of integer hoping the integer // would not be present in the array int first = Integer.MAX_VALUE; int second = Integer.MAX_VALUE; for (int i = 1; i < n; i++) { // if this element is previously // seen, increment count1. if (first == arr[i]) count1++; // if this element is previously // seen, increment count2. else if (second == arr[i]) count2++; else if (count1 == 0) { count1++; first = arr[i]; } else if (count2 == 0) { count2++; second = arr[i]; } // if current element is different // from both the previously seen // variables, decrement both the // counts. else { count1--; count2--; } } count1 = 0; count2 = 0; // Again traverse the array and // find the actual counts. for (int i = 0; i < n; i++) { if (arr[i] == first) count1++; else if (arr[i] == second) count2++; } if (count1 > n / 3) return first; if (count2 > n / 3) return second; return -1; } // Driver code public static void main(String args[]) { int arr[] = { 1, 2, 3, 1, 1 }; int n = arr.length; System.out.println(appearsNBy3(arr, n)); } } // This code is contributed by Arnab Kundu |
Python 3
# Python 3 program to find if # any element appears more than # n/3. import sys def appearsNBy3(arr, n): count1 = 0 count2 = 0 first = sys.maxsize second = sys.maxsize for i in range(0, n): # if this element is # previously seen, # increment count1. if (first == arr[i]): count1 += 1 # if this element is # previously seen, # increment count2. elif (second == arr[i]): count2 += 1 elif (count1 == 0): count1 += 1 first = arr[i] elif (count2 == 0): count2 += 1 second = arr[i] # if current element is # different from both # the previously seen # variables, decrement # both the counts. else: count1 -= 1 count2 -= 1 count1 = 0 count2 = 0 # Again traverse the array # and find the actual counts. for i in range(0, n): if (arr[i] == first): count1 += 1 elif (arr[i] == second): count2 += 1 if (count1 > n / 3): return first if (count2 > n / 3): return second return -1 # Driver code arr = [1, 2, 3, 1, 1 ] n = len(arr) print(appearsNBy3(arr, n)) # This code is contributed by # Smitha |
C#
// C# program to find if any element appears // more than n/3. using System; class GFG { static int appearsNBy3(int []arr, int n) { int count1 = 0, count2 = 0; // take the integers as the maximum // value of integer hoping the integer // would not be present in the array int first = int.MaxValue; int second = int.MaxValue; for (int i = 1; i < n; i++) { // if this element is previously // seen, increment count1. if (first == arr[i]) count1++; // if this element is previously // seen, increment count2. else if (second == arr[i]) count2++; else if (count1 == 0) { count1++; first = arr[i]; } else if (count2 == 0) { count2++; second = arr[i]; } // if current element is different // from both the previously seen // variables, decrement both the // counts. else { count1--; count2--; } } count1 = 0; count2 = 0; // Again traverse the array and // find the actual counts. for (int i = 0; i < n; i++) { if (arr[i] == first) count1++; else if (arr[i] == second) count2++; } if (count1 > n / 3) return first; if (count2 > n / 3) return second; return -1; } // Driver code static public void Main(String []args) { int []arr = { 1, 2, 3, 1, 1 }; int n = arr. Length; Console.WriteLine(appearsNBy3(arr, n)); } } // This code is contributed by Arnab Kundu |
PHP
<?php // PHP program to find if any element appears // more than n/3. function appearsNBy3( $arr, $n) { $count1 = 0; $count2 = 0; $first = PHP_INT_MAX ; $second = PHP_INT_MAX ; for ( $i = 0; $i < $n; $i++) { // if this element is previously seen, // increment count1. if ($first == $arr[$i]) $count1++; // if this element is previously seen, // increment count2. else if ($second == $arr[$i]) $count2++; else if ($count1 == 0) { $count1++; $first = $arr[$i]; } else if ($count2 == 0) { $count2++; $second = $arr[$i]; } // if current element is different from // both the previously seen variables, // decrement both the counts. else { $count1--; $count2--; } } $count1 = 0; $count2 = 0; // Again traverse the array and find the // actual counts. for ($i = 0; $i < $n; $i++) { if ($arr[$i] == $first) $count1++; else if ($arr[$i] == $second) $count2++; } if ($count1 > $n / 3) return $first; if ($count2 > $n / 3) return $second; return -1; } // Driver code $arr = array( 1, 2, 3, 1, 1 ); $n = count($arr); echo appearsNBy3($arr, $n) ; // This code is contributed by anuj_67. ?> |
1
Recommended Posts:
- Find top three repeated in array
- Kth smallest element in the array using constant space when array can't be modified
- Smallest element in an array that is repeated exactly 'k' times.
- Largest element in the array that is repeated exactly k times
- Maximum subarray sum in an array created after repeated concatenation
- Minimum array size after repeated replacement of even sum pair with sum
- Count of only repeated element in a sorted array of consecutive elements
- Find maximum array sum after making all elements same with repeated subtraction
- Minimum number of times A has to be repeated such that B is a substring of it
- A product array puzzle | Set 2 (O(1) Space)
- Duplicates in an array in O(n) and by using O(1) extra space | Set-2
- Shuffle array {a1, a2, .. an, b1, b2, .. bn} as {a1, b1, a2, b2, a3, b3, ……, an, bn} without using extra space
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
- Find duplicate in an array in O(n) and by using O(1) extra space
- Duplicates in an array in O(n) time and by using O(1) extra space | Set-3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



