Divisible

You are given an array of size N that contains integers. Here, is an even number. You are required to perform the following operations:

  1. Divide the array of numbers into 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.

If the newly-generated number is divisible by 11, then print OUI. Otherwise, print NON.

Example:

Input:  n = 6, a={15478, 8452, 8232, 874, 985, 4512}
Output: OUI

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int last(int n)
{
    int x;
    while (n)
    {
        x = n % 10;
        n = n / 10;
    }
    return x;
}
int first(int n)
{
    return n % 10;
}
bool divide(string sint p)
{
    int n = s.size();
    string x = "";
    x += s[0];
    x += s[1];
    int i = 2;
    int l;
    while (i < n)
    {
        stringstream sstream(x);
        int y = 0;
        sstream >> y;
        l = y % p;
        x = "";
        if (l == 0 && i < n)
        {
            x += s[i];
            i++;
            if (i < n)
                x += s[i];
            i++;
        }
        else if (l != 0 && i < n)
        {
            x += to_string(l);
            x += s[i];
            i++;
        }
    }
    stringstream sstream(x);
    int y = 0;
    sstream >> y;
    l = y % p;
    if (l == 0)
        return true;
    return false;
}

void divisible(int nint a[])
{

    string str = "";
    for (int i = 0i < n / 2i++)
    {
        int x = last(a[i]);
        str += to_string(x);
    }
    for (int i = n / 2i < ni++)
    {
        int x = first(a[i]);
        str += to_string(x);
    }

    bool flag = divide(str11);
    if (flag)
        cout << "OUI\n";
    else
        cout << "NON\n";
}
int main()
{
    int n = 6;
    int a[n] = {15478845282328749854512};

    divisible(na);

    return 0;
}


No comments:

Post a Comment