You are given an array A of size N that contains integers. Here, N is an even number. You are required to perform the following operations:
  1. Divide the array of numbers in two equal halves
    Note: Here, two equal parts of a test case are created by dividing the array into two equal parts.
  2. Take the first digit of the numbers that are available in the first half of the array (first 50% of the test case)
  3. Take the last digit of the numbers that are available in the second half of the array (second 50% of the test case)
  4. Generate a number by using the digits that have been selected in the above steps
Your task is to determine whether the newly-generated number is divisible by 11.
Input format
  • First line: A single integer N denoting the size of array A
  • Second line: N space-separated integers denoting the elements of array A
Output format
If the newly-generated number is divisible by 11, then print OUI. Otherwise, print NON.
Constraints
1N1051A[i]105
 
SAMPLE INPUT
6
15478 8452 8232 874 985 4512
SAMPLE OUTPUT
OUI
Explanation
The first digit of 15478 is 1.
The first digit of 8452 is 8.
The first digit of 8232 is 8.
The last digit of 874 is 4.
The last digit of 985 is 5.
The last digit of 4512 is 2.
The newly generated number will be 188452 which is divisible by 11. 

Solution-



import java.util.Scanner;
import java.util.ang;
class Divisibility
{
    public static void main(String... args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n%2 != 0)
        return;
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
        {
            arr[i] = sc.nextInt();
        }
        int mid = n/2;
        String s1 = "";
        for(int i=0; i<mid; i++)
        {
            s1 = s1 + Character.toString(Integer.toString(arr[i]).charAt(0));
        }
        String s2 = "";
        for(int i=mid; i<n; i++)
        {
            s2 = s2 + arr[i]%10;
        }
        String conc = s1 + s2;
     
        int n1 = 0;
        int n2 = 0;
        for(int i=0; i<conc.length(); i++)
        {
            if((i+1) % 2 == 0)
            n1 = n1 + Character.getNumericValue(conc.charAt(i));
            else
            n2 = n2 + Character.getNumericValue(conc.charAt(i));
        }
        int result = Math.abs(n1 - n2);
        if(result % 11 == 0)
        System.out.println("OUI");
        else
        System.out.println("NON");
    }
}

Post a Comment

Previous Post Next Post