Majority element in a circular array of 0’s and 1’s
Given a circular array containing only 0’s and 1’s, of size n where n = p*q (p and q are both odd integers). The task is to check if there is a way such that 1 will be in majority after applying the following operations:
- Divide circular array into p subarrays each of size q.
- In each subarray, the number which is in majority will get stored into array B.
- Now, 1 will said to be in majority if it is in majority into array B.
Note: A number is in majority in an array if it occurs more than half times of the size of an array.
Examples:
Input: p = 3, q = 3, array[] = {0, 0, 1, 1, 0, 1, 1, 0, 0}
Output: Yes
Assume index of the array from 1 to N, Since the array is circular so index N and 1 will be adjacent.
Divide this circular array into subarray in this way :-
{2, 3, 4}, {5, 6, 7} and {8, 9, 1}. [These are the index of elements]
In {2, 3, 4}, 1 is in majority,
in {5, 6, 7}, again 1 is in majority and
In {8, 9, 1}, 0 is in majority.
Now insert 1, 1, 0 into array B so array B = {1, 1, 0}
In array B, 1 is the majority element so print Yes.
Input: p = 3, q = 3, array[] = {1, 0, 0, 1, 1, 0, 1, 0, 0}
Output: No
No matter how you divide this circular subarray,
1 will not be in majority. Hence, the answer is No.
Approach:
- First of all, iterate over the circular array and count the total number of 1’s in each of p subarray(of size q).
- Store this number into another array (of size p).
- If in this case, 1 is in majority then, print yes.
- Else take another set by moving the previous set’s index 1 unit either increasing or decreasing it, and keep track of only those indices which are new in given set and update number of 1’s in the array.
- Repeat the 2nd and 3rd step.
We will repeat q times if there is no majority of 1 found until that, the answer would be NO, else ( like in the previous example case ) the answer would be 1.
Since at the maximum, we can repeat this process q times and each time we are tracking only two elements in each of p subarray.
Explanation:
In given example-1, we will divide circular subarray as [indices] => {1, 2, 3}, {4, 5, 6}, {7, 8, 9} and store number of 1’s in another array which are [1, 2, 1] in subarrays respectively.
Taking another set in example 1 by increasing 1 unit so set will be {2, 3, 4}, {5, 6, 7}, {8, 9, 1} now in set 1 only change is inclusion of element 4 and removal of element 1, we will only track these so updated number of 1’s will be 2, 2, 0.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void majority( bool a[], int p, int q, int size)
{
int start = 0, ends = q;
int arr[p];
int k = 0;
while (k < p) {
int one = 0;
for ( int j = start; j < ends; j++) {
if (a[j] == 1) {
one++;
}
}
start = ends;
ends = ends + q;
arr[k] = one;
k++;
}
start = 0;
ends = q;
bool found = 0;
while (ends > 0) {
int dist_one = 0;
for ( int i = 0; i < p; i++)
if (arr[i] > q / 2)
dist_one++;
if (dist_one > p / 2) {
found = 1;
cout << "Yes" << endl;
return ;
}
start--;
ends--;
if (start < 0)
start = size + start;
int st = start, en = ends, l = 0;
while (en < size) {
if (a[st % size] != a[en % size]) {
if (a[st % size] == 1)
arr[l]++;
else
arr[l]--;
}
l++;
st = (st + q);
en = en + q;
}
}
if (found == 0) {
cout << "No" << endl;
}
}
int main()
{
int p = 3, q = 3;
int n = p * q;
bool a[] = { 0, 0, 1, 1, 0, 1, 1, 0, 0 };
majority(a, p, q, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static void majority( int a[], int p, int q, int size)
{
int start = 0 , ends = q;
int []arr = new int [p];
int k = 0 ;
while (k < p) {
int one = 0 ;
for ( int j = start; j < ends; j++) {
if (a[j] == 1 ) {
one++;
}
}
start = ends;
ends = ends + q;
arr[k] = one;
k++;
}
start = 0 ;
ends = q;
boolean found = false ;
while (ends > 0 ) {
int dist_one = 0 ;
for ( int i = 0 ; i < p; i++)
if (arr[i] > q / 2 )
dist_one++;
if (dist_one > p / 2 ) {
found = true ;
System.out.println( "Yes" );
return ;
}
start--;
ends--;
if (start < 0 )
start = size + start;
int st = start, en = ends,l = 0 ;
while (en < size) {
if (a[st % size] != a[en % size]) {
if (a[st % size] == 1 )
arr[l]++;
else
arr[l]--;
}
l++;
st = (st + q);
en = en + q;
}
}
if (found == false ) {
System.out.println( "No" );
}
}
public static void main(String args[])
{
int p = 3 , q = 3 ;
int n = p * q;
int a[] = { 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 };
majority(a, p, q, n);
}
}
|
Python3
def majority(a, p, q, size) :
start = 0
ends = q
arr = [ None ] * p
k = 0
while (k < p):
one = 0
for j in range (start, ends):
if (a[j] = = 1 ):
one = one + 1
start = ends
ends = ends + q
arr[k] = one
k = k + 1
start = 0
ends = q
found = 0
while (ends > 0 ) :
dist_one = 0
for i in range ( 0 , p):
if (arr[i] > q / 2 ):
dist_one = dist_one + 1
if (dist_one > p / 2 ) :
found = 1
print ( "Yes" )
return
start = start - 1
ends = ends - 1
if (start < 0 ):
start = size + start
st = start
en = ends
l = 0
while (en < size) :
if (a[st % size] ! = a[en % size]) :
if (a[st % size] = = 1 ):
arr[l] = arr[l] + 1
else :
arr[l] = arr[l] - 1
l = l + 1
st = (st + q)
en = en + q
if (found = = 0 ) :
print ( "No" )
p = 3
q = 3
n = p * q
a = [ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 ]
majority(a, p, q, n)
|
C#
using System;
class GFG
{
public static void majority( int [] a, int p,
int q, int size)
{
int start = 0, ends = q;
int [] arr = new int [p];
int k = 0;
while (k < p)
{
int one = 0;
for ( int j = start; j < ends; j++)
{
if (a[j] == 1)
{
one++;
}
}
start = ends;
ends = ends + q;
arr[k] = one;
k++;
}
start = 0;
ends = q;
bool found = false ;
while (ends > 0)
{
int dist_one = 0;
for ( int i = 0; i < p; i++)
{
if (arr[i] > q / 2)
{
dist_one++;
}
}
if (dist_one > p / 2)
{
found = true ;
Console.WriteLine( "Yes" );
return ;
}
start--;
ends--;
if (start < 0)
{
start = size + start;
}
int st = start, en = ends, l = 0;
while (en < size)
{
if (a[st % size] != a[en % size])
{
if (a[st % size] == 1)
{
arr[l]++;
}
else
{
arr[l]--;
}
}
l++;
st = (st + q);
en = en + q;
}
}
if (found == false )
{
Console.WriteLine( "No" );
}
}
public static void Main( string [] args)
{
int p = 3, q = 3;
int n = p * q;
int [] a = new int [] {0, 0, 1, 1,
0, 1, 1, 0, 0};
majority(a, p, q, n);
}
}
|
PHP
<?php
function majority( $a , $p , $q , $size )
{
$start = 0; $ends = $q ;
$arr = array (0);
$k = 0;
while ( $k < $p ) {
$one = 0;
for ( $j = $start ; $j < $ends ; $j ++) {
if ( $a [ $j ] == 1) {
$one ++;
}
}
$start = $ends ;
$ends = $ends + $q ;
$arr [ $k ] = $one ;
$k ++;
}
$start = 0;
$ends = $q ;
$found = 0;
while ( $ends > 0) {
$dist_one = 0;
for ( $i = 0; $i < $p ; $i ++)
if ( $arr [ $i ] > $q / 2)
$dist_one ++;
if ( $dist_one > $p / 2) {
$found = 1;
echo "Yes" , "\n" ;
return ;
}
$start --;
$ends --;
if ( $start < 0)
$start = $size + $start ;
$st = $start ; $en = $ends ; $l = 0;
while ( $en < $size ) {
if ( $a [ $st % $size ] != $a [ $en % $size ]) {
if ( $a [ $st % $size ] == 1)
$arr [ $l ]++;
else
$arr [ $l ]--;
}
$l ++;
$st = ( $st + $q );
$en = $en + $q ;
}
}
if ( $found == 0) {
echo "No" , "\n" ;
}
}
$p = 3; $q = 3;
$n = $p * $q ;
$a = array ( 0, 0, 1, 1, 0, 1, 1, 0, 0 );
majority( $a , $p , $q , $n );
#This Code is Contributed by ajit
?>
|
Javascript
<script>
function majority(a, p, q, size)
{
let start = 0, ends = q;
let arr = new Array(p);
arr.fill(0);
let k = 0;
while (k < p)
{
let one = 0;
for (let j = start; j < ends; j++)
{
if (a[j] == 1)
{
one++;
}
}
start = ends;
ends = ends + q;
arr[k] = one;
k++;
}
start = 0;
ends = q;
let found = false ;
while (ends > 0)
{
let dist_one = 0;
for (let i = 0; i < p; i++)
{
if (arr[i] > parseInt(q / 2, 10))
{
dist_one++;
}
}
if (dist_one > parseInt(p / 2, 10))
{
found = true ;
document.write( "Yes" );
return ;
}
start--;
ends--;
if (start < 0)
{
start = size + start;
}
let st = start, en = ends, l = 0;
while (en < size)
{
if (a[st % size] != a[en % size])
{
if (a[st % size] == 1)
{
arr[l]++;
}
else
{
arr[l]--;
}
}
l++;
st = (st + q);
en = en + q;
}
}
if (found == false )
{
document.write( "No" );
}
}
let p = 3, q = 3;
let n = p * q;
let a = [0, 0, 1, 1, 0, 1, 1, 0, 0];
majority(a, p, q, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(p*q)
- Auxiliary Space: O(1)
Last Updated :
31 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...