给定大小为N的数组arr [] ,任务是构造满足以下条件的大小为N的数组brr [] :
- 在数组brr []的每对连续元素中,一个元素必须被另一个元素整除,即brr [i]必须被brr [i + 1]整除,反之亦然。
- 数组brr []中的每个第i个元素都必须满足brr [i]> = arr [i] / 2 。
- 数组arr []的元素之和必须大于或等于2 *Σabs(arr [i] – brr [i]) 。
例子:
Input: arr[] = { 11, 5, 7, 3, 2 }
Output: 8 4 4 2 2
Explanation:
abs(11 – 8) + abs(5 – 4) + abs(7 – 4) + abs(3 – 2) + abs(2 – 2) = 8
arr[0] + arr[1] + … + arr[4] = 28
2 * 8 <= 28 and for every ith element brr[i] >= arr[i] / 2.
Therefore, one of the possible values of brr[] are 8 4 4 2 2.
Input: arr[] = { 11, 7, 5 }
Output: { 8, 4, 4 }
方法:该想法基于以下观察:
If brr[i] is the nearest power of 2 and is smaller than or equal to arr[i], then brr[i] must be greater than or equal to arr[i] / 2 and also the sum of elements of the array, arr[] must be greater than or equal to 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)