📌  相关文章
📜  从给定范围内的整数生成长度为N的双音序列

📅  最后修改于: 2021-05-14 02:14:01             🧑  作者: Mango

给定整数NLR ,任务是根据范围[L,R]中的整数生成长度为N双音序列,以使第一个元素最大。如果无法创建这样的序列,则打印“ -1”

例子:

方法:想法是使用双端队列 这样就可以从头到尾添加元素。请按照以下步骤解决问题:

  • 初始化双端队列以存储生成的双音序列的元素。
  • 将变量i初始化为0,然后开始在结果列表中从(R – i)开始添加元素,直到i小于(R – L + 1)(N – 1)的最小值。
  • 完成上述步骤后,如果结果列表的大小小于N ,则从列表的开头开始将(R – 1)中的add元素添加到L ,直到结果列表的大小不为N为止。
  • 完成上述步骤后,如果N大于(R – L)* 2 +1 ,则无法构造这样的序列,然后打印“ -1”,否则打印存储在双端队列中的序列。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to construct bitonic
// sequence of length N from
// integers in the range [L, R]
void bitonicSequence(int num, int lower,
                     int upper)
{
     
    // If sequence is not possible
    if (num > (upper - lower) * 2 + 1)
    {
        cout << -1;
        return;
    }
 
    // Store the resultant list
    deque ans;
    deque::iterator j = ans.begin();
 
    for(int i = 0;
            i < min(upper - lower + 1, num - 1);
            i++)
        ans.push_back(upper - i);
         
    // If size of deque < n
    for(int i = 0;
            i < num - ans.size();
            i++)
          
    // Add elements from start
    ans.push_front(upper - i - 1);
 
    // Print the stored in the list
    cout << '[';
    for(j = ans.begin(); j != ans.end(); ++j)
        cout << ' ' << *j;
         
    cout << ' ' << ']';
}
 
// Driver Code
int main()
{
    int N = 5, L = 3, R = 10;
 
    // Function Call
    bitonicSequence(N, L, R);
 
    return 0;
}
 
// This code is contributed by jana_sayantan


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to construct bitonic
    // sequence of length N from
    // integers in the range [L, R]
    public static void bitonicSequence(
        int num, int lower, int upper)
    {
        // If sequence is not possible
        if (num > (upper - lower) * 2 + 1) {
            System.out.println(-1);
            return;
        }
 
        // Store the resultant list
        Deque ans
            = new ArrayDeque<>();
 
        for (int i = 0;
             i < Math.min(upper - lower + 1,
                          num - 1);
             i++)
            ans.add(upper - i);
 
        // If size of deque < n
        for (int i = 0;
             i < num - ans.size(); i++)
 
            // Add elements from start
            ans.addFirst(upper - i - 1);
 
        // Print the stored in the list
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5, L = 3, R = 10;
 
        // Function Call
        bitonicSequence(N, L, R);
    }
}


Python3
# Python3 program for the above approach
from collections import deque
 
# Function to construct bitonic
# sequence of length N from
# integers in the range [L, R]
def bitonicSequence(num, lower, upper):
     
    # If sequence is not possible
    if (num > (upper - lower) * 2 + 1):
        print(-1)
        return
 
    # Store the resultant list
    ans = deque()
     
    for i in range(min(upper - lower + 1,
                                 num - 1)):
        ans.append(upper - i)
 
    # If size of deque < n
    for i in range(num - len(ans)):
         
        # Add elements from start
        ans.appendleft(upper - i - 1)
 
    # Print the stored in the list
    print(list(ans))
 
# Driver Code
if __name__ == '__main__':
     
    N = 5
    L = 3
    R = 10
 
    # Function Call
    bitonicSequence(N, L, R)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to construct bitonic
// sequence of length N from
// integers in the range [L, R]
public static void bitonicSequence(int num,
                                   int lower,
                                   int upper)
{
     
    // If sequence is not possible
    if (num > (upper - lower) * 2 + 1)
    {
        Console.WriteLine(-1);
        return;
    }
 
    // Store the resultant list
    List ans = new List();
 
    for(int i = 0;
            i < Math.Min(upper - lower + 1,
                           num - 1); i++)
        ans.Add(upper - i);
 
    // If size of deque < n
    for(int i = 0;
            i < num - ans.Count; i++)
 
        // Add elements from start
        ans.Insert(0,upper - i - 1);
 
    // Print the stored in the list
    Console.Write("[");
    foreach(int x in ans)
        Console.Write(x + ", ");
         
    Console.Write("]");
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 5, L = 3, R = 10;
     
    // Function Call
    bitonicSequence(N, L, R);
}
}
 
// This code is contributed by Rajput-Ji


输出:
[9, 10, 9, 8, 7]

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