📌  相关文章
📜  通过避开给定的索引 B | 指针可以在 N 步中达到的最大索引2套

📅  最后修改于: 2021-09-04 08:22:22             🧑  作者: Mango

给定两个整数NB ,任务是打印数组中可以达到的最大索引,从第0索引开始,在N步中,而不将自己置于索引B的任何点,其中在每个i步中,指针可以向右移动i 个索引。

例子:

朴素的方法:请参阅上一篇文章,了解解决问题的最简单方法。

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

高效的方法:解决问题的最佳想法基于以下观察:

请按照以下步骤解决问题:

  • 初始化两个指针i = 0j = 1。
  • 初始化一个变量,比如sum,来存储前N 个自然数的和N * (N + 1) / 2。
  • 初始化一个变量,比如cnt = 0和另一个变量,比如flag = false。
  • 迭代直到cnt小于N。
    • j增加i
    • 增加j
    • 增加cnt
    • 如果在任何迭代中, i等于B ,则设置flag = true并跳出循环。
  • 如果flagfalse ,则打印sum 。否则,打印sum – 1。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum
// index the pointer can reach
int maximumIndex(int N, int B)
{
    // Initialize two pointers
    int i = 0, j = 1;
 
    // Stores number of steps
    int cnt = 0;
 
    // Stores sum of first N
    // natural numbers
    int sum = N * (N + 1) / 2;
 
    bool flag = false;
 
    while (cnt < N) {
 
        // Increment i with j
        i += j;
 
        // Increment j with 1
        j++;
 
        // Increment count
        cnt++;
 
        // If i points to B
        if (i == B) {
 
            // Break
            flag = true;
            break;
        }
    }
 
    // Print the pointer index
    if (!flag) {
        cout << sum;
    }
    else
        cout << sum - 1;
}
 
// Driver Code
int main()
{
    // Given value of N & B
    int N = 4, B = 6;
 
    // Function call to find maximum
    // index the pointer can reach
    maximumIndex(N, B);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the maximum
// index the pointer can reach
static void maximumIndex(int N, int B)
{
     
    // Initialize two pointers
    int i = 0, j = 1;
 
    // Stores number of steps
    int cnt = 0;
 
    // Stores sum of first N
    // natural numbers
    int sum = N * (N + 1) / 2;
 
    boolean flag = false;
 
    while (cnt < N)
    {
         
        // Increment i with j
        i += j;
 
        // Increment j with 1
        j++;
 
        // Increment count
        cnt++;
 
        // If i points to B
        if (i == B)
        {
             
            // Break
            flag = true;
            break;
        }
    }
 
    // Print the pointer index
    if (!flag == true)
    {
        System.out.print(sum);
    }
    else
    {
        System.out.print(sum - 1);
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given value of N & B
    int N = 4, B = 6;
 
    // Function call to find maximum
    // index the pointer can reach
    maximumIndex(N, B);
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the maximum
# index the pointer can reach
def maximumIndex(N, B):
     
    # Initialize two pointers
    i, j = 0, 1
 
    # Stores number of steps
    cnt = 0
 
    # Stores sum of first N
    # natural numbers
    sum = N * (N + 1) // 2
 
    flag = False
 
    while (cnt < N):
 
        # Increment i with j
        i += j
 
        # Increment j with 1
        j += 1
 
        # Increment count
        cnt += 1
 
        # If i points to B
        if (i == B):
 
            # Break
            flag = True
            break
 
    # Print the pointer index
    if (not flag):
        print (sum)
    else:
        print(sum - 1)
 
# Driver Code
if __name__ == '__main__':
     
    # Given value of N & B
    N, B = 4, 6
 
    # Function call to find maximum
    # index the pointer can reach
    maximumIndex(N, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum
// index the pointer can reach
static void maximumIndex(int N, int B)
{
     
    // Initialize two pointers
    int i = 0, j = 1;
 
    // Stores number of steps
    int cnt = 0;
 
    // Stores sum of first N
    // natural numbers
    int sum = N * (N + 1) / 2;
 
    bool flag = false;
 
    while (cnt < N)
    {
         
        // Increment i with j
        i += j;
 
        // Increment j with 1
        j++;
 
        // Increment count
        cnt++;
 
        // If i points to B
        if (i == B)
        {
             
            // Break
            flag = true;
            break;
        }
    }
 
    // Print the pointer index
    if (!flag == true)
    {
        Console.Write(sum);
    }
    else
    {
       Console.Write(sum - 1);
    }
}
 
// Driver Code
static public void Main ()
{
     
    // Given value of N & B
    int N = 4, B = 6;
 
    // Function call to find maximum
    // index the pointer can reach
    maximumIndex(N, B);
}
}
 
// This code is contributed by avijitmondal1998


Javascript


输出:
9

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live