<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 查找是否可以使用一個外部數字使數組元素相同 > 原文: [https://www.geeksforgeeks.org/find-whether-possible-make-array-elements-using-one-external-number/](https://www.geeksforgeeks.org/find-whether-possible-make-array-elements-using-one-external-number/) 給定一個數組,可以使用任何外部數字 x 執行三個操作。 1. 一次將 x 添加到元素 2. 一次從元素中減去 x 3. 對元素不執行任何操作 查找是否存在數字 X,以便如果使用數字 X 執行上述操作,則所得數組具有相等的元素。 如果存在數字,則打印“ YES”,并以空格分隔值,否則打印“ NO” **示例**: ``` Input : [1, 1, 3, 5, 5] Output : YES, x = 2 Explanation : The number 2 can be added to the first two elements and can be subtracted from the last two elements to obtain a common element 3 throughout the array Input : [1, 3, 5, 7, 9] Output : NO ``` 這個想法是從給定的數組中形成一組獨特的元素。 出現以下情況: 1. 唯一元素的數量為 1。x = 0 時,答案為是。 2. 唯一元素的數量為 2。x =兩個唯一元素的差,答案為是。 3. 唯一元素的數量為 3。 * 如果中值和最大值之間的差異與中值和最小值之間的差異相同,則答案為是,其中 x =中值和最大值之間或中值和最小值之間的差異。 * 否則答案為否。 在 Python 中,我們可以使用 Python 中設置的[快速查找唯一元素。](https://www.geeksforgeeks.org/sets-in-python/) ``` # Program in python 2.x to find an element X # that can be used to operate on an array and # get equal elements # Prints "YES" and an element x if we can # equalize array using x. Else prints "NO" def canEqualise(array): ????# We all the unique elements (using set ????# function). Then we sort unique elements. ????uniques = sorted(set(array)) ????# if there are only 1 or 2 unique elements, ????# then we can add or subtract x from one of them ????# to get the other element ????if len(uniques) == 1: ????????print("YES " + "0") ????elif len(uniques) == 2: ????????print("YES " + str(uniques[1] - uniques[0])) ????# If count of unique elements is three, then ????# difference between the middle and minimum ????# should be same as difference between maximum ????# and middle ????elif len(uniques) == 3: ????????if uniques[2] - uniques[1] == uniques[1] - uniques[0]: ????????????X = uniques[2] - uniques[1] ????????????print("YES " + str(X)) ????????else: ????????????print("NO") ????# if there are more than three unique elements, then ????# we cannot add or subtract the same value from all ????# the elements. ????else: ????????print("NO") # Driver code array = [55, 52, 52, 49, 52] canEqualise(array) ``` 輸出: ``` YES 3 ``` 此代碼具有復雜性 **`O(N log N)`** 可以擴展相同的問題,以要求使用兩個數字來均衡數組。 按照相同的過程,我們將需要數組中的 5 個唯一元素來要求兩個數字來使數組相等。 因此,要使用 n 個數字來均衡一個數組,我們將需要(2n + 1)個數組中的唯一元素。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看