- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Check if the given number K is enough to reach the end of an array in Python

Suppose we have an array nums and another value k. We have to check whether it is possible to reach the end of the array by performing these operations or not Operation: Traverse nums and, if any non-prime value is there then decrement the value of k by 1. Now if any value is prime then refill the value of k to its initial value.

So, if the input is like nums = [8, 5, 6, 7, 8], k = 2, then the output will be True as nums[0] is not prime, then make k = 1, then nums[1] is prime so k = 2, then nums[2] is not prime so k = 1, again nums[3] is prime so k = 2 and finally nums[4] is not prime and k = 1 and we are at last index.

To solve this, we will follow these steps −

- temp := k
- for i in range 0 to size of arr - 1, do
- if arr[i] is prime, then
- k := temp

- otherwise,
- k := k - 1

- if k <= 0 and i < size of arr - 1 and arr[i + 1] is not prime, then
- return False

- if arr[i] is prime, then
- return True

Let us see the following implementation to get better understanding −

## Example Code

def isPrime(num): if num > 1: for i in range(2, num): if num % i == 0: return False return True return False def solve(arr,k): temp = k for i in range(len(arr)): if isPrime(arr[i]): k = temp else: k -= 1 if k <= 0 and i < (len(arr) - 1) and isPrime(arr[i + 1]) == False: return False return True nums = [8, 5, 6, 7, 8] k = 2 print(solve(nums, k))

## Input

[8, 5, 6, 7, 8], 2

## Output

True

- Related Questions & Answers
- Find if it is possible to reach the end through given transitions in C++
- Python program to check if the given number is Happy Number
- C++ Program to check k rupees are enough to reach final cell or not
- Python Program to check if the given array is Monotonic
- Python program to check if the given number is a Disarium Number
- Program to check we can reach end of list by starting from k in Python
- Check if the given number is Ore number or not in Python
- Program to check if number of compass usage to get out of a maze is enough in Python
- Check if it is possible to reach a number by making jumps of two given length in Python
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- C Program for Minimum number of jumps to reach the end
- Check if given number is perfect square in Python
- Check if the array is beautiful in Python
- Java Program to Check if An Array Contains the Given Value

Advertisements