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

📅  最后修改于: 2021-04-17 19:09:18             🧑  作者: 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.


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

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