📜  0 和 1 的圆形阵列中的多数元素

📅  最后修改于: 2021-10-28 01:35:46             🧑  作者: Mango

给定一个仅包含 0 和 1 的圆形数组,大小为 n,其中 n = p*q(p 和 q 都是奇数整数)。任务是检查是否有一种方法可以在应用以下操作后使 1 占多数:

  1. 将圆形数组分成 p 个大小为 q 的子数组。
  2. 在每个子数组中,占多数的数将存储到数组 B 中。
  3. 现在,如果 1 在数组 B 中占多数,则称 1 占多数。

注意:如果数字出现的次数超过数组大小的一半,则该数字在数组中占多数。
例子:

方法:

  1. 首先,遍历循环数组并计算 p 个子数组(大小为 q)中每个子数组中 1 的总数。
  2. 将此数字存储到另一个数组(大小为 p)中。
  3. 如果在这种情况下,1 占多数,则打印 yes。
  4. 否则,通过移动前一个集合的索引 1 单位来增加或减少它来获取另一个集合,并仅跟踪给定集合中的新索引并更新数组中 1 的数量。
  5. 重复第 2 步和第 3 步。

如果在此之前没有找到 1 的多数,我们将重复 q 次,答案将是 NO,否则(如前面的示例案例)答案将是 1。
因为最多,我们可以重复这个过程 q 次,每次我们只跟踪 p 个子阵列中的每个元素中的两个元素。
解释:
在给定的示例 1 中,我们将圆形子数组划分为 [indices] => {1, 2, 3}, {4, 5, 6}, {7, 8, 9} 并将 1 的数量存储在另一个数组中,它们是[1, 2, 1] 分别在子数组中。
在示例 1 中通过增加 1 个单位来获取另一个集合,因此集合将是 {2, 3, 4}, {5, 6, 7}, {8, 9, 1} 现在在集合 1 中唯一的变化是包含元素 4 和删除对于元素 1,我们将只跟踪这些,因此更新的 1 数将为 2、2、0。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check if 1 is the majority
// element or not
void majority(bool a[], int p, int q, int size)
{
    // assuming starting and ending index of 1st subarray
    int start = 0, ends = q;
 
    // to store majority of p
    int arr[p];
 
    // subarrays each of size q ;
    int k = 0;
 
    // Loop to calculate total number
    // of 1's in subarray which will get
    // stored in array arr[]
    while (k < p) {
        int one = 0;
        for (int j = start; j < ends; j++) {
            if (a[j] == 1) {
                one++;
            }
        }
 
        // starting index of next subarray
        start = ends;
 
        // ending index of next subarray
        ends = ends + q;
 
        // storing 1's
        arr[k] = one;
        k++;
    }
 
    start = 0;
    ends = q;
 
    // variable to keep a check
    // if 1 is in majority or not
    bool found = 0;
 
    // In this case, we are repeating
    // the task of calculating
    // total number of 1's backward
    while (ends > 0) {
 
        // to store the total number of 1's
        int dist_one = 0;
 
        // Check if 1 is in majority in
        // this subarray
        for (int i = 0; i < p; i++)
            if (arr[i] > q / 2)
                dist_one++;
 
        // If 1 is in majority return
        if (dist_one > p / 2) {
            found = 1;
            cout << "Yes" << endl;
 
            return;
        }
 
        // shifting starting index of
        // subarray by 1 unit leftwards
        start--;
 
        // shifting ending index of
        // subarray by 1 unit leftwards
        ends--;
 
        // to ensure it is a valid index
        // ( array is circular) -1 index means
        // last index of a circular array
        if (start < 0)
            start = size + start;
 
        int st = start, en = ends, l = 0;
 
        // now to track changes occur
        // due to shifting of the subarray
        while (en < size) {
 
            if (a[st % size] != a[en % size]) {
 
                // st refers to starting index of
                // new subarray and en refers to
                // last element of same subarray
                // but in previous iteration
                if (a[st % size] == 1)
                    arr[l]++;
 
                else
                    arr[l]--;
            }
            l++;
 
            // now repeating the same
            // for other subarrays too
            st = (st + q);
            en = en + q;
        }
    }
 
    if (found == 0) {
        cout << "No" << endl;
    }
}
 
// Driver code
int main()
{
    int p = 3, q = 3;
    int n = p * q;
 
    bool a[] = { 0, 0, 1, 1, 0, 1, 1, 0, 0 };
 
    // circular array of given size
    majority(a, p, q, n);
 
    return 0;
}


Java
// Java implementation of above approach
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
// Function to check if 1 is the majority
// element or not
static void majority(int a[], int p, int q, int size)
{
    // assuming starting and ending index of 1st subarray
    int start = 0, ends = q;
  
    // to store majority of p
    int []arr = new int[p];
  
    // subarrays each of size q ;
    int k = 0;
  
    // Loop to calculate total number
    // of 1's in subarray which will get
    // stored in array arr[]
    while (k < p) {
        int one = 0;
        for (int j = start; j < ends; j++) {
            if (a[j] == 1) {
                one++;
            }
        }
  
        // starting index of next subarray
        start = ends;
  
        // ending index of next subarray
        ends = ends + q;
  
        // storing 1's
        arr[k] = one;
        k++;
    }
  
    start = 0;
    ends = q;
  
    // variable to keep a check
    // if 1 is in majority or not
    boolean  found = false;
  
    // In this case, we are repeating
    // the task of calculating
    // total number of 1's backward
    while (ends > 0) {
  
        // to store the total number of 1's
        int dist_one = 0;
  
        // Check if 1 is in majority in
        // this subarray
        for (int i = 0; i < p; i++)
            if (arr[i] > q / 2)
                dist_one++;
  
        // If 1 is in majority return
        if (dist_one > p / 2) {
            found = true;
            System.out.println( "Yes" );
  
            return;
        }
  
        // shifting starting index of
        // subarray by 1 unit leftwards
        start--;
  
        // shifting ending index of
        // subarray by 1 unit leftwards
        ends--;
  
        // to ensure it is a valid index
        // ( array is circular) -1 index means
        // last index of a circular array
        if (start < 0)
            start = size + start;
  
        int st = start, en = ends,l = 0;
  
        // now to track changes occur
        // due to shifting of the subarray
        while (en < size) {
  
            if (a[st % size] != a[en % size]) {
  
                // st refers to starting index of
                // new subarray and en refers to
                // last element of same subarray
                // but in previous iteration
                if (a[st % size] == 1)
                    arr[l]++;
  
                else
                    arr[l]--;
            }
            l++;
  
            // now repeating the same
            // for other subarrays too
            st = (st + q);
            en = en + q;
        }
    }
  
    if (found == false ) {
        System.out.println( "No" );
    }
}
  
// Driver code
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 };
  
    // circular array of given size
    majority(a, p, q, n);
  
     
}
}


Python3
# Python3 implementation of
# above approach
 
# Function to check if 1 is
# the majority element or not
def majority(a, p, q, size) :
 
    # assuming starting and
    # ending index of 1st subarray
    start = 0
    ends = q
 
    # to store arr = []
    arr = [None] * p
     
    # subarrays each of size q
    k = 0
     
    # Loop to calculate total number
    # of 1's in subarray which
    # will get stored in array arr
    while (k < p):
        one = 0
        for j in range(start, ends):
            if (a[j] == 1):
                one = one + 1
             
        # starting index of
        # next subarray
        start = ends
 
        # ending index of next
        # subarray
        ends = ends + q
 
        # storing 1's
        arr[k] = one
        k = k + 1
     
    start = 0
    ends = q
 
    # variable to keep a check
    # if 1 is in majority or not
    found = 0
 
    # In this case, we are
    # repeating the task of
    # calculating total number
    # of 1's backward
    while (ends > 0) :
 
        # to store the total
        # number of 1's
        dist_one = 0
  
        # Check if 1 is in majority
        # in this subarray
        for i in range(0, p):
            if (arr[i] > q / 2):
                dist_one = dist_one + 1
 
        # If 1 is in majority return
        if (dist_one > p / 2) :
            found = 1
            print("Yes")
            return
 
        # shifting starting index of
        # subarray by 1 unit leftwards
        start = start - 1
 
        # shifting ending index
        # of subarray by 1 unit
        # leftwards
        ends = ends - 1
 
        # to ensure it is a valid
        # index( array is circular) -1
        # index means last index of
        # a circular array
        if (start < 0):
            start = size + start
 
        st = start
        en = ends
        l = 0
 
        # now to track changes occur
        # due to shifting of the
        # subarray
        while (en < size) :
 
            if (a[st % size] != a[en % size]) :
 
                # st refers to starting index of
                # new subarray and en refers to
                # last element of same subarray
                # but in previous iteration
                if (a[st % size] == 1):
                    arr[l] = arr[l] + 1
 
                else:
                    arr[l] = arr[l] - 1
             
            l = l + 1
 
            # now repeating the same
            # for other subarrays too
            st = (st + q)
            en = en + q
         
    if (found == 0) :
        print("No")
     
# Driver code
p = 3
q = 3
n = p * q
 
a = [ 0, 0, 1, 1, 0, 1, 1, 0, 0 ]
 
# circular array of given size
majority(a, p, q, n)
 
# This code is contributed
# by Yatin Gupta


C#
// C# implementation of above approach
using System;
 
class GFG
{
// Function to check if 1 is the
// majority element or not
public static void majority(int[] a, int p,
                            int q, int size)
{
    // assuming starting and ending
    // index of 1st subarray
    int start = 0, ends = q;
 
    // to store majority of p
    int[] arr = new int[p];
 
    // subarrays each of size q ;
    int k = 0;
 
    // Loop to calculate total number
    // of 1's in subarray which will
    // get stored in array arr[]
    while (k < p)
    {
        int one = 0;
        for (int j = start; j < ends; j++)
        {
            if (a[j] == 1)
            {
                one++;
            }
        }
 
        // starting index of next subarray
        start = ends;
 
        // ending index of next subarray
        ends = ends + q;
 
        // storing 1's
        arr[k] = one;
        k++;
    }
 
    start = 0;
    ends = q;
 
    // variable to keep a check
    // if 1 is in majority or not
    bool found = false;
 
    // In this case, we are repeating
    // the task of calculating
    // total number of 1's backward
    while (ends > 0)
    {
 
        // to store the total number of 1's
        int dist_one = 0;
 
        // Check if 1 is in majority in
        // this subarray
        for (int i = 0; i < p; i++)
        {
            if (arr[i] > q / 2)
            {
                dist_one++;
            }
        }
 
        // If 1 is in majority return
        if (dist_one > p / 2)
        {
            found = true;
            Console.WriteLine("Yes");
 
            return;
        }
 
        // shifting starting index of
        // subarray by 1 unit leftwards
        start--;
 
        // shifting ending index of
        // subarray by 1 unit leftwards
        ends--;
 
        // to ensure it is a valid index
        // ( array is circular) -1 index means
        // last index of a circular array
        if (start < 0)
        {
            start = size + start;
        }
 
        int st = start, en = ends, l = 0;
 
        // now to track changes occur
        // due to shifting of the subarray
        while (en < size)
        {
 
            if (a[st % size] != a[en % size])
            {
 
                // st refers to starting index of
                // new subarray and en refers to
                // last element of same subarray
                // but in previous iteration
                if (a[st % size] == 1)
                {
                    arr[l]++;
                }
 
                else
                {
                    arr[l]--;
                }
            }
            l++;
 
            // now repeating the same
            // for other subarrays too
            st = (st + q);
            en = en + q;
        }
    }
 
    if (found == false)
    {
        Console.WriteLine("No");
    }
}
 
// Driver code
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};
 
    // circular array of given size
    majority(a, p, q, n);
}
}
 
// This code is contributed by Shrikant13


PHP
 0) {
 
        // to store the total number of 1's
        $dist_one = 0;
 
        // Check if 1 is in majority in
        // this subarray
        for ($i = 0; $i < $p; $i++)
            if ($arr[$i] > $q / 2)
                $dist_one++;
 
        // If 1 is in majority return
        if ($dist_one > $p / 2) {
            $found = 1;
            echo  "Yes" ,"\n";
 
            return;
        }
 
        // shifting starting index of
        // subarray by 1 unit leftwards
        $start--;
 
        // shifting ending index of
        // subarray by 1 unit leftwards
        $ends--;
 
        // to ensure it is a valid index
        // ( array is circular) -1 index means
        // last index of a circular array
        if ($start < 0)
            $start = $size + $start;
 
         $st = $start; $en = $ends; $l = 0;
 
        // now to track changes occur
        // due to shifting of the subarray
        while ($en < $size) {
 
            if ($a[$st % $size] != $a[$en % $size]) {
 
                // st refers to starting index of
                // new subarray and en refers to
                // last element of same subarray
                // but in previous iteration
                if ($a[$st % $size] == 1)
                    $arr[$l]++;
 
                else
                    $arr[$l]--;
            }
            $l++;
 
            // now repeating the same
            // for other subarrays too
            $st = ($st + $q);
            $en = $en + $q;
        }
    }
 
    if ($found == 0) {
        echo  "No" ,"\n";
    }
}
 
// Driver code
    $p = 3; $q = 3;
    $n = $p * $q;
 
     $a = array( 0, 0, 1, 1, 0, 1, 1, 0, 0 );
 
    // circular array of given size
    majority($a, $p, $q, $n);
 
 
 
#This Code is Contributed by ajit
?>


Javascript


输出:
Yes

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程