Generation

generate functionThu, 06 Jul 2023

Given two integers N and B, the task is to print the maximum index a pointer, starting from 0th index can reach in an array of natural numbers(i.e., 0, 1, 2, 3, 4, 5…), say arr[], in N steps without placing itself at index B at any point in Java.

import java.util.Scanner; class TestClass { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T > 0) { int N = sc.nextInt(); int B = sc.nextInt(); int arr[] = new int[N]; for (int i = 0; i < N; i++) arr[i] = sc.nextInt(); int result = 0; if (B < N) { int res = arr[0]; for (int i = 1; i < B; i++) { res = Math.max(res, arr[i]); } result += res; for (int i = B; i < N; i++) { res = Math.max(res, arr[i]); } result += res; } else { int res = arr[0]; for (int i = 1; i < N;

Want to kickstart your project?Use the new AI Studio to create your code