Check if the given number K is enough to reach the end of an array
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 : 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 the current element is prime or not.
- If it is Prime then refill the power of K else decrements 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++
#include <iostream>
using namespace std;
bool is_Prime( int num )
{
if (num == 1)
return false ;
for ( int i=2 ; i*i <= num ; i++ )
{
if ( num % i == 0 )
return false ;
}
return true ;
}
bool isReachable( int arr[] , int n , int k)
{
int x = k ;
for ( int i=0 ; i < n ; i++ )
{
if ( is_Prime(arr[i]) )
{
k = x;
}
else
{
k-- ;
}
if ( k <= 0 && i < (n-1) && (!is_Prime(arr[i+1])) )
return false ;
}
return true ;
}
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
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static boolean is_Prime( int num)
{
if (num == 1 )
return false ;
for ( int i = 2 ;
i * i <= num ; i++ )
{
if (num % i == 0 )
return false ;
}
return true ;
}
static boolean isReachable( int arr[] ,
int n , int k)
{
int x = k ;
for ( int i = 0 ; i < n ; i++ )
{
if (is_Prime(arr[i]))
{
k = x;
}
else
{
k-- ;
}
if (k <= 0 && i < (n - 1 ) &&
(is_Prime(arr[i + 1 ]) != true ))
return false ;
}
return true ;
}
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
from math import sqrt
def is_Prime(num):
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
def isReachable(arr, n , k):
x = k
for i in range ( 0 , n, 1 ):
if ( is_Prime(arr[i])):
k = x
else :
k - = 1
if (k < = 0 and i < (n - 1 ) and
(is_Prime(arr[i + 1 ])) = = False ):
return False
return True
if __name__ = = '__main__' :
arr = [ 6 , 3 , 4 , 5 , 6 ]
n = len (arr)
k = 2
if (isReachable( arr , n , k )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool is_Prime( int num)
{
if (num == 1)
return false ;
for ( int i = 2 ;
i * i <= num ; i++ )
{
if (num % i == 0)
return false ;
}
return true ;
}
static bool isReachable( int []arr ,
int n , int k)
{
int x = k ;
for ( int i = 0 ; i < n ; i++ )
{
if (is_Prime(arr[i]))
{
k = x;
}
else
{
k-- ;
}
if (k <= 0 && i < (n - 1) &&
(is_Prime(arr[i + 1]) != true ))
return false ;
}
return true ;
}
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" );
}
}
|
PHP
<?php
function is_Prime( $num )
{
if ( $num == 1)
return false;
for ( $i = 2 ; ( $i * $i ) <= $num ; $i ++ )
{
if ( $num % $i == 0)
return false;
}
return true;
}
function isReachable( $arr , $n , $k )
{
$x = $k ;
for ( $i = 0 ; $i < $n ; $i ++)
{
if (is_Prime( $arr [ $i ]))
{
$k = $x ;
}
else
{
$k -- ;
}
if ( $k <= 0 && $i < ( $n - 1) &&
(!is_Prime( $arr [ $i + 1])))
return false ;
}
return true ;
}
$arr = array (6, 3, 4, 5, 6);
$n = sizeof( $arr );
$k = 2;
if (isReachable( $arr , $n , $k ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function is_Prime(num)
{
if (num == 1)
return false ;
for (let i = 2; i * i <= num; i++)
{
if (num % i == 0)
return false ;
}
return true ;
}
function isReachable(arr, n, k)
{
let x = k ;
for (let i = 0 ; i < n ; i++ )
{
if (is_Prime(arr[i]))
{
k = x;
}
else
{
k-- ;
}
if (k <= 0 && i < (n - 1) &&
(is_Prime(arr[i + 1]) != true ))
return false ;
}
return true ;
}
let arr = [ 6, 3, 4, 5, 6 ];
let n = arr.length;
let k = 2 ;
if (isReachable(arr, n, k) == true )
document.write( "Yes" + "</br>" );
else
document.write( "No" + "</br>" );
</script>
|
Time complexity: O(N(sqrt N))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using the Sieve of Eratosthenes to check if a number is prime or not.
Below is the implementation of the efficient approach:
C++
#include <iostream>
#define MAX 1000000
using namespace std;
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;
}
}
}
bool isReachable( int arr[] , int n , int sieve[] , int k)
{
int x = k;
for ( int i=0 ; i < n ; i++ )
{
if ( sieve[arr[i]] )
{
k = x;
}
else
{
k -= 1;
}
if ((k <= 0) && (i < (n-1)) &&
(sieve[arr[i+1]] == 0))
{
return false ;
}
}
return true ;
}
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
class GFG
{
static int MAX = 1000000 ;
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 ;
}
}
}
}
static boolean isReachable( int arr[], int n,
int sieve[], int k)
{
int x = k;
for ( int i = 0 ; i < n; i++)
{
if (sieve[arr[i]] == 1 )
{
k = x;
}
else
{
k -= 1 ;
}
if ((k <= 0 ) && (i < (n - 1 ))
&& (sieve[arr[i + 1 ]] == 0 ))
{
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Python3
import math
def SieveOfEratosthenes(sieve, max ):
for i in range ( 0 , max ):
sieve[i] = 1
sqt = int (math.sqrt( max ))
for i in range ( 2 , sqt):
if (sieve[i] = = 1 ):
for j in range (i * 2 , max , i):
sieve[j] = 0
def isReachable(arr, n, sieve, k):
x = k
for i in range ( 0 , n):
if (sieve[arr[i]] ! = 0 ):
k = x
else :
k - = 1
if ((k < = 0 ) and (i < (n - 1 )) and
(sieve[arr[i + 1 ]] = = 0 )):
return 0
return 1
arr = [ 6 , 3 , 4 , 5 , 6 ]
sieve = [ 0 for x in range ( 1000000 )]
n = len (arr)
k = 2
SieveOfEratosthenes(sieve, 1000000 )
ch = isReachable(arr, n, sieve, k)
if (ch):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int MAX = 1000000;
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;
}
}
}
}
static bool isReachable( int []arr, int n,
int []sieve, int k)
{
int x = k;
for ( int i = 0; i < n; i++)
{
if (sieve[arr[i]] == 1)
{
k = x;
}
else
{
k -= 1;
}
if ((k <= 0) && (i < (n - 1))
&& (sieve[arr[i + 1]] == 0))
{
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Javascript
<script>
let MAX = 1000000;
function SieveOfEratosthenes(sieve, max)
{
for (let i = 0; i < max; i++)
{
sieve[i] = 1;
}
for (let i = 2; i * i < max; i++)
{
if (sieve[i] == 1)
{
for (let j = i * 2; j < max; j += i)
{
sieve[j] = 0;
}
}
}
}
function isReachable(arr, n, sieve, k)
{
let x = k;
for (let i = 0; i < n; i++)
{
if (sieve[arr[i]] == 1)
{
k = x;
}
else
{
k -= 1;
}
if ((k <= 0) && (i < (n - 1)) && (sieve[arr[i + 1]] == 0))
{
return false ;
}
}
return true ;
}
let arr = [6, 3, 4, 5, 6];
let sieve = new Array(MAX);
let n = arr.length;
let k = 2;
SieveOfEratosthenes(sieve, MAX);
if (isReachable(arr, n, sieve, k))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time complexity: O(MAX * log(log(MAX)))
Auxiliary Space: O(MAX)
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...