📌  相关文章
📜  在使用给定条件生成的数组中找到最大的元素

📅  最后修改于: 2021-05-05 01:20:04             🧑  作者: Mango

给定整数N (2≤N≤1e6) ,任务是查找数组arr []的大小为N + 1的最大元素,该元素基于以下步骤生成:

  • arr [0] = 0
  • arr [1] = 1
  • 当2≤2 * i≤N时arr [2 * i] = arr [i]
  • 当2≤2 * i + 1≤N时arr [2 * i + 1] = Arr [i] + Arr [i +1]

例子:

方法:解决问题的方法是根据给定的规则集生成所有数组元素,然后在其中找到最大值。使用以下步骤迭代查找每个数组元素:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to generate the
// required array
vector findArray(int n)
{
    // Stores the array
    vector Arr(n + 1);
 
    // Base case
    Arr[0] = 0;
    Arr[1] = 1;
 
    // Iterate over the indices
    for (int i = 2; i <= n; i++) {
 
        // If current index is even
        if (i % 2 == 0) {
 
            Arr[i] = Arr[i / 2];
        }
        // Otherwise
        else {
 
            Arr[i]
                = Arr[(i - 1) / 2]
                  + Arr[(i - 1) / 2 + 1];
        }
    }
 
    return Arr;
}
 
// Function to find and return
// the maximum array element
int maxElement(int n)
{
    // If n is 0
    if (n == 0)
        return 0;
 
    // If n is 1
    if (n == 1)
        return 1;
 
    // Generates the required array
    vector Arr = findArray(n);
 
    // Return maximum element of Arr
    return *max_element(
        Arr.begin(), Arr.end());
}
 
// Driver Code
int main()
{
    int N = 7;
    cout << maxElement(N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
import java.util.Arrays;
 
class GFG{
 
// Function to generate the
// required array and to find
// and return the maximum array
// element
static int[] findArray(int n)
{
     
    // Stores the array
    int Arr[] = new int[n + 1];
     
    // Base case
    Arr[0] = 0;
    Arr[1] = 1;
 
    // Iterate over the indices
    for(int i = 2; i <= n; i++)
    {
         
        // If current index is even
        if (i % 2 == 0)
        {
            Arr[i] = Arr[i / 2];
        }
         
        // Otherwise
        else
        {
            Arr[i] = Arr[(i - 1) / 2] +
                     Arr[(i - 1) / 2 + 1];
        }
    }
    return Arr;
}
 
// Function to find and return
// the maximum array element
static int maxElement(int n)
{
     
    // If n is 0
    if (n == 0)
        return 0;
 
    // If n is 1
    if (n == 1)
        return 1;
 
    // Generates the required array
    int[] Arr = findArray(n);
 
    // Return maximum element of Arr
    int ans = Integer.MIN_VALUE;
    for(int i = 0; i < n; i++)
    {
        ans = Math.max(ans, Arr[i]);
    }
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 7;
     
    System.out.println(maxElement(N));
}
}
 
// This code is contributed by ananyadixit8


Python3
# Python3 program to implement
# the above approach
 
# Function to generate the
# required array
def findArray(n):
     
    # Stores the array
    Arr = [0] * (n + 1)
     
    # Base case
    Arr[1] = 1
     
    # Iterate over the indices
    for i in range(2, n + 1):
         
        # If current index is even
        if (i % 2 == 0):
            Arr[i] = Arr[i // 2]
             
        # Otherwise
        else:
            Arr[i] = (Arr[(i - 1) // 2] +
                      Arr[(i - 1) // 2 + 1])
 
    return Arr
 
# Function to find and return
# the maximum array element
def maxElement(n):
     
    # If n is 0
    if (n == 0):
        return 0
         
    # If n is 1
    if (n == 1):
        return 1
         
    # Generates the required array
    Arr = findArray(n)
 
    # Return maximum element of Arr
    return max(Arr)
     
# Driver Code
if __name__ == "__main__" :
 
    N = 7
     
    print(maxElement(N))
 
# This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to generate the
// required array and to find
// and return the maximum array
// element
static int[] findArray(int n)
{
     
    // Stores the array
    int []Arr = new int[n + 1];
     
    // Base case
    Arr[0] = 0;
    Arr[1] = 1;
 
    // Iterate over the indices
    for(int i = 2; i <= n; i++)
    {
         
        // If current index is even
        if (i % 2 == 0)
        {
            Arr[i] = Arr[i / 2];
        }
         
        // Otherwise
        else
        {
            Arr[i] = Arr[(i - 1) / 2] +
                     Arr[(i - 1) / 2 + 1];
        }
    }
    return Arr;
}
 
// Function to find and return
// the maximum array element
static int maxElement(int n)
{
     
    // If n is 0
    if (n == 0)
        return 0;
 
    // If n is 1
    if (n == 1)
        return 1;
 
    // Generates the required array
    int[] Arr = findArray(n);
 
    // Return maximum element of Arr
    int ans = int.MinValue;
    for(int i = 0; i < n; i++)
    {
        ans = Math.Max(ans, Arr[i]);
    }
    return ans;
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 7;   
    Console.WriteLine(maxElement(N));
}
}
 
// This code is contributed by shikhasingrajput


输出:
3

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