Another approach, we could make the series as first n odd integers, it’ll always be less than 10^5 for the given constraint ( nth odd number = 2*n-1), and no two elements in the array would sum to any other element in the array ( sum of odds is even, if all are odd, their sum would be even all the time, we don’t have any even in the array)
I used the approach of prime numbers but i excluded 2 just to make the loop working. So the loop will start from 3 to 10^5 and if the number is prime number then i will print it.
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
static void arr(){
int n= sc.nextInt();
ArrayListv = new ArrayList<>();
for(int i = 0; i<n; i++){
v.add((long) (i+3));
}
for(int i=0;i<n;i++){
System.out.print(v.get(i)+" “);
}
System.out.print(”\n");
}
static Scanner sc = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception
{
int tc=sc.nextInt();
while(tc–>0)arr();
The code you provided is not running on Code chef IDE, maybe you did not paste the correct code. Never mind I went through your code by your profile and found you have used wrong logic.
For input: 8
Your output: 3,4,5,6,7,8,9,10
But we need Arr[i]+Arr[j]!=Arr[k]
Here in your output we clearly see 3+5=8