Answer to Question #336484 in Java | JSP | JSF for mahesh

Question #336484

N a country of zombies each city has a certain percentage of zombies. Cities are designated as 1....


n a country of zombies each city has a certain percentage of zombies. Cities are designated as



1. A city[i] is magical if city[i] and city[i+1] have no common divisor other than 1.



2. A city is good if the percentage of zombies in the city[i] is more than percentage of zombies in city[i+1]



Find a city that is perfect, where perfect means both good and magical,



if there are more than one perfect cities, output the left-most city index. Also, the minimum number of cities in a country is 2 and there will be at least one perfect city



Input Specification:



input1: An array representing the percentage of zombies in each city



input2: Number of cities in the country



Output Specification:



Return the favourable city index "i



1
Expert's answer
2022-05-04T12:39:15-0400
import java.util.Scanner;

public class Answer {
    static boolean divisor(int a, int b) {
        int gcd = 1;
        for (int i = 1; i <= a && i <= b; i++) {
            if (a % i == 0 && b % i == 0) {
                gcd = i;
            }
        }
        return gcd <= 1;
    }
    static int check(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] > array[i + 1] && divisor(array[i], array[i + 1])) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println("Enter number of cities in the country: ");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println("Enter the percentage of zombies in each city: ");
        int[] array = new int[num];
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println("Favourable city index: " + check(array));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS