📌  相关文章
📜  K 在不同值之间跳转后二进制数组中要达到的最大索引

📅  最后修改于: 2022-05-13 01:56:07.028000             🧑  作者: Mango

K 在不同值之间跳转后二进制数组中要达到的最大索引

给定一个大小为N的二进制数组arr[]和一个整数K ,任务是找到从第一个索引开始的恰好K次跳转中可以达到的最高索引,此时可以在具有不同值的索引之间进行一次跳转。

例子:

方法:可以根据以下观察解决问题:

为了更好地理解,下面给出了一个说明:

插图:

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

  • 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)