Given an array arr[] of size N, the task is to construct an array brr[] of size N that satisfies the following conditions:
- In every pair of consecutive elements in the array brr[], one element must be divisible by the other, i.e. brr[i] must be divisible by brr[i + 1] or vice-versa.
- Every ith element in the array brr[] must satisfy brr[i] >= arr[i] / 2.
- The sum of elements of the array arr[] must be greater than or equal to 2 * ?abs(arr[i] – brr[i]).
Examples:
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 }
Approach: The idea is based on the following observation:
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]).
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void constructArray( int arr[], int N)
{
int brr[N] = { 0 };
for ( int i = 0; i < N; i++) {
int K = log (arr[i]) / log (2);
int R = pow (2, K);
brr[i] = R;
}
for ( int i = 0; i < N; i++) {
cout << brr[i] << " " ;
}
}
int main()
{
int arr[] = { 11, 5, 7, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
constructArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void constructArray( int arr[], int N)
{
int brr[] = new int [N];
for ( int i = 0 ; i < N; i++)
{
int K = ( int )(Math.log(arr[i]) /
Math.log( 2 ));
int R = ( int )Math.pow( 2 , K);
brr[i] = R;
}
for ( int i = 0 ; i < N; i++)
{
System.out.print(brr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 11 , 5 , 7 , 3 , 2 };
int N = arr.length;
constructArray(arr, N);
}
}
|
Python3
from math import log
def constructArray(arr, N):
brr = [ 0 ] * N
for i in range (N):
K = int (log(arr[i]) / log( 2 ))
R = pow ( 2 , K)
brr[i] = R
for i in range (N):
print (brr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 11 , 5 , 7 , 3 , 2 ]
N = len (arr)
constructArray(arr, N)
|
C#
using System;
class GFG{
static void constructArray( int []arr, int N)
{
int [] brr = new int [N];
Array.Clear(brr, 0, brr.Length);
for ( int i = 0; i < N; i++)
{
int K = ( int )(Math.Log(arr[i]) /
Math.Log(2));
int R = ( int )Math.Pow(2, K);
brr[i] = R;
}
for ( int i = 0; i < N; i++)
{
Console.Write(brr[i] + " " );
}
}
public static void Main()
{
int []arr = { 11, 5, 7, 3, 2 };
int N = arr.Length;
constructArray(arr, N);
}
}
|
Javascript
<script>
function constructArray(arr , N) {
var brr = Array(N).fill(0);
for (i = 0; i < N; i++) {
var K = parseInt( (Math.log(arr[i]) / Math.log(2)));
var R = parseInt( Math.pow(2, K));
brr[i] = R;
}
for (i = 0; i < N; i++) {
document.write(brr[i] + " " );
}
}
var arr = [ 11, 5, 7, 3, 2 ];
var N = arr.length;
constructArray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!