Given an array arr[] of n elements and a number K. The task is to determine if it is possible to reach the end of the array by doing the below operations:
Traverse the given array and,
- If any element is found to be non-prime then decrement the value of K by 1.
- If any element is prime then refill the value of K to its initial value.
If it is possible to reach the end of array with (K > 0), then print YES otherwise print NO.
Examples:
Input : K = 2 , arr[]={ 6, 3, 4, 5, 6} ;
Output : Yes
Explanation :
1- arr[0] is not prime, so K = K-1 = 1
2- arr[1] is prime so K will be refilled to its
initial value. Therefore, K = 2.
3- arr[2] is not prime.
Therefore, K = 2-1 = 1
4- arr[3] is prime so K will be refilled to its
initial value. Therefore, K = 2.
5- arr[4] is not prime.
Therefore, K = 2-1 = 1
6- Since the end of the array is reached with K>=0
So output is YES
Input : n=6 , k=3;
arr[]={ 1, 2, 10, 4, 6, 8};
Output : No
Recommended: Please solve it on “PRACTICE “first , before moving on to the solution.
Simple Approach:
- Traverse each element of the array and Check if the value of current element is prime or not.
- If it is Prime then refill the power of K else decrement by 1.
- If it is possible to reach the end of the array with (K > 0) then print “YES” otherwise “NO”.
Below is the implementation of the above approach:
C++
// C++ program to check if it is possible // to reach the end of the array #include <iostream> using namespace std; // Function To check number is prime or not bool is_Prime( int num ) { // because 1 is not prime if(num == 1) return false; for(int i=2 ; i*i <= num ; i++ ) { if( num % i == 0 ) return false; } return true; } // Function to check whether it is possible // to reach the end of the array or not bool isReachable( int arr[] , int n , int k) { // store initial value of K int x = k ; for(int i=0 ; i < n ; i++ ) { // Call is_prime function to // check if a number is prime. if( is_Prime(arr[i]) ) { // Refill K to initial value k = x; } else { // Decrement k by 1 k-- ; } if( k <= 0 && i < (n-1) && (!is_Prime(arr[i+1])) ) return false ; } return true ; } // Driver Code int main() { int arr[] = { 6, 3, 4, 5, 6}; int n = sizeof(arr)/sizeof(arr[0]) ; int k = 2 ; isReachable( arr , n , k ) ? cout << "Yes" << endl : cout << "No" << endl ; return 0 ; } |
Java
// Java program to check if // it is possible to reach // the end of the array import java.io.*; import java.util.*; import java.lang.*; class GFG { // Function To check // number is prime or not static boolean is_Prime(int num) { // because 1 is not prime if(num == 1) return false; for(int i = 2 ; i * i <= num ; i++ ) { if(num % i == 0) return false; } return true; } // Function to check whether // it is possible to reach // the end of the array or not static boolean isReachable(int arr[] , int n , int k) { // store initial value of K int x = k ; for(int i = 0 ; i < n ; i++ ) { // Call is_prime function to // check if a number is prime. if(is_Prime(arr[i])) { // Refill K to // initial value k = x; } else { // Decrement k by 1 k-- ; } if(k <= 0 && i < (n - 1) && (is_Prime(arr[i + 1]) != true)) return false ; } return true ; } // Driver Code public static void main(String args[]) { int arr[] = new int[]{ 6, 3, 4, 5, 6}; int n = arr.length; int k = 2 ; if(isReachable(arr, n, k) == true) System.out.print("Yes" + "\n"); else System.out.print("No" + "\n"); } } |
Python3
# Python 3 program to check if it is # possible to reach the end of the array from math import sqrt # Function To check number is prime or not def is_Prime(num): # because 1 is not prime if(num == 1): return False k = int(sqrt(num)) + 1 for i in range(2, k, 1): if(num % i == 0): return False return True # Function to check whether it is possible # to reach the end of the array or not def isReachable(arr, n , k): # store initial value of K x = k for i in range(0, n, 1): # Call is_prime function to # check if a number is prime. if( is_Prime(arr[i])): # Refill K to initial value k = x else: # Decrement k by 1 k -= 1 if(k <= 0 and i < (n - 1) and (is_Prime(arr[i + 1])) == False): return False return True # Driver Code if __name__ == '__main__': arr = [6, 3, 4, 5, 6] n = len(arr) k = 2 if (isReachable( arr , n , k )): print("Yes") else: print("No") # This code is contributed by # Sahil_Shelangia |
C#
// C# program to check if // it is possible to reach // the end of the array using System; class GFG { // Function To check // number is prime or not static bool is_Prime(int num) { // because 1 is not prime if(num == 1) return false; for(int i = 2 ; i * i <= num ; i++ ) { if(num % i == 0) return false; } return true; } // Function to check whether // it is possible to reach // the end of the array or not static bool isReachable(int []arr , int n , int k) { // store initial // value of K int x = k ; for(int i = 0 ; i < n ; i++ ) { // Call is_prime function // to check if a number // is prime. if(is_Prime(arr[i])) { // Refill K to // initial value k = x; } else { // Decrement k by 1 k-- ; } if(k <= 0 && i < (n - 1) && (is_Prime(arr[i + 1]) != true)) return false ; } return true ; } // Driver Code public static void Main() { int []arr = new int[]{ 6, 3, 4, 5, 6}; int n = arr.Length; int k = 2 ; if(isReachable(arr, n, k) == true) Console.WriteLine("Yes" + "\n"); else Console.WriteLine("No" + "\n"); } } // This code is contributed by vt_m |
PHP
<?php // PHP program to check if it is possible // to reach the end of the array // Function To check number // is prime or not function is_Prime($num ) { // because 1 is not prime if($num == 1) return false; for($i = 2 ; ($i * $i) <= $num ; $i++ ) { if($num % $i == 0) return false; } return true; } // Function to check whether it is possible // to reach the end of the array or not function isReachable($arr , $n , $k) { // store initial value of K $x = $k ; for($i = 0 ; $i < $n ; $i++) { // Call is_prime function to // check if a number is prime. if(is_Prime($arr[$i])) { // Refill K to initial value $k = $x; } else { // Decrement k by 1 $k-- ; } if($k <= 0 && $i < ($n - 1) && (!is_Prime($arr[$i + 1]))) return false ; } return true ; } // Driver Code $arr = array(6, 3, 4, 5, 6); $n = sizeof($arr); $k = 2; if(isReachable( $arr , $n , $k )) echo "Yes"; else echo "No" ; // This code is contributed // by Sach_Code ?> |
Yes
Time complexity: O(N(sqrt N))
Efficient Approach: The above approach can be optimized by using Sieve of Eratosthenes to check if a number is prime or not.
Below is the implementation of the efficient approach:
C++
// CPP program to check if it is possible // to reach the end of the array #include <iostream> #define MAX 1000000 using namespace std; // Function for Sieve of Eratosthenes void SieveOfEratosthenes( int sieve[], int max ) { for(int i=0; i<max; i++) sieve[i] = 1; for(int i=2 ; i*i <= max ; i++ ) { if( sieve[i] == 1 ) { for( int j=i*2 ; j <= max ; j+=i ) sieve[ j ] = 0; } } } // Function to check if it is possible to // reach end of the array bool isReachable( int arr[] , int n , int sieve[] , int k) { // store initial value of K int x = k; for(int i=0 ; i < n ; i++ ) { if( sieve[arr[i]] ) { // Refill K to initial value k = x; } else { // Decrement k by 1 k -= 1; } if((k <= 0) && (i < (n-1)) && (sieve[arr[i+1]] == 0)) { return false ; } } return true ; } // Driver Code int main() { int arr[] = {6, 3, 4, 5, 6}; int sieve[MAX]; int n = sizeof(arr)/sizeof(arr[0]) ; int k = 2; SieveOfEratosthenes(sieve, MAX) ; isReachable( arr , n , sieve , k ) ? cout << "Yes" << endl : cout << "No" << endl ; return 0 ; } |
Java
// Java program to check if it is possible // to reach the end of the array class GFG { static int MAX = 1000000; // Function for Sieve of Eratosthenes static void SieveOfEratosthenes(int sieve[], int max) { for (int i = 0; i < max; i++) { sieve[i] = 1; } for (int i = 2; i * i < max; i++) { if (sieve[i] == 1) { for (int j = i * 2; j < max; j += i) { sieve[j] = 0; } } } } // Function to check if it is possible to // reach end of the array static boolean isReachable(int arr[], int n, int sieve[], int k) { // store initial value of K int x = k; for (int i = 0; i < n; i++) { if (sieve[arr[i]] == 1) { // Refill K to initial value k = x; } else { // Decrement k by 1 k -= 1; } if ((k <= 0) && (i < (n - 1)) && (sieve[arr[i + 1]] == 0)) { return false; } } return true; } // Driver Code public static void main(String[] args) { int arr[] = {6, 3, 4, 5, 6}; int[] sieve = new int[MAX]; int n = arr.length; int k = 2; SieveOfEratosthenes(sieve, MAX); if (isReachable(arr, n, sieve, k)) { System.out.println("Yes"); } else { System.out.println("No"); } } } /* This code contributed by PrinciRaj1992 */ |
C#
// C# program to check if it is possible // to reach the end of the array using System; class GFG { static int MAX = 1000000; // Function for Sieve of Eratosthenes static void SieveOfEratosthenes(int []sieve, int max) { for (int i = 0; i < max; i++) { sieve[i] = 1; } for (int i = 2; i * i < max; i++) { if (sieve[i] == 1) { for (int j = i * 2; j < max; j += i) { sieve[j] = 0; } } } } // Function to check if it is possible to // reach end of the array static bool isReachable(int []arr, int n, int []sieve, int k) { // store initial value of K int x = k; for (int i = 0; i < n; i++) { if (sieve[arr[i]] == 1) { // Refill K to initial value k = x; } else { // Decrement k by 1 k -= 1; } if ((k <= 0) && (i < (n - 1)) && (sieve[arr[i + 1]] == 0)) { return false; } } return true; } // Driver Code static public void Main () { int []arr = {6, 3, 4, 5, 6}; int[] sieve = new int[MAX]; int n = arr.Length; int k = 2; SieveOfEratosthenes(sieve, MAX); if (isReachable(arr, n, sieve, k)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } /* This code contributed by ajit. */ |
Yes
Time complexity: O(?Max * loglog(Max)) + O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Shortest path to reach one prime to other by changing single digit at a time
- Check if given number is Emirp Number or not
- Check whether the given number is Euclid Number or not
- Check whether given number N is a Moran Number or not
- Count the number of primes in the prefix sum array of the given array
- Check if is possible to get given sum from a given set of elements
- Check if each element of the given array is the product of exactly K prime numbers
- Check whether the given number is Wagstaff prime or not
- Check if a number is a Trojan Number
- Check if the number is a Prime power number
- Number of 0s and 1s at prime positions in the given array
- Insert minimum number in array so that sum of array becomes prime
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Nearest prime number in the array of every array element
- Querying maximum number of divisors that a number in a given range has
- Determine whether a given number is a Hyperperfect Number
- Total number of divisors for a given number
- Find the largest good number in the divisors of given number N
- Check whether given three numbers are adjacent primes
- Check whether the given numbers are Cousin prime or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

