📌  相关文章
📜  生成一个给定大小的数组,该数组具有相等的计数以及奇数和偶数之和

📅  最后修改于: 2021-06-25 23:06:40             🧑  作者: Mango

给定一个整数N ,任务是找到一个长度为N的数组,该数组包含相同数量的奇数和偶数元素,且该数组中的偶数和奇数元素之和相等。
注意:如果没有这样的数组,则打印-1。
例子:

方法:该问题的主要观察结果是,只有数组的长度是4的倍数才能形成具有相等数量的偶数和奇数元素且具有相等总和的数组。下面是步骤说明:

  • 数组的偶数元素是从2开始的自然数的前N / 2个偶数元素。
  • 类似地,(N / 2 – 1)所述阵列的奇数元素是第一(N / 2 – 1)从1开始自然数的奇数元素。
  • 数组的最后一个奇数元素是使数组的偶数和奇数元素之和相等的必需值。
    Last Odd Element = 
       (sum of even elements) - 
       (sum of N/2 - 1 odd elements)

    下面是上述方法的实现:

    C++
    // C++ implementation to find the
    // array containing same count of
    // even and odd elements with equal
    // sum of even and odd elements
      
    #include 
      
    using namespace std;
      
    // Function to find the array such that
    // the array contains the same count
    // of even and odd elements with equal
    // sum of even and odd elements
    void findSolution(int N)
    {
      
        // Length of array which is not
        // divisible by 4 is unable to
        // form such array
        if (N % 4 != 0)
            cout << -1 << "\n";
        else {
            int temp = 0, sum_odd = 0,
                sum_even = 0;
            int result[N] = { 0 };
      
            // Loop to find the resulted
            // array containing the same
            // count of even and odd elements
            for (int i = 0; i < N; i += 2) {
                temp += 2;
      
                result[i + 1] = temp;
                // Find the total sum
                // of even elements
                sum_even += result[i + 1];
      
                result[i] = temp - 1;
                // Find the total sum
                // of odd elements
                sum_odd += result[i];
            }
      
            // Find the difference between the
            // total sum of even and odd elements
            int diff = sum_even - sum_odd;
      
            // The difference will be added
            // in the last odd element
            result[N - 2] += diff;
      
            for (int i = 0; i < N; i++)
                cout << result[i] << " ";
            cout << "\n";
        }
    }
      
    // Driver Code
    int main()
    {
        int N = 8;
        findSolution(N);
        return 0;
    }


    Java
    // Java implementation to find the
    // array containing same count of
    // even and odd elements with equal
    // sum of even and odd elements
      
    class GFG{
      
    // Function to find the array such that
    // the array contains the same count
    // of even and odd elements with equal
    // sum of even and odd elements
    static void findSolution(int N)
    {
      
        // Length of array which is not
        // divisible by 4 is unable to
        // form such array
        if (N % 4 != 0)
            System.out.print(-1 + "\n");
      
        else 
        {
            int temp = 0, sum_odd = 0;
            int sum_even = 0;
            int result[] = new int[N];
      
            // Loop to find the resulted
            // array containing the same
            // count of even and odd elements
            for(int i = 0; i < N; i += 2)
            {
               temp += 2;
               result[i + 1] = temp;
                 
               // Find the total sum
               // of even elements
               sum_even += result[i + 1];
               result[i] = temp - 1;
                 
               // Find the total sum
               // of odd elements
               sum_odd += result[i];
            }
              
            // Find the difference between the
            // total sum of even and odd elements
            int diff = sum_even - sum_odd;
      
            // The difference will be added
            // in the last odd element
            result[N - 2] += diff;
      
            for(int i = 0; i < N; i++)
               System.out.print(result[i] + " ");
            System.out.print("\n");
        }
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int N = 8;
        findSolution(N);
    }
    }
      
    // This code is contributed by Amit Katiyar


    Python3
    # Python3 implementation to find the 
    # array containing same count of 
    # even and odd elements with equal 
    # sum of even and odd elements 
      
    # Function to find the array such that 
    # the array contains the same count 
    # of even and odd elements with equal 
    # sum of even and odd elements 
    def findSolution(N): 
      
        # Length of array which is not 
        # divisible by 4 is unable to 
        # form such array 
        if (N % 4 != 0): 
            print(-1) 
        else: 
            temp = 0
            sum_odd = 0
            sum_even = 0
            result = [0] * N 
      
            # Loop to find the resulted 
            # array containing the same 
            # count of even and odd elements 
            for i in range(0, N, 2): 
                temp += 2
                result[i + 1] = temp 
                  
                # Find the total sum 
                # of even elements 
                sum_even += result[i + 1] 
                result[i] = temp - 1
                  
                # Find the total sum 
                # of odd elements 
                sum_odd += result[i] 
      
            # Find the difference between the 
            # total sum of even and odd elements 
            diff = sum_even - sum_odd 
      
            # The difference will be added 
            # in the last odd element 
            result[N - 2] += diff 
              
            for i in range(N): 
                print(result[i], end = " ") 
            print() 
      
    # Driver Code 
    N = 8; 
    findSolution(N)
          
    # This code is contributed by divyamohan123


    C#
    // C# implementation to find the
    // array containing same count of
    // even and odd elements with equal
    // sum of even and odd elements
    using System;
      
    class GFG{
      
    // Function to find the array such that
    // the array contains the same count
    // of even and odd elements with equal
    // sum of even and odd elements
    static void findSolution(int N)
    {
      
        // Length of array which is not
        // divisible by 4 is unable to
        // form such array
        if (N % 4 != 0)
            Console.Write(-1 + "\n");
      
        else
        {
            int temp = 0, sum_odd = 0;
            int sum_even = 0;
            int []result = new int[N];
      
            // Loop to find the resulted
            // array containing the same
            // count of even and odd elements
            for(int i = 0; i < N; i += 2)
            {
               temp += 2;
               result[i + 1] = temp;
                 
               // Find the total sum
               // of even elements
               sum_even += result[i + 1];
               result[i] = temp - 1;
                 
               // Find the total sum
               // of odd elements
               sum_odd += result[i];
            }
              
            // Find the difference between the
            // total sum of even and odd elements
            int diff = sum_even - sum_odd;
      
            // The difference will be added
            // in the last odd element
            result[N - 2] += diff;
      
            for(int i = 0; i < N; i++)
               Console.Write(result[i] + " ");
            Console.Write("\n");
        }
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 8;
        findSolution(N);
    }
    }
      
    // This code is contributed by Rohit_ranjan


    Javascript


    输出:
    1 2 3 4 5 6 11 8

    性能分析:

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

    如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。