📌  相关文章
📜  检查是否可以通过相邻对的增量使圆形阵列的所有元素相等

📅  最后修改于: 2021-09-04 08:07:39             🧑  作者: Mango

给定一个大小为N的圆形数组arr[] 任务是检查是否可以通过将相邻元素对增加1来使圆形数组的所有数组元素相等。

例子:

处理方法:解决这个问题,可以观察到由要递增的元素组成的两个索引,一个是偶数,另一个是奇数。因此,如果我们增加偶数索引元素的值,那么奇数索引元素也会增加。因此,只有奇数索引元素和偶数索引元素之和相等,才能使所有数组元素相等。请按照以下步骤解决问题:

  1. 计算所有偶数索引数的总和,即sumEven
  2. 计算所有奇数索引数的总和,即sumOdd
  3. 如果发现sumEvensumOdd相等,则打印“”,否则打印“”。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if all array
// elements can be made equal
bool checkEquall(int arr[], int N)
{
    // Stores the sum of even and
    // odd array elements
    int sumEven = 0, sumOdd = 0;
    for (int i = 0; i < N; i++) {
 
        // If index is odd
        if (i & 1)
            sumOdd += arr[i];
        else
            sumEven += arr[i];
    }
 
    if (sumEven == sumOdd)
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 7, 3, 5, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    if (checkEquall(arr, N))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if all array
// elements can be made equal
static boolean checkEquall(int arr[], int N)
{
     
    // Stores the sum of even and
    // odd array elements
    int sumEven = 0, sumOdd = 0;
     
    for(int i = 0; i < N; i++)
    {
         
        // If index is odd
        if (i % 2 == 1)
            sumOdd += arr[i];
        else
            sumEven += arr[i];
    }
 
    if (sumEven == sumOdd)
        return true;
    else
        return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 7, 3, 5, 7 };
    int N = arr.length;
     
    if (checkEquall(arr, N))
        System.out.print("YES" + "\n");
    else
        System.out.print("NO" + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to implement
# the above approach
 
# Function to check if all array
# elements can be made equal
def checkEquall(arr, N):
 
    # Stores the sum of even and
    # odd array elements
    sumEven, sumOdd = 0, 0
     
    for i in range(N):
 
        # If index is odd
        if (i & 1):
            sumOdd += arr[i]
        else:
            sumEven += arr[i]
     
    if (sumEven == sumOdd):
        return True
    else:
        return False
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 2, 7, 3, 5, 7 ]
    N = len(arr)
     
    if (checkEquall(arr, N)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to check if all array
// elements can be made equal
static bool checkEquall(int []arr, int N)
{
     
    // Stores the sum of even and
    // odd array elements
    int sumEven = 0, sumOdd = 0;
     
    for(int i = 0; i < N; i++)
    {
         
        // If index is odd
        if (i % 2 == 1)
            sumOdd += arr[i];
        else
            sumEven += arr[i];
    }
 
    if (sumEven == sumOdd)
        return true;
    else
        return false;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 7, 3, 5, 7 };
    int N = arr.Length;
     
    if (checkEquall(arr, N))
        Console.Write("YES" + "\n");
    else
        Console.Write("NO" + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
YES

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live