📌  相关文章
📜  根据给定的约束将数组分为相等和的两部分

📅  最后修改于: 2021-04-26 05:19:45             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是选择一个整数x (该数组可能存在或可能不存在)并将其所有出现的内容从数组中删除,并将剩余的数组划分为两个非空子数组-设置为:

  1. 第一组的元素严格小于x
  2. 第二组的元素严格大于x
  3. 两组元素之和相等。
      如果存在这样的整数,则打印“是”,否则打印“否”

      例子:

      方法:想法是首先对数组进行排序,然后对介于1到数组中存在的最大数字之间的所有数字进行二进制搜索,并检查是否从数组中删除了所有出现的元素后,元素在其左侧的总和( (小于此值),并且右侧出现的元素之和(大于此值)相等。

      下面是上述方法的实现:

      C++
      // C++ implementation of the approach
      #include 
      using namespace std;
        
      // Function that checks if the given
      // conditions are satisfied
      void IfExists(int arr[], int n)
      {
          // To store the prefix sum
          // of the array elements
          int sum[n];
        
          // Sort the array
          sort(arr, arr + n);
        
          sum[0] = arr[0];
        
          // Compute the prefix sum array
          for (int i = 1; i < n; i++)
              sum[i] = sum[i - 1] + arr[i];
        
          // Maximum element in the array
          int max = arr[n - 1];
        
          // Variable to check if there exists any number
          bool flag = false;
        
          for (int i = 1; i <= max; i++) {
        
              // Stores the index of the largest
              // number present in the array
              // smaller than i
              int findex = 0;
        
              // Stores the index of the smallest
              // number present in the array
              // greater than i
              int lindex = 0;
        
              int l = 0;
              int r = n - 1;
        
              // Find index of smallest number
              // greater than i
              while (l <= r) {
                  int m = (l + r) / 2;
        
                  if (arr[m] < i) {
                      findex = m;
                      l = m + 1;
                  }
                  else
                      r = m - 1;
              }
        
              l = 1;
              r = n;
              flag = false;
        
              // Find index of smallest number
              // greater than i
              while (l <= r) {
                  int m = (r + l) / 2;
        
                  if (arr[m] > i) {
                      lindex = m;
                      r = m - 1;
                  }
                  else
                      l = m + 1;
              }
        
              // If there exists a number
              if (sum[findex] == sum[n - 1] - sum[lindex - 1]) {
                  flag = true;
                  break;
              }
          }
        
          // If no such number exists
          // print no
          if (flag)
              cout << "Yes";
          else
              cout << "No";
      }
        
      // Driver code
      int main()
      {
          int arr[] = { 1, 2, 2, 5 };
          int n = sizeof(arr) / sizeof(int);
          IfExists(arr, n);
        
          return 0;
      }


      Java
      // Java implementation of the approach 
      import java.util.*;
        
      class GFG
      {
        
      // Function that checks if the given 
      // conditions are satisfied 
      static void IfExists(int arr[], int n) 
      { 
          // To store the prefix sum 
          // of the array elements 
          int sum[] = new int[n]; 
        
          // Sort the array 
          Arrays.sort(arr); 
        
          sum[0] = arr[0]; 
        
          // Compute the prefix sum array 
          for (int i = 1; i < n; i++) 
              sum[i] = sum[i - 1] + arr[i]; 
        
          // Maximum element in the array 
          int max = arr[n - 1]; 
        
          // Variable to check if there exists any number 
          boolean flag = false; 
        
          for (int i = 1; i <= max; i++) 
          { 
        
              // Stores the index of the largest 
              // number present in the array 
              // smaller than i 
              int findex = 0; 
        
              // Stores the index of the smallest 
              // number present in the array 
              // greater than i 
              int lindex = 0; 
        
              int l = 0; 
              int r = n - 1; 
        
              // Find index of smallest number 
              // greater than i 
              while (l <= r) 
              { 
                  int m = (l + r) / 2; 
        
                  if (arr[m] < i) 
                  { 
                      findex = m; 
                      l = m + 1; 
                  } 
                  else
                      r = m - 1; 
              } 
        
              l = 1; 
              r = n; 
              flag = false; 
        
              // Find index of smallest number 
              // greater than i 
              while (l <= r) 
              { 
                  int m = (r + l) / 2; 
        
                  if (arr[m] > i) 
                  { 
                      lindex = m; 
                      r = m - 1; 
                  } 
                  else
                      l = m + 1; 
              } 
        
              // If there exists a number 
              if (sum[findex] == sum[n - 1] - sum[lindex - 1]) 
              { 
                  flag = true; 
                  break; 
              } 
          } 
        
          // If no such number exists 
          // print no 
          if (flag) 
              System.out.println("Yes"); 
          else
              System.out.println("No"); 
      } 
        
      // Driver code 
      public static void main(String args[])
      { 
          int arr[] = { 1, 2, 2, 5 }; 
          int n = arr.length; 
          IfExists(arr, n); 
      }
      } 
        
      // This code is contributed by Arnab Kundu


      Python3
      # Python3 implementation of the approach 
        
      # Function that checks if the given 
      # conditions are satisfied 
      def IfExists(arr, n) :
        
          # To store the prefix sum 
          # of the array elements 
          sum = [0] * n; 
        
          # Sort the array 
          arr.sort(); 
        
          sum[0] = arr[0]; 
        
          # Compute the prefix sum array 
          for i in range(1, n) : 
              sum[i] = sum[i - 1] + arr[i]; 
        
          # Maximum element in the array 
          max = arr[n - 1]; 
        
          # Variable to check if there 
          # exists any number 
          flag = False; 
        
          for i in range(1, max + 1) :
        
              # Stores the index of the largest 
              # number present in the array 
              # smaller than i 
              findex = 0; 
        
              # Stores the index of the smallest 
              # number present in the array 
              # greater than i 
              lindex = 0; 
        
              l = 0; 
              r = n - 1; 
        
              # Find index of smallest number 
              # greater than i 
              while (l <= r) :
                  m = (l + r) // 2; 
        
                  if (arr[m] < i) :
                      findex = m; 
                      l = m + 1; 
                    
                  else :
                      r = m - 1; 
                
              l = 1; 
              r = n; 
              flag = False; 
        
              # Find index of smallest number 
              # greater than i 
              while (l <= r) : 
                  m = (r + l) // 2; 
        
                  if (arr[m] > i) : 
                      lindex = m; 
                      r = m - 1; 
                    
                  else :
                      l = m + 1; 
                
              # If there exists a number 
              if (sum[findex] == sum[n - 1] - 
                                 sum[lindex - 1]) : 
                  flag = True; 
                  break; 
                
          # If no such number exists 
          # print no 
          if (flag) : 
              print("Yes"); 
          else :
              print("No"); 
        
      # Driver code 
      if __name__ == "__main__" : 
        
          arr = [ 1, 2, 2, 5 ]; 
            
          n = len(arr) ;
          IfExists(arr, n); 
            
      # This code is contributed by Ryuga


      C#
      // C# implementation of the approach 
      using System;
        
      class GFG
      {
            
      // Function that checks if the given 
      // conditions are satisfied 
      static void IfExists(int[] arr, int n) 
      { 
          // To store the prefix sum 
          // of the array elements 
          int[] sum = new int[n]; 
        
          // Sort the array 
          Array.Sort(arr); 
        
          sum[0] = arr[0]; 
        
          // Compute the prefix sum array 
          for (int i = 1; i < n; i++) 
              sum[i] = sum[i - 1] + arr[i]; 
        
          // Maximum element in the array 
          int max = arr[n - 1]; 
        
          // Variable to check if there exists any number 
          bool flag = false; 
        
          for (int i = 1; i <= max; i++) 
          { 
        
              // Stores the index of the largest 
              // number present in the array 
              // smaller than i 
              int findex = 0; 
        
              // Stores the index of the smallest 
              // number present in the array 
              // greater than i 
              int lindex = 0; 
        
              int l = 0; 
              int r = n - 1; 
        
              // Find index of smallest number 
              // greater than i 
              while (l <= r) 
              { 
                  int m = (l + r) / 2; 
        
                  if (arr[m] < i) 
                  { 
                      findex = m; 
                      l = m + 1; 
                  } 
                  else
                      r = m - 1; 
              } 
        
              l = 1; 
              r = n; 
              flag = false; 
        
              // Find index of smallest number 
              // greater than i 
              while (l <= r) 
              { 
                  int m = (r + l) / 2; 
        
                  if (arr[m] > i) 
                  { 
                      lindex = m; 
                      r = m - 1; 
                  } 
                  else
                      l = m + 1; 
              } 
        
              // If there exists a number 
              if (sum[findex] == sum[n - 1] - sum[lindex - 1]) 
              { 
                  flag = true; 
                  break; 
              } 
          } 
        
          // If no such number exists 
          // print no 
          if (flag) 
              Console.WriteLine("Yes"); 
          else
              Console.WriteLine("No"); 
      } 
        
      // Driver code 
      public static void Main()
      { 
          int[] arr = { 1, 2, 2, 5 }; 
          int n = arr.Length; 
          IfExists(arr, n); 
      }
      } 
        
      // This code is contributed by Code_Mech.


      PHP
       $i) 
                  {
                      $lindex = $m;
                      $r = $m - 1;
                  }
                  else
                      $l = $m + 1;
              }
        
              // If there exists a number
              if ($sum[$findex] == $sum[$n - 1] -
                                   $sum[$lindex - 1])
              {
                  $flag = true;
                  break;
              }
          }
        
          // If no such number exists
          // print no
          if ($flag == true)
              echo "Yes";
          else
              echo "No";
      }
        
      // Driver code
      $arr = array(1, 2, 2, 5 );
      $n = sizeof($arr);
      IfExists($arr, $n);
        
      // This code is contributed by ihritik
      ?>


      输出:
      Yes