📌  相关文章
📜  生成具有前一个元素和下一个元素的按位与的数组

📅  最后修改于: 2022-05-13 01:57:49.008000             🧑  作者: Mango

生成具有前一个元素和下一个元素的按位与的数组

给定一个包含N个元素的整数数组arr[] ,任务是生成另一个数组,该数组具有前一个元素和下一个元素的(按位)与,但有以下例外。

  1. 第一个元素是第一个和第二个元素的按位与。
  2. 最后一个元素是最后一个和倒数第二个元素的按位与。

例子:

方法:新数组的第一个和第二个元素可以分别计算为arr[0] & arr[1]arr[N – 1] & arr[N – 2] 。其余元素可以计算为arr[i – 1] & arr[i + 1]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to generate the array that
// satisfies the given condition
void generateArr(int arr[], int n)
{
 
    // If there is only a single element
    // in the array
    if (n == 1) {
        cout << arr[0];
        return;
    }
 
    // To store the generated array
    int barr[n];
 
    // First element
    barr[0] = arr[0] & arr[1];
 
    // Last element
    barr[n - 1] = arr[n - 1] & arr[n - 2];
 
    // Rest of the elements
    for (int i = 1; i < n - 1; i++)
        barr[i] = arr[i - 1] & arr[i + 1];
 
    // Print the generated array
    for (int i = 0; i < n; i++)
        cout << barr[i] << " ";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    generateArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java .io.*;
 
class GFG
{
static void generateArr(int[] arr, int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // store current value of arr[0]
    // and update it
    int prev = arr[0];
    arr[0] = arr[0] & arr[1];
 
    // Update rest of the array elements
    for (int i = 1; i < n - 1; i++)
    {
        // Store current value of
        // next interation
        int curr = arr[i];
 
        // Update current value using
        // previous value
        arr[i] = prev & arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev & arr[n - 1];
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int n = arr.length;
 
    generateArr(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
}
 
// This code is contributed by Nikhil


Python3
# Python3 implementation of the approach
 
# Function to generate the array that
# satisfies the given condition
def generateArr(arr, n):
     
    # If there is only a single element
    # in the array
    if (n == 1):
        print(arr[0]);
        return;
 
    # To store the generated array
    barr = [0] * n;
 
    # First element
    barr[0] = arr[0] & arr[1];
 
    # Last element
    barr[n - 1] = arr[n - 1] & arr[n - 2];
 
    # Rest of the elements
    for i in range(1, n - 1):
        barr[i] = arr[i - 1] & arr[i + 1];
 
    # Print the generated array
    for i in range(n):
        print(barr[i], end = " ");
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6];
    n = len(arr);
 
    generateArr(arr, n);
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
class GFG
{
static void generateArr(int[] arr, int n)
{
    // Nothing to do when array size is 1
    if (n <= 1)
        return;
 
    // store current value of arr[0]
    // and update it
    int prev = arr[0];
    arr[0] = arr[0] & arr[1];
 
    // Update rest of the array elements
    for (int i = 1; i < n - 1; i++)
    {
        // Store current value of
        // next interation
        int curr = arr[i];
 
        // Update current value using
        // previous value
        arr[i] = prev & arr[i + 1];
 
        // Update previous value
        prev = curr;
    }
 
    // Update last array element separately
    arr[n - 1] = prev & arr[n - 1];
}
 
// Driver Code
static public void Main ()
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int n = arr.Length;
 
    generateArr(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
}
 
// This code is contributed by ajit.


Javascript


输出:
0 1 0 1 4 4

时间复杂度: O(N)