K 在不同值之间跳转后二进制数组中要达到的最大索引
给定一个大小为N的二进制数组arr[]和一个整数K ,任务是找到从第一个索引开始的恰好K次跳转中可以达到的最高索引,此时可以在具有不同值的索引之间进行一次跳转。
例子:
Input: arr[] = {0, 1, 1, 0, 1, 0}, K = 2
Output: 5
Explanation: All possible jumps are:
{0, 1, 3}, {0, 2, 3}, {0, 1, 5}, {0, 2, 5}, {0, 4, 5}
So, the highest index that can be reached is index 5.
Input: arr[] = {1, 0, 1, 1, 0}, K = 3
Output: 4
方法:可以根据以下观察解决问题:
- The highest possible value of K is same as the total number of shifts between consecutive 1s and consecutive 0s.
- As in a jump, the two values are different, so if K is even then the value at starting index and value at last index will be same and if K is odd ten they will be different.
- Now to find the highest index (when it is possible to make K jumps), iterate from end of array and based on K being even or odd return the first index i such that arr[i] = arr[0] or arr[i] ≠ arr[0] (because it is already found that a total of K jumps can be made between them).
为了更好地理解,下面给出了一个说明:
插图:
Consider arr[] = {0, 1, 1, 0, 1, 0}, K = 2.
Highest possible value of K = 4:
=> Consecutive 0s in range [0, 0]. Total shifts = 0
=> Consecutive 1s in range [1, 2]. Shift from consecutive 0s to 1s. Total shifts = 1.
=> Consecutive 0s in range [3, 3]. Shift from consecutive 1s to 0s. Total shifts = 1+1 = 2.
=> Consecutive 1s in range [4, 4]. Shift from consecutive 0s to 1s. Total shifts = 2+1 = 3.
=> Consecutive 0s in range [5, 5]. Shift from consecutive 1s to 0s. Total shifts = 3+1 = 4.
Iterate from i = 5 to 0:
=>For i = 5: arr[5] = arr[0] = 0. K = 2 i.e. even. Stop iterating
Highest index that can be reached is 5.
One such path is (0->1->5).
请按照以下步骤解决问题:
- 从i = 0 到 N-1遍历数组:
- 查找从连续 0 到连续 1 的总移位,反之亦然(例如count )。
- 如果K > count ,则返回-1 ,因为 K 跳跃是不可能的。
- 否则,从 i = N-1 遍历到 0:
- 如果K是偶数,则在arr[i] = arr[0]时停止迭代。
- 如果K是奇数,则在arr[i] ≠ arr[0]时停止迭代。
- 返回从上述步骤获得的最高索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the index to which
// the longest jump can be made
int maxJump(int arr[], int N, int k)
{
int i;
// To store possible cases count
int count = 0;
for (int i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
// Traversing the array A[]
// from the end
// to find longest index
for (i = N - 1; i >= 0; i--) {
// Firstly checking
// if k is even and
// if first and last element
// match
if (k % 2 == 0 && arr[i] == arr[0]) {
// Return the required index
return i;
}
// Or, if k is odd
// and first and last
// element doesn't match
if (k % 2 != 0 && arr[i] != arr[0]) {
// Return the required index
return i;
}
}
}
return -1;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 1, 0, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
int k = 2;
// Function call
cout << maxJump(arr, N, k);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to find the index to which
// the longest jump can be made
static int maxJump(int arr[], int N, int k)
{
int i;
// To store possible cases count
int count = 0;
for ( i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
// Traversing the array A[]
// from the end
// to find longest index
for (i = N - 1; i >= 0; i--) {
// Firstly checking
// if k is even and
// if first and last element
// match
if (k % 2 == 0 && arr[i] == arr[0]) {
// Return the required index
return i;
}
// Or, if k is odd
// and first and last
// element doesn't match
if (k % 2 != 0 && arr[i] != arr[0]) {
// Return the required index
return i;
}
}
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 0, 1, 1, 0, 1, 0 };
int N = arr.length;
int k = 2;
// Function call
System.out.println(maxJump(arr, N, k));
}
}
// This code is contributed by lokeshpotta20.
Python3
# Python3 rogram for the above approach
# Function to find the index to which
# the longest jump can be made
def maxJump(arr, N, k):
# To store possible cases count
count = 0
for i in range(1, N):
if arr[i] != arr[i - 1]:
count += 1
if count >= k:
# Traversing the array A[]
# from the end
# to find longest index
for i in range(N - 1, -1, -1):
# Firstly checking
# if k is even and
# if first and last element
# match
if k % 2 == 0 and arr[i] == arr[0]:
# Return the required index
return i
# Or, if k is odd
# and first and last
# element doesn't match
if k % 2 != 0 and arr[i] != arr[0]:
# Return the required index
return i
return -1
# Driver Code
arr = [0, 1, 1, 0, 1, 0]
N = len(arr)
k = 2
# function call
print(maxJump(arr, N, k))
# This code is contributed by phasing17.
C#
using System;
public class GFG{
// Function to find the index to which
// the longest jump can be made
static int maxJump(int[] arr, int N, int k)
{
int i;
// To store possible cases count
int count = 0;
for ( i = 1; i < N; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
if (count >= k) {
// Traversing the array A[]
// from the end
// to find longest index
for (i = N - 1; i >= 0; i--) {
// Firstly checking
// if k is even and
// if first and last element
// match
if (k % 2 == 0 && arr[i] == arr[0]) {
// Return the required index
return i;
}
// Or, if k is odd
// and first and last
// element doesn't match
if (k % 2 != 0 && arr[i] != arr[0]) {
// Return the required index
return i;
}
}
}
return -1;
}
// Driver Code
static public void Main (){
int[] arr = { 0, 1, 1, 0, 1, 0 };
int N = arr.Length;
int k = 2;
// Function call
Console.Write(maxJump(arr, N, k));
}
}
// This code is contributed by hrithikgarg03188.
Javascript
5
时间复杂度: O(N)
辅助空间: O(N)