📌  相关文章
📜  生成一个N长度的数组,其总和等于给定数组的相同索引元素的绝对差之和的两倍

📅  最后修改于: 2021-05-13 22:36:57             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是构造满足以下条件的大小为N的数组brr []

  • 在数组brr []的每对连续元素中,一个元素必须被另一个元素整除,即brr [i]必须被brr [i + 1]整除,反之亦然。
  • 数组brr []中的每个i元素都必须满足brr [i]> = arr [i] / 2
  • 数组arr []的元素之和必须大于或等于2 *Σabs(arr [i] – brr [i])

例子:

方法:该想法基于以下观察:

请按照以下步骤解决问题:

  • 初始化一个数组brr [] ,以存储满足给定条件的元素。
  • 遍历数组arr [] 。对于每个i元素,找到小于或等于arr [i]的最接近2的幂并将其存储在brr [i]中
  • 最后,打印数组brr []

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to construct an array
// with given conditions
void constructArray(int arr[], int N)
{
    int brr[N] = { 0 };
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        int K = log(arr[i]) / log(2);
 
        // Stores closest power of 2
        // less than or equal to arr[i]
        int R = pow(2, K);
 
        // Stores R into brr[i]
        brr[i] = R;
    }
 
    // Print array elements
    for (int i = 0; i < N; i++) {
        cout << brr[i] << " ";
    }
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 11, 5, 7, 3, 2 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    constructArray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to conan array
// with given conditions
static void constructArray(int arr[], int N)
{
    int brr[] = new int[N];
     
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
        int K = (int)(Math.log(arr[i]) /
                      Math.log(2));
         
        // Stores closest power of 2
        // less than or equal to arr[i]
        int R = (int)Math.pow(2, K);
         
        // Stores R into brr[i]
        brr[i] = R;
    }
     
    // Print array elements
    for(int i = 0; i < N; i++)
    {
        System.out.print(brr[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 11, 5, 7, 3, 2 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    constructArray(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
from math import log
 
# Function to construct an array
# with given conditions
def constructArray(arr, N):
    brr = [0]*N
 
    # Traverse the array arr[]
    for i in range(N):
        K = int(log(arr[i])/log(2))
 
        # Stores closest power of 2
        # less than or equal to arr[i]
        R = pow(2, K)
 
        # Stores R into brr[i]
        brr[i] = R
 
    # Prarray elements
    for i in range(N):
        print(brr[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
   
  # Given array
    arr = [11, 5, 7, 3, 2]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    constructArray(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach   
using System;   
      
class GFG{   
      
// Function to construct an array   
// with given conditions   
static void constructArray(int []arr, int N)   
{   
    int[] brr = new int[N];   
    Array.Clear(brr, 0, brr.Length);   
     
    // Traverse the array arr[]   
    for(int i = 0; i < N; i++)
    {   
        int K = (int)(Math.Log(arr[i]) /
                      Math.Log(2));   
                       
        // Stores closest power of 2   
        // less than or equal to arr[i]   
        int R = (int)Math.Pow(2, K);   
         
        // Stores R into brr[i]   
        brr[i] = R;   
    }   
      
    // Print array elements   
    for(int i = 0; i < N; i++)
    {
        Console.Write(brr[i] + " ");   
    }   
}   
      
// Driver Code   
public static void Main()   
{   
     
    // Given array   
    int []arr = { 11, 5, 7, 3, 2 };   
      
    // Size of the array   
    int N = arr.Length;   
      
    // Function Call   
    constructArray(arr, N);   
}   
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
8 4 4 2 2

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