给定三个整数N , low和high ,任务是找到字典上最大的双音序列,由位于[low, high]范围内的N 个元素组成。如果不可能生成这样的序列,则打印“不可能” 。
例子:
Input: N = 5, low = 2, high = 6
Output: 5 6 5 4 3
Explanation:
The sequence {arr[0], arr[1]} is strictly increasing followed by strictly decreasing sequence of the remaining elements. This sequence is the lexicographically largest possible having all elements in the range [2, 6] and length of this sequence is 5.
Input: N = 10, low = 4, high = 10
Output: 7 8 9 10 9 8 7 6 5 4
方法:思想是在结果序列中找到合适的high索引,然后保持序列中相邻元素之间的差为1 ,这样形成的双音序列是字典序最大的。请按照以下步骤解决问题:
- 初始化大小为N的数组A[]以存储结果序列。
- 初始化变量high_index = -1以将high的索引存储在A[] 中并设置high_index = N – (high – low + 1) 。
- 如果high_index > (N – 1) / 2 ,则剩余的N/2 个元素不能严格按递增顺序放置。所以,打印“不可能”。
- 否则,请执行以下步骤:
- 如果high_index ≤ 0 ,则设置high_index = 1因为在开始时必须有一个严格递增的序列。
- 保持严格递减的序列,与范围[high_index, 0]相差1 ,从值high 开始。
- 保持严格递减的序列与来自范围为1的差[high_index + 1,N – 1]开始的值(高- 1)。
- 完成上述步骤后,打印数组A[] 中的所有元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
void LargestArray(int N, int low, int high)
{
// Store index of highest element
int high_index = N - (high - low + 1);
// If high_index > (N-1)/2, then
// remaining N/2 elements cannot
// be placed in bitonic order
if (high_index > (N - 1) / 2) {
cout << "Not Possible";
return;
}
// If high_index <= 0, then
// set high_index as 1
if (high_index <= 0)
high_index = 1;
// Stores the resultant sequence
int A[N];
// Store the high value
int temp = high;
// Maintain strictly decreasing
// sequence from index high_index
// to 0 starting with temp
for (int i = high_index; i >= 0; i--) {
// Store the value and decrement
// the temp variable by 1
A[i] = temp--;
}
// Maintain the strictly decreasing
// sequence from index high_index + 1
// to N - 1 starting with high - 1
high -= 1;
for (int i = high_index + 1; i < N; i++)
// Store the value and decrement
// high by 1
A[i] = high--;
// Print the resultant sequence
for (int i = 0; i < N; i++) {
cout << A[i] << ' ';
}
}
// Driver Code
int main()
{
int N = 5, low = 2, high = 6;
// Function Call
LargestArray(N, low, high);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
static void LargestArray(int N, int low,
int high)
{
// Store index of highest element
int high_index = N - (high - low + 1);
// If high_index > (N-1)/2, then
// remaining N/2 elements cannot
// be placed in bitonic order
if (high_index > (N - 1) / 2)
{
System.out.print("Not Possible");
return;
}
// If high_index <= 0, then
// set high_index as 1
if (high_index <= 0)
high_index = 1;
// Stores the resultant sequence
int[] A = new int[N];
// Store the high value
int temp = high;
// Maintain strictly decreasing
// sequence from index high_index
// to 0 starting with temp
for(int i = high_index; i >= 0; i--)
{
// Store the value and decrement
// the temp variable by 1
A[i] = temp--;
}
// Maintain the strictly decreasing
// sequence from index high_index + 1
// to N - 1 starting with high - 1
high -= 1;
for(int i = high_index + 1; i < N; i++)
// Store the value and decrement
// high by 1
A[i] = high--;
// Print the resultant sequence
for(int i = 0; i < N; i++)
{
System.out.print(A[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5, low = 2, high = 6;
// Function Call
LargestArray(N, low, high);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# Function to find the lexicographically
# largest bitonic sequence of size N
# elements lies in the range[low, high]
def LargestArray(N, low, high):
# Store index of highest element
high_index = N - (high - low + 1)
# If high_index > (N-1)/2, then
# remaining N/2 elements cannot
# be placed in bitonic order
if (high_index > (N - 1) // 2):
print("Not Possible")
return
# If high_index <= 0, then
# set high_index as 1
if (high_index <= 0):
high_index = 1
# Stores the resultant sequence
A = [0] * N
# Store the high value
temp = high
# Maintain strictly decreasing
# sequence from index high_index
# to 0 starting with temp
for i in range(high_index, -1, -1):
# Store the value and decrement
# the temp variable by 1
A[i] = temp
temp = temp - 1
# Maintain the strictly decreasing
# sequence from index high_index + 1
# to N - 1 starting with high - 1
high -= 1
for i in range(high_index + 1, N):
# Store the value and decrement
# high by 1
A[i] = high
high = high - 1
# Print the resultant sequence
for i in range(N):
print(A[i], end = " ")
# Driver Code
N = 5
low = 2
high = 6
# Function Call
LargestArray(N, low, high)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the lexicographically
// largest bitonic sequence of size N
// elements lies in the range[low, high]
static void LargestArray(int N, int low,
int high)
{
// Store index of highest element
int high_index = N - (high - low + 1);
// If high_index > (N-1)/2, then
// remaining N/2 elements cannot
// be placed in bitonic order
if (high_index > (N - 1) / 2)
{
Console.Write("Not Possible");
return;
}
// If high_index <= 0, then
// set high_index as 1
if (high_index <= 0)
high_index = 1;
// Stores the resultant sequence
int[] A = new int[N];
// Store the high value
int temp = high;
// Maintain strictly decreasing
// sequence from index high_index
// to 0 starting with temp
for(int i = high_index; i >= 0; i--)
{
// Store the value and decrement
// the temp variable by 1
A[i] = temp--;
}
// Maintain the strictly decreasing
// sequence from index high_index + 1
// to N - 1 starting with high - 1
high -= 1;
for(int i = high_index + 1; i < N; i++)
// Store the value and decrement
// high by 1
A[i] = high--;
// Print the resultant sequence
for(int i = 0; i < N; i++)
{
Console.Write(A[i] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 5, low = 2, high = 6;
// Function Call
LargestArray(N, low, high);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
5 6 5 4 3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live