import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
//SINCE BECAUSE OF GIVEN RULES,NEXT INDEX CAN BE ONLY OLD INDEX OR +1 OF OLD INDEX
static int selectedIndex = 0;
public static void main(String[] args) throws
Path filePath = Paths.get("/Users/Giray/Desktop/test.txt");
Scanner scanner = new Scanner(filePath);
if (scanner.hasNextInt()) {
integersFromFile.add(scanner.nextInt());
} else {
scanner.next();
}
}
int sum = findSum(integersFromFile, findColumnCount(integersFromFile));
System.out.
}
public static int findColumnCount(LinkedList
int totalItemCount = array.size();
int firstNumber = 1;
int countOfColumns = 0;
while (totalItemCount != 0 ) {
countOfColumns++;
totalItemCount = totalItemCount - firstNumber;
firstNumber++;
}
return countOfColumns;
}
public static boolean isPrime(int n) {
if (n <= 1) {
return true;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return true;
}
}
return false;
}
public static
return false;
}
if (number <= 3) {
return false;
}
int tempNum1 = 0;
int tempNum2 = 0;
int currentColumn = 1;
if
return true;
}
}
return true;
}
public static void findSum(LinkedList
int tempMax = 0;
for (int currentColumn=1; currentColumn<=column; currentColumn++) {
// FIRST COLUMN
if
sum
array.removeFirst();
}
// 2 OR MORE BIGGER
else if (currentColumn != 1 ) {
if (array.get(selectedIndex) > array.get(selectedIndex+1)) {
tempMax = array.get(selectedIndex);
if (isNonPrime(tempMax)) {
sum = sum + tempMax;
}
}
else if (array.get(selectedIndex+1) > array.get(selectedIndex)) {
tempMax = array.get(selectedIndex+1);
if (isNonPrime(tempMax)) {
sum = sum + tempMax;
selectedIndex = selectedIndex + 1;
}
}
// FOR DELETING ELEMENTS FROM ARRAY
for (int j=0;j
}
}
break;
}
else {
tempNum1 = array.get(selectedIndex);
tempNum2 = array.get(selectedIndex+1);
//BOTH NOT PRIME AND TEMP1 BIGGER THAN TEMP2
if (isPrime(tempNum1) && isPrime(tempNum2) && tempNum1> tempNum2) {
sum += tempNum1;
}
// BOTH NOT PRIME A ND TEMP2 BIGGER THAN TEMP1
else if (isPrime(tempNum1) && isPrime(tempNum2) && tempNum2 > tempNum1) {
sum += tempNum2;
selectedIndex += 1;
}
// BOTH PRIME
else if (!isPrime(tempNum1) && !isPrime(tempNum2)) {
return sum;
}
// ONLY TEMP2 NOT PRIME
else if (!isPrime(tempNum1) && isPrime(tempNum2)) {
sum += tempNum2;
selectedIndex += 1;
}
// ONLY TEMP1 NOT PRIME
else if (!isPrime(tempNum2) && isPrime(tempNum1)) {
sum += tempNum1;
}
for (int i=1;i<=currentColumn;i++) {
array.removeFirst();
}
}
currentColumn += 1;
}
return sum;
}
}