Тёмный

1502. Can Make Arithmetic Progression From Sequence | CPP | LEETCODE POTD TOADY | Deepak Joshi 

Deepak Joshi
Подписаться 358
Просмотров 59
50% 1

LinkdIn : / deepak-joshi-a15b21228
1502. Can Make Arithmetic Progression From Sequence : leetcode.com/p...

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 4   
@JohnSmith-uu5gp
@JohnSmith-uu5gp Год назад
Nice Explanation!!! But try to do without sorting / using formula - instead of loop.
@deepak_joshi_the_conqurer_
@deepak_joshi_the_conqurer_ Год назад
Hello John After recording this video I have came to a new approach of solving it by using a set : class Solution { public: bool canMakeArithmeticProgression(vector& arr) { int minValue = *min_element(arr.begin(), arr.end()); int maxValue = *max_element(arr.begin(), arr.end()); int n = arr.size(); if (maxValue - minValue == 0) { return true; } if ((maxValue - minValue) % (n - 1) != 0) { return false; } int diff = (maxValue - minValue) / (n - 1); unordered_set numberSet; for (int i = 0; i < n; i++) { if ((arr[i] - minValue) % diff != 0) { return false; } numberSet.insert(arr[i]); } return numberSet.size() == n; } };
@deepak_joshi_the_conqurer_
@deepak_joshi_the_conqurer_ Год назад
In this approach the Time complexity as well as space complexity is O(n) better than the previous one O(nlogn)
@JohnSmith-uu5gp
@JohnSmith-uu5gp Год назад
​@@deepak_joshi_the_conqurer_ Nice, but still you can optimize it, here space complexity still O(n) , we can solve this question without taking O(n) we can do it in O(1) space complexity by finding the correct AP place in array if it's not correct position then we can swipe it to make correct ap array, so I suggest you when you make the video please try to focus the thought or intuition process brutefroce to optimalest approach becuse ""code too kovii karlega but approch or think process zarurii haii na vaiii kaise orvi optamize karsakte haii --- ekdom brutefroce" your videos was really fantastic just keep in consideration my word when you provide a solution Infront of audience, Nice bro your daily dedication really appreciable.