📌  相关文章
📜  构造一个大小为N的数组,其中奇数元素的总和等于偶数元素的总和

📅  最后修改于: 2021-04-24 23:56:33             🧑  作者: Mango

给定一个始终为偶数的整数N ,任务是创建一个大小为N的数组,其中包含N / 2个偶数N / 2个奇数。数组的所有元素都应该是不同的,并且偶数之和等于奇数之和。如果不存在这样的数组,则打印-1。
例子:

方法:要解决上述问题,第一个观察结果是无法创建大小为N的数组,该大小为2的倍数,但不是4的倍数。因为,如果发生这种情况,则包含奇数的一半的总和将始终为奇数,而包含偶数的另一半的总和将始终为偶数,因此两个半部的总和不能相同。
因此,满足问题陈述的数组的大小N始终应为4的倍数。该方法是首先构造从2开始的N / 2个偶数,这是数组的前半部分。然后从1开始创建数组的另一部分,最后计算最后一个奇数元素,以使两个半数相等。为此,奇数最后一个元素应为(N / 2)– 1 + N。
下面是上述方法的实现:

CPP
// C++ program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
 
#include 
using namespace std;
 
// Function to construct the required array
void arrayConstruct(int N)
{
 
    // To construct first half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        cout << i << " ";
 
    // To construct second half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        cout << i << " ";
 
    // Calculate the last number of second half
    // so as to make both the halves equal
    cout << N - 1 + (N / 2) << endl;
}
 
// Function to construct the required array
void createArray(int N)
{
 
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
 
        // function call to construct array
        arrayConstruct(N);
 
    else
        cout << -1 << endl;
}
 
// Driver code
int main()
{
 
    int N = 8;
 
    createArray(N);
 
    return 0;
}


Java
// Java program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
class GFG{
  
// Function to conthe required array
static void arrayConstruct(int N)
{
  
    // To confirst half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        System.out.print(i+ " ");
  
    // To consecond half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        System.out.print(i+ " ");
  
    // Calculate the last number of second half
    // so as to make both the halves equal
    System.out.print(N - 1 + (N / 2) +"\n");
}
  
// Function to conthe required array
static void createArray(int N)
{
  
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
  
        // function call to conarray
        arrayConstruct(N);
  
    else
        System.out.print(-1 +"\n");
}
  
// Driver code
public static void main(String[] args)
{
    int N = 8;
  
    createArray(N);
}
}
 
// This code is contributed by Princi Singh


Python3
# python3 program to Create an array
# of size N consisting of distinct
# elements where sum of odd elements
# is equal to sum of even elements
 
# Function to construct the required array
def arrayConstruct(N):
 
    # To construct first half,
    # distinct even numbers
    for i in range(2, N + 1, 2):
        print(i,end=" ")
 
    # To construct second half,
    # distinct odd numbers
    for i in range(1, N - 1, 2):
        print(i, end=" ")
 
    # Calculate the last number of second half
    # so as to make both the halves equal
    print(N - 1 + (N // 2))
 
# Function to construct the required array
def createArray(N):
 
    # check if size is multiple of 4
    # then array exist
    if (N % 4 == 0):
 
        # function call to construct array
        arrayConstruct(N)
 
    else:
        cout << -1 << endl
 
# Driver code
if __name__ == '__main__':
 
    N = 8
 
    createArray(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
using System;
 
class GFG{
   
// Function to conthe required array
static void arrayConstruct(int N)
{
   
    // To confirst half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        Console.Write(i + " ");
   
    // To consecond half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        Console.Write(i + " ");
   
    // Calculate the last number of second half
    // so as to make both the halves equal
    Console.Write(N - 1 + (N / 2) +"\n");
}
   
// Function to conthe required array
static void createArray(int N)
{
   
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
   
        // function call to conarray
        arrayConstruct(N);
   
    else
        Console.Write(-1 +"\n");
}
   
// Driver code
public static void Main(String[] args)
{
    int N = 8;
   
    createArray(N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2 4 6 8 1 3 5 11