📜  通过根据给定条件减少最终余数来检查原始数组中是否存在最终余数

📅  最后修改于: 2022-05-13 01:56:04.999000             🧑  作者: Mango

通过根据给定条件减少最终余数来检查原始数组中是否存在最终余数

给定一个自然数数组arr[] ,任务是从前面删除一个元素,从末尾删除一个元素并找到它们的乘积,然后将乘积除以数组的中间元素,并将我们得到的余数附加到数组的末尾并继续相同的操作,直到数组的大小变为 2。

现在,当最后一个数组只包含两个元素时,两个元素的乘积除以原始数组的大小,如果原始数组中存在余数,我们必须打印“YES” ,否则为“NO”

例子:

天真的方法:

  • mid作为数组的中间元素。
  • 从前后弹出元素,取出它的产品,然后将产品除以中间元素。 (从前面弹出元素需要O(N) 时间)。
  • 将余数从后面插入一个数组,继续下去直到数组的大小变为2。
  • 现在将元素的乘积除以原始数组的大小并检查余数是否存在于原始数组中。

以下是实施上述方法的步骤:

  • 将原始数组复制到另一个数组,并对数组进行操作。
  • mid为中间元素,前后 pop 元素,取其乘积除以mid
  • 附加我们从后面分成数组后得到的余数。
  • 重复步骤 2 ,直到数组的长度等于2
  • 最后,取剩余元素的乘积除以n
  • 如果原始数组中存在余数,则打印“YES” ,否则打印“NO”

下面是上述算法的实现。

C++
#include 
using namespace std;
 
int Reduced(vector a, int n)
{
    // copying original array
    vector original_array;
    original_array = a;
 
    // loop till length of array become 2.
    while (a.size() != 2)
    {
 
        // find middle element
        int mid = a.size() / 2;
 
        int mid_ele = a[mid];
 
        // pop element from front and rear
        int start = a[0];
        a.erase(a.begin());
        int end = a[a.size() - 1];
        a.pop_back();
 
        // find remainder
        int rmd = (start * end) % mid_ele;
 
        // append remainder to a
        a.push_back(rmd);
 
        // now since length of array is 2
        // take product and divide it by n
        int remainder = (a[0] * a[1]) % n;
 
        // if remainder is present is original array
        // return 1, else return 0
        for (int i = 0; i < original_array.size(); i++)
        {
            if (original_array[i] == remainder)
            {
                return 1;
            }
        }
    }
    return 0;
}
 
int main()
{
    vector Arr = {2, 3, 4, 8, 5, 7};
    int N = Arr.size();
 
    // calling function Reduced
    int x = Reduced(Arr, N);
 
    // if x = 1 print YES else NO
    if (x)
        cout << ("YES");
    else
        cout << ("NO");
 
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
import java.util.*;
class GFG {
 
  static int Reduced(Vector a, int n)
  {
 
    // copying original array
    Vector original_array = new Vector<>();
    original_array = a;
 
    // loop till length of array become 2.
    while (a.size() != 2) {
 
      // find middle element
      int mid = a.size() / 2;
 
      int mid_ele = a.get(mid);
 
      // pop element from front and rear
      int start = a.get(0);
      a.remove(0);
      int end = a.get(a.size() - 1);
      a.remove(a.size() - 1);
 
      // find remainder
      int rmd = (start * end) % mid_ele;
 
      // append remainder to a
      a.add(rmd);
 
      // now since length of array is 2
      // take product and divide it by n
      int remainder = (a.get(0) * a.get(1)) % n;
 
      // if remainder is present is original array
      // return 1, else return 0
      for (int i = 0; i < original_array.size(); i++) {
        if (original_array.get(i) == remainder) {
          return 1;
        }
      }
    }
    return 0;
  }
 
  public static void main(String[] args)
  {
    int[] arr = { 2, 3, 4, 8, 5, 7 };
    int N = arr.length;
    Vector Arr = new Vector<>();
    for (int i = 0; i < N; i++)
      Arr.add(arr[i]);
 
    // calling function Reduced
    int x = Reduced(Arr, N);
 
    // if x = 1 print YES else NO
    if (x == 1)
      System.out.print("YES");
    else
      System.out.print("NO");
 
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python program to implement above approach
 
def Reduced(a, n):
    # copying original array
    original_array = a[:]
 
    # loop till length of array become 2.
    while len(a) != 2:
 
        # find middle element
        mid = len(a)//2
        mid_ele = a[mid]
 
        # pop element from front and rear
        start = a.pop(0)
        end = a.pop()
 
        # find remainder
        rmd = (start * end) % mid_ele
 
        # append remainder to a
        a.append(rmd)
 
    # now since length of array is 2
    # take product and divide it by n
    remainder = (a[0]*a[1]) % n
 
    # if remainder is present is original array
    # return 1, else return 0
    if remainder in original_array:
        return 1
    return 0
 
 
Arr = [2, 3, 4, 8, 5, 7]
N = len(Arr)
 
# calling function Reduced
x = Reduced(Arr, N)
 
# if x = 1 print YES else NO
if x:
    print("YES")
else:
    print("NO")


C#
using System;
using System.Collections.Generic;
class GFg
{
    static int Reduced(List a, int n)
    {
        // copying original array
        List original_array = new List(a);
 
        // loop till length of array become 2.
        while (a.Count != 2) {
 
            // find middle element
            int mid = a.Count / 2;
 
            int mid_ele = a[mid];
 
            // pop element from front and rear
            int start = a[0];
            a.RemoveAt(0);
            int end = a[a.Count - 1];
            a.RemoveAt(a.Count - 1);
 
            // find remainder
            int rmd = (start * end) % mid_ele;
 
            // append remainder to a
            a.Add(rmd);
 
            // now since length of array is 2
            // take product and divide it by n
            int remainder = (a[0] * a[1]) % n;
 
            // if remainder is present is original array
            // return 1, else return 0
            for (int i = 0; i < original_array.Count; i++) {
                if (original_array[i] == remainder) {
                    return 1;
                }
            }
        }
        return 0;
    }
 
  // Driver code
    public static void Main()
    {
        List Arr
            = new List() { 2, 3, 4, 8, 5, 7 };
        int N = Arr.Count;
 
        // calling function Reduced
        int x = Reduced(Arr, N);
 
        // if x = 1 print YES else NO
        if (x > 0)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
YES

时间复杂度: O(N^2)
辅助空间: O(N)

有效方法(使用两个指针算法

  • 以 start 为0和 end 为n-1并通过将 start 和 end 之和除以 2 找到中间索引。
  • 求运算后的余数,用余数替换最后一个元素
  • 继续执行上述操作,直到 start 等于 end-1。
  • 现在数组将剩下两个元素,取arr的元素并将其乘积除以n并检查余数是否存在于原始数组中。

以下是实施上述方法的步骤:

  • 将原始数组复制到另一个数组,并对数组进行操作。
  • 初始从0开始,以n-1结束
  • 找到中间索引作为 start 和 end 除以2的总和。
  • 通过将开始结束元素相乘除以中间元素来查找余数。
  • 用余数替换 end 元素,从1开始递增,保持 end 与n-1相同。
  • 重复步骤3直到 start 等于end-1
  • 在数组的索引开始和结束处取元素的乘积 并将其除以n
  • 如果原始数组中存在余数,则打印“YES” ,否则打印“NO”