Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two people are playing a game where Kyle gives Genny an array arr of size n. There are some positions in the array where the index (According to 1 based indexing ) is NOT prime and the element at that index is prime.

Kyle has to find those elements and give that to Genny in order of their appearance in array.

Your task is to help Genny complete his task.

NOTE :Print -1 if there are no such elements.


2 3 5 7 11 13 17 19 23 29

2 7 13 19 23 29

What I have tried:

import java.util.*;

public class Main {

    public static void KyleArrayGame(int n, int[] arr) {
        // write code and print here
		int index =1;
		int flag=0;
		for(int i=0; i<n; i++){
				System.out.print(index+" ");

    public static void main(String[] args) {
        Scanner scn = new Scanner(;
        int n = scn.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;++i){
            arr[i] = scn.nextInt();
Updated 1-Jan-23 2:23am
Member 15627495 1-Jan-23 6:45am    
for(int index= 1 ; index < n ;index++){ ... }

you have badly understand the exercise.
Richard MacCutchan 1-Jan-23 7:17am    
The final value for index should be equal to n since it is a 1-based counter.

Think about the question.
Set found = 0 - this will be the count of valid items.
You need to count from 1 to n for the index values. For each index value that is not prime, check the array value at that position, remembering that array offsets count from zero, not one. If the value is prime then print it, and also add one to found. Continue until the index number equals the count of array elements. If found is zero, i.e. no valid prime entries were found, then print "-1". Hint: not all odd numbers are primes.
Share this answer
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

What you have shown us as "your code so far" has nothing at all to do with the problem as stated - it looks like you found a random bit of code on the internet and assumed it would do what you want. Throw it away, read the assignment carefully, and think about what it is asking you to do.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900