📌  相关文章
📜  生成一个长度为 N 的数组,其具有相等的计数和两个奇偶校验元素的总和

📅  最后修改于: 2021-10-26 05:32:43             🧑  作者: Mango

给定一个整数N (3 ≤ N ≤ 10 5 ) ,任务是生成一个由N 个不同的正元素组成的数组,使得双方元素的计数和总和(即偶数和奇数)相同。如果无法构造这样的数组,则打印-1

例子:

处理方法:按照以下步骤解决问题:

  1. 如果N是奇数或(N / 2)是奇数,则打印-1
  2. 除此以外:
    • 2开始打印N/2 个偶数值并将计数存储在某个变量中,例如SumEven
    • 1开始打印N / 2 – 1 个奇数值,并将总和存储在另一个变量中,比如SumOdd
    • 最后打印SumEven – SumOdd这也是奇数

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Fun dtion to print the
// required sequence
void Print(int N)
{
    if ((N / 2) % 2 == 1
|| (N % 2 == 1)) {
        cout << -1 << endl;
        return;
    }
 
// Stores count of even
// and odd elements
    int CurEven = 2, CurOdd = 1;
 
// Stores sum of even
// and odd elements
    int SumOdd = 0, SumEven = 0;
 
// Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++) {
 
        cout << CurEven << " ";
        SumEven += CurEven;
        CurEven += 2;
    }
 
// Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++) {
        cout << CurOdd << " ";
        SumOdd += CurOdd;
        CurOdd += 2;
    }
 
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    cout << CurOdd << '\n';
}
 
// Driver Code
int main()
{
    int N = 12;
    Print(N);
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG
{
 
  // Fun dtion to print the
  // required sequence
  static void Print(int N)
  {
    if ((N / 2) % 2 == 1 || (N % 2 == 1))
    {
      System.out.print(-1);
      return;
    }
 
    // Stores count of even
    // and odd elements
    int CurEven = 2, CurOdd = 1;
 
    // Stores sum of even
    // and odd elements
    int SumOdd = 0, SumEven = 0;
 
    // Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++)
    {
      System.out.print(CurEven + " ");
      SumEven += CurEven;
      CurEven += 2;
    }
 
    // Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++)
    {
      System.out.print(CurOdd + " ");
      SumOdd += CurOdd;
      CurOdd += 2;
    }
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    System.out.println(CurOdd);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int N = 12;
    Print(N);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python Program to implement
# the above approach
 
# Fun dtion to print the
# required sequence
def Print(N):
    if ((N / 2) % 2 or (N % 2)):
        print(-1)
        return
 
# Stores count of even
# and odd elements
    CurEven = 2
    CurOdd = 1
 
# Stores sum of even
# and odd elements
    SumOdd = 0
    SumEven = 0
 
# Print N / 2 even elements
    for i in range(N // 2):
 
        print(CurEven,end=" ")
        SumEven += CurEven
        CurEven += 2
 
# Print N / 2 - 1 odd elements
    for i in range( (N//2) - 1):
        print(CurOdd, end=" ")
        SumOdd += CurOdd
        CurOdd += 2
 
    CurOdd = SumEven - SumOdd
 
    # Print final odd element
    print(CurOdd)
 
# Driver Code
N = 12
Print(N)
 
# This code is contributed by rohitsingh07052


C#
// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Fun dtion to print the
  // required sequence
  static void Print(int N)
  {
    if ((N / 2) % 2 == 1 || (N % 2 == 1))
    {
      Console.WriteLine(-1);
      return;
    }
 
    // Stores count of even
    // and odd elements
    int CurEven = 2, CurOdd = 1;
 
    // Stores sum of even
    // and odd elements
    int SumOdd = 0, SumEven = 0;
 
    // Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++)
    {
      Console.Write(CurEven + " ");
      SumEven += CurEven;
      CurEven += 2;
    }
 
    // Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++)
    {
      Console.Write(CurOdd + " ");
      SumOdd += CurOdd;
      CurOdd += 2;
    }
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    Console.WriteLine(CurOdd);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 12;
    Print(N);
  }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
2 4 6 8 10 12 1 3 5 7 9 17

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