📌  相关文章
📜  按照给定操作打印N个不同的数字

📅  最后修改于: 2021-05-14 01:24:00             🧑  作者: Mango

给定整数N始终为偶数,我们的任务是按照给定条件打印N个不同的数字:

  • 前半数是偶数,而后半数是奇数
  • 前半个数字的元素之和与后半个数字的元素之和应相等

如果上述条件满足,则打印数组,否则输出“ -1”。
例子:

方法:
为了解决上述问题,我们必须观察到整数N必须为4的倍数

  • 我们知道前N / 2个偶数之和将为偶数,因此,如果其他N / 2个整数的和也为偶数,则N / 2必须为偶数,因为奇数个奇数整数的和为总是很奇怪。
  • 如果N / 2为偶数,则N为4的倍数,因此,如果n不被4整除,则答案为“ -1”,否则,将存在一个可能的数组。
  • 为了打印阵列,我们将考虑两部分,即前半部分即N / 2个元素将是2的倍数,而另一半将是2-1的倍数。对于数组中的最后一个元素,我们将计算通过应用直接公式N + N / 2-1来获得整数,因为我们应该使两个半的和相等。

下面是上述方法的实现:

C++
// C++ implementation to Print N distinct numbers
#include 
using namespace std;
 
// Function to print the required array
bool printArr(int n)
{
 
    // Check if number is a multiple of 4
    if (n % 4 == 0) {
        // Printing Left Half of the array
        for (int i = 1; i <= n / 2; i++)
            cout << i * 2 << ' ';
 
        // Printing Right Half of the array
        for (int i = 1; i < n / 2; i++)
            cout << i * 2 - 1 << ' ';
        cout << n + n / 2 - 1 << '\n';
    }
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int n = 22;
 
    printArr(n);
 
    return 0;
}


Java
// Java implementation to print N distinct numbers
import java.util.*;
 
class GFG{
 
// Function to print the required array
static void printArr(int n)
{
 
    // Check if number is a multiple of 4
    if(n % 4 == 0)
    {
       // Printing left half of the array
       for(int i = 1; i <= n / 2; i++)
           System.out.print(i * 2 + " ");
 
       // Printing Right Half of the array
       for(int i = 1; i < n / 2; i++)
           System.out.print(i * 2 - 1 + " ");
 
       System.out.println(n + n / 2 - 1);
    }
    else
        System.out.print("-1");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 22;
    printArr(n);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to print
# N distinct numbers
 
# Function to print the required array
def printArr(n):
 
    # Check if number is a multiple of 4
    if (n % 4 == 0):
         
        # Printing Left Half of the array
        for i in range(1, (n / 2) + 1):
            print (i * 2, end = " ")
 
        # Printing Right Half of the array
        for i in range(1, n / 2):
            print (i * 2 - 1, end = " ")
             
        print (n + n / 2 - 1, end = "\n")
 
    else:
        print ("-1")
 
# Driver code
n = 22
printArr(n)
 
# This code is contributed by PratikBasu


C#
// C# implementation to print N distinct numbers
using System;
 
public class GFG{
 
// Function to print the required array
static void printArr(int n)
{
 
    // Check if number is a multiple of 4
    if(n % 4 == 0)
    {
         
    // Printing left half of the array
    for(int i = 1; i <= n / 2; i++)
        Console.Write(i * 2 + " ");
 
    // Printing Right Half of the array
    for(int i = 1; i < n / 2; i++)
        Console.Write(i * 2 - 1 + " ");
 
    Console.WriteLine(n + n / 2 - 1);
    }
     
    else
        Console.Write("-1");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 22;
    printArr(n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
-1

时间复杂度: O(N)