Skip to content

Conversation

@ashmod
Copy link

@ashmod ashmod commented May 12, 2023

The problem is that your code has a time complexity due to the nested loops.
For an array of size 10^5, this would require up to 10^10 operations, which is too many for a program with a 2-second time limit.

My proposed solution would be to check for the minimum element in the array, and then check if all the elements are divisible by that minimum element.
If there is a number in the array that divides all numbers, it must divide the minimum element as well.

Here is one way to implement it:

/*
The problem is that your code has a time complexity due to the nested loops. 
For an array of size 10^5, this would require up to 10^10 operations, which is too many for a program with a 2-second time limit.

My proposed solution would be to check for the minimum element in the array, and then check if all the elements are divisible by that minimum element.
If there is a number in the array that divides all numbers, it must divide the minimum element as well.
*/

#include <iostream>
#include <climits> //this is for the INT_MAX constant
using namespace std;

int main()
{
    const int max_capacity = 10e5;
    int arr[max_capacity];
    int min_element = INT_MAX; //we initalize the min_element to the maximum possible value of an integer so that later, we can replace it with the minimum element in the array :)
    bool isDivisible = true; //this is the flag in your interpretation

    int actual_size;
    cin >> actual_size;

    for (int i = 0; i < actual_size; i++) {
        cin >> arr[i];
        if (arr[i] < min_element) {
            min_element = arr[i];
        }
    }

    for (int i = 0; i < actual_size; i++) {
        if (arr[i] % min_element != 0) {
            isDivisible = false;
            break;
        }
    }

    if (isDivisible) {
        cout << min_element;
    }
    else {
        cout << -1;
    }

    return 0;
}

Feel free to ask follow-ups. Good luck coding!

@ghost ghost closed this by deleting the head repository May 12, 2023
@ghost
Copy link

ghost commented May 12, 2023

It takes long time to find your comment, thank you shehab for your helping but may you simlify the explanation a little more because i didn't get what you want to do.. if you don't have the time to do so, feel free not to reply... i'll reread it in another time..

@ghost
Copy link

ghost commented May 12, 2023

I think i begin to understand what you meant to do,
For the division to give integer number the numenerator must be greater than the denomenator, so you get the minimum nuber and chech its condition only, not as i made check for each element, right?
But the last edit for my code was to check this condition before entering the loop of division so i thought cheking the condition is less im time than taking modules of numbers
Thanks a lot for your help

@ghost
Copy link

ghost commented May 12, 2023

i got it now, thank you for your help!
one last question,
i know that this ideal code is a reason for long experience in the field of programming.. actually i just want to know a little of them, so my question is how?? i think your answer will be practicing but in my humble opinion this will take a life time without a mentor.. so do u have other suggestions, reading codes from specific references or sth like that??
i'll say it to you again, if you don't have the time to answer me, feel free not to reply

@ghost
Copy link

ghost commented May 12, 2023

Very grateful for you.. shokraaan

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant