📌  相关文章
📜  检查每个索引 i 是否有一个索引 j 使得两个方向上的元素之和相等

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

检查每个索引 i 是否有一个索引 j 使得两个方向上的元素之和相等

给定一个大小为 N 的圆形数组。任务是检查对于从 0 到 N-1 的每个索引 i,是否存在一个不等于 i 的索引 j,使得顺时针方向的所有数字的总和从i 到 j 等于从 i 到 j 逆时针方向的所有数字之和。
例子:

方法:

  • 当 N 为奇数时,答案总是“否”,因为不可能为每个索引 i 找到索引 j。
  • 如果 N 是偶数,则检查每个索引 i 的相反元素是否完全相同,则答案为“是”。
  • 如果任何索引的相反元素,即a[(i+(n/2))] 不等于 a[i] ,那么答案将是“否”。

以下是上述方法的实现:

C++
// C++ program to check if the
// number lies in given range
#include 
using namespace std;
 
// Function that returns the maximum element.
bool check(int a[], int n)
{
 
    // check for odd
    if (n % 2 == 1)
        return false;
 
    // check if the opposite element is same
    // as a[i]
    for (int i = 0; i < n / 2; i++) {
        if (a[i] != a[i + (n / 2)])
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int a[] = { 1, 4, 1, 4 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    if (check(a, n))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
//Java program to check if the
//number lies in given range
 
public class GFG {
 
    //Function that returns the maximum element.
    static boolean check(int a[], int n)
    {
 
     // check for odd
     if (n % 2 == 1)
         return false;
 
     // check if the opposite element is same
     // as a[i]
     for (int i = 0; i < n / 2; i++) {
         if (a[i] != a[i + (n / 2)])
             return false;
     }
 
     return true;
    }
 
    //Driver code
    public static void main(String[] args) {
         
        int a[] = { 1, 4, 1, 4 };
 
         int n = a.length;
 
         if (check(a, n))
             System.out.println("YES");
         else
             System.out.println("NO");
    }
}


Python 3
# Python 3 Program  to check if the
# number lies in given range
 
# Function that returns the maximum element.
def check(a, n) :
 
    # check for odd
    if n % 2 == 1:
        return False
 
    # check if the opposite element is same
    # as a[i]
    for i in range(n//2) :
        if a[i] != a[i + (n//2)]:
            return False
 
    return True
 
# Driver Code
if __name__ == "__main__" :
    a = [ 1, 4, 1, 4]
 
    n = len(a)
 
    if check(a, n) :
        print("YES")
    else :
        print("NO")
     
# This code is contributed by ANKITRAI1


C#
// C# program to check if the
// number lies in given range
 
class GFG
{
 
// Function that returns the
// maximum element.
static bool check(int[] a, int n)
{
 
// check for odd
if (n % 2 == 1)
    return false;
 
// check if the opposite
// element is same as a[i]
for (int i = 0; i < (int)n / 2; i++)
{
    if (a[i] != a[i + (int)(n / 2)])
        return false;
}
 
return true;
}
 
// Driver code
public static void Main()
{
    int[] a = new int[]{ 1, 4, 1, 4 };
 
    int n = a.Length;
 
    if (check(a, n))
        System.Console.WriteLine("YES");
    else
        System.Console.WriteLine("NO");
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
YES

时间复杂度: O(n)
辅助空间: O(1)