📌  相关文章
📜  二进制圆数组中的最小位更改以达到索引

📅  最后修改于: 2021-04-24 18:35:49             🧑  作者: Mango

给定一个大小为N个元素的二进制圆数组和两个表示圆数组中索引的正整数xy 。任务是检查从索引x到索引y的顺时针或逆时针路径,我们将遇到最小数目的位翻转。如果计数相等,则输出“顺时针”或“逆时针”以及最小位翻转的值。

例子:

Input : arr[] = { 0, 0, 0, 1, 1, 0 }
        x = 0, y = 5
Output : Anti-clockwise 0
The path 0 -> 1 -> 2 -> 3 -> 4 -> 5, we have only 1 value change i.e from index 2 to 3.
The path 0 -> 5 have 0 value change.
So, the answer is Anti-clockwise 0.

Input : s = { 1, 1, 0, 1, 1 }
        x = 2, y = 0
Output : Clockwise 1

想法是通过顺时针旋转一次并存储count1,然后逆时针旋转并存储count2进行检查。然后通过比较count1和count2输出。

如何顺时针或逆时针行驶?
在x> y的数组中将很难沿顺时针方向移动,而在y> x的逆时针方向上将很难相同。因此,我们将给定的二进制数组存储在字符串“ S”中。为了使其呈圆形,我们将S附加到S上,即S = S +S。我们将在x和y中进行调整以顺时针或逆时针移动。
现在,如果y> x并顺时针旋转,则很容易从x迭代到y并计算翻转位的数量。
如果y> x并逆时针旋转,我们将添加| S |。到x,然后从y迭代到x,并计算翻转位的数量。

现在,如果x> y,我们将交换x和y并使用上述方法计算答案。然后输出与结果相反的结果。

要计算翻转位的数量,只需存储索引的当前位,然后检查下一个索引是否与当前位相同。如果是,则不执行其他任何操作,将当前位更改为下一个索引的位,并将最小位增加1。

以下是此方法的实现:

C++
// CPP program to find direction with minimum flips
#include 
using namespace std;
  
// finding which path have minimum flip bit and
// the minimum flip bits
void minimumFlip(string s, int x, int y)
{
    // concatenating given strin to itself,
    // to make it circular
    s = s + s;
  
    // check x is greater than y.
    // marking if output need to
    // be opposite.
    bool isOpposite = false;    
    if (x > y) {
        swap(x, y);
        isOpposite = true;
    }
  
    // iterate Clockwise
    int valClockwise = 0;
    char cur = s[x];
    for (int i = x; i <= y; i++) {
          
        // if current bit is not equal 
        // to next index bit.
        if (s[i] != cur) {
            cur = s[i];
            valClockwise++;
        }
    }
  
    // iterate Anti-Clockwise
    int valAnticlockwise = 0;
    cur = s[y];
    x += s.length();
    for (int i = y; i <= x; i++) {
          
        // if current bit is not equal
        // to next index bit.
        if (s[i] != cur) {
            cur = s[i];
            valAnticlockwise++;
        }
    }
  
    // Finding whether Clockwise or Anti-clockwise 
    // path take minimum flip.
    if (valClockwise <= valAnticlockwise) {
        if (!isOpposite)
            cout << "Clockwise " 
                << valClockwise << endl;
        else
            cout << "Anti-clockwise " 
                 << valAnticlockwise << endl;
    }
    else {
        if (!isOpposite)
            cout << "Anti-clockwise "
                 << valAnticlockwise << endl;
        else
            cout << "Clockwise "
                 << valClockwise << endl;
    }
}
  
// Driven Program
int main()
{
    int x = 0, y = 8;
    string s = "000110";
    minimumFlip(s, x, y);
    return 0;
}


Java
// Java program to find direction
// with minimum flips 
class GFG
{
  
    // finding which path have
    // minimum flip bit and 
    // the minimum flip bits 
    static void minimumFlip(String s,
                            int x, int y)
    {
        // concatenating given strin to  
        // itself, to make it circular 
        s = s + s;
  
        // check x is greater than y. 
        // marking if output need to 
        // be opposite. 
        boolean isOpposite = false;
        if (x > y) 
        {
            swap(x, y);
            isOpposite = true;
        }
  
        // iterate Clockwise 
        int valClockwise = 0;
        char cur = s.charAt(x);
        for (int i = x; i <= y; i++) 
        {
  
            // if current bit is not equal 
            // to next index bit. 
            if (s.charAt(i) != cur)
            {
                cur = s.charAt(i);
                valClockwise++;
            }
        }
  
        // iterate Anti-Clockwise 
        int valAnticlockwise = 0;
        cur = s.charAt(y);
        x += s.length();
        for (int i = y; i < x; i++)
        {
  
            // if current bit is not equal 
            // to next index bit. 
            if (s.charAt(i) != cur) 
            {
                cur = s.charAt(i);
                valAnticlockwise++;
            }
        }
  
        // Finding whether Clockwise
        // or Anti-clockwise path 
        // take minimum flip. 
        if (valClockwise <= valAnticlockwise)
        {
            if (!isOpposite) 
            {
                System.out.println("Clockwise " +
                                    valClockwise);
            } 
            else 
            {
                System.out.println("Anti-clockwise " +
                                    valAnticlockwise);
            }
  
        } 
        else if (!isOpposite) 
        {
            System.out.println("Anti-clockwise " + 
                                valAnticlockwise);
        } 
        else 
        {
            System.out.println("Clockwise " +
                                valClockwise);
        }
    }
  
    static void swap(int a, int b) 
    {
        int c = a;
        a = b;
        b = c;
    }
      
    // Driver code
    public static void main(String[] args) 
    {
        int x = 0, y = 8;
        String s = "000110";
        minimumFlip(s, x, y);
    }
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to find direction
# with minimum flips
  
# finding which path have minimum flip bit 
# and the minimum flip bits
def minimumFlip(s, x, y):
      
    # concatenating given string to itself,
    # to make it circular
    s = s + s
      
    # check x is greater than y.
    # marking if output need to
    # be opposite.
    isOpposite = False
    if (x > y):
        temp = y
        y = x;
        x = temp
        isOpposite = True
  
    # iterate Clockwise
    valClockwise = 0
    cur = s[x]
    for i in range(x, y + 1, 1):
          
        # if current bit is not equal 
        # to next index bit.
        if (s[i] != cur):
            cur = s[i]
            valClockwise += 1
  
    # iterate Anti-Clockwise
    valAnticlockwise = 0
    cur = s[y]
    x += len(s) - 1
    for i in range(y, x + 1, 1):
          
        # if current bit is not equal
        # to next index bit.
        if (s[i] != cur):
            cur = s[i]
            valAnticlockwise += 1
  
    # Finding whether Clockwise or Anti-clockwise 
    # path take minimum flip.
    if (valClockwise <= valAnticlockwise):
        if (isOpposite == False):
            print("Clockwise", valClockwise)
        else:
            print("Anti-clockwise", 
                   valAnticlockwise)
  
    else:
        if (isOpposite == False):
            print("Anti-clockwise", 
                   valAnticlockwise)
  
        else:
            print("Clockwise", valClockwise)
  
# Driver Code
if __name__ == '__main__':
    x = 0
    y = 8
    s = "000110"
    minimumFlip(s, x, y)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find direction
// with minimum flips 
using System;
  
class GFG
{
  
    // finding which path have
    // minimum flip bit and 
    // the minimum flip bits 
    static void minimumFlip(String s,
                            int x, int y)
    {
        // concatenating given strin to 
        // itself, to make it circular 
        s = s + s;
  
        // check x is greater than y. 
        // marking if output need to 
        // be opposite. 
        bool isOpposite = false;
        if (x > y) 
        {
            swap(x, y);
            isOpposite = true;
        }
  
        // iterate Clockwise 
        int valClockwise = 0;
        char cur = s[x];
        for (int i = x; i <= y; i++) 
        {
  
            // if current bit is not equal 
            // to next index bit. 
            if (s[i] != cur)
            {
                cur = s[i];
                valClockwise++;
            }
        }
  
        // iterate Anti-Clockwise 
        int valAnticlockwise = 0;
        cur = s[y];
        x += s.Length;
        for (int i = y; i < x; i++)
        {
  
            // if current bit is not equal 
            // to next index bit. 
            if (s[i] != cur) 
            {
                cur = s[i];
                valAnticlockwise++;
            }
        }
  
        // Finding whether Clockwise
        // or Anti-clockwise path 
        // take minimum flip. 
        if (valClockwise <= valAnticlockwise)
        {
            if (!isOpposite) 
            {
                Console.WriteLine("Clockwise " +
                                    valClockwise);
            } 
            else
            {
                Console.WriteLine("Anti-clockwise " +
                                    valAnticlockwise);
            }
  
        } 
        else if (!isOpposite) 
        {
            Console.WriteLine("Anti-clockwise " + 
                                valAnticlockwise);
        } 
        else
        {
            Console.WriteLine("Clockwise " +
                                valClockwise);
        }
    }
  
    static void swap(int a, int b) 
    {
        int c = a;
        a = b;
        b = c;
    }
      
    // Driver code
    public static void Main(String[] args) 
    {
        int x = 0, y = 8;
        String s = "000110";
        minimumFlip(s, x, y);
    }
}
  
// This code contributed by Rajput-Ji


输出:

Clockwise 2