给定整数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]
例子:
Input: N = 7
Output: 3
Explanation: Constructing the array based on the given rules:
- Arr[0] = 0
- Arr[1] = 1
- Arr[(1 * 2) = 2] = Arr[1] = 1
- Arr[(1 * 2) + 1 = 3] = Arr[1] + Arr[2] = 1 + 1 = 2
- Arr[(2 * 2) = 4] = Arr[2] = 1
- Arr[(2 * 2) + 1 = 5] = Arr[2] + Arr[3] = 1 + 2 = 3
- Arr[(3 * 2) = 6] = Arr[3] = 2
- Arr[(3 * 2) + 1 = 7] = Arr[3] + Arr[4] = 2 + 1 = 3
Therefore, the array constructed is {0, 1, 1, 2, 1, 3, 2, 3}. The maximum element present in the array is 3.
Input: N = 2
Output: 1
Explanation: According to the given rules, the maximum among Arr[0], Arr[1], and Arr[2] is 1.
方法:解决问题的方法是根据给定的规则集生成所有数组元素,然后在其中找到最大值。使用以下步骤迭代查找每个数组元素:
If i is even: Arr[i] = Arr[i/2]
Otherwise, if i is odd: Arr[i] = Arr[(i – 1)/2] + Arr[(i – 1)/2 +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)