Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在将Java更改为伪代码时遇到问题_Java_Algorithm_Pseudocode - Fatal编程技术网

我在将Java更改为伪代码时遇到问题

我在将Java更改为伪代码时遇到问题,java,algorithm,pseudocode,Java,Algorithm,Pseudocode,给定两个不同元素的未排序数组,任务是从两个数组中查找其和等于x的所有对。 示例: Input : arr1[] = {-1, -2, 4, -6, 5, 7} arr2[] = {6, 3, 4, 0} x = 8 Output : 4 4 5 3 Input : arr1[] = {1, 2, 4, 5, 7} arr2[] = {5, 6, 3, 4

给定两个不同元素的未排序数组,任务是从两个数组中查找其和等于x的所有对。 示例:

    Input :  arr1[] = {-1, -2, 4, -6, 5, 7}
             arr2[] = {6, 3, 4, 0}  
             x = 8
    Output : 4 4
             5 3
    
    Input : arr1[] = {1, 2, 4, 5, 7} 
            arr2[] = {5, 6, 3, 4, 8}  
            x = 9
    Output : 1 8
             4 5
             5 4
这个问题的一个有效解决方案是散列。我们将所有第一个数组元素存储在哈希表中。对于第二个数组的元素,我们从x中减去每个元素,并在哈希表中检查结果。如果结果存在,我们将在哈希中打印元素和键(这是第一个数组的元素)。 代码如下:

    // JAVA Code for Given two unsorted arrays,  
    // find all pairs whose sum is x 
    import java.util.*; 
      
    class GFG { 
      
         // Function to find all pairs in both arrays 
        // whose sum is equal to given value x 
        public static void findPairs(int arr1[], int arr2[], 
                                     int n, int m, int x) 
        { 
            // Insert all elements of first array in a hash 
            HashMap<Integer, Integer> s = new HashMap<Integer, Integer>(); 
              
            for (int i = 0; i < n; i ++) 
                s.put(arr1[i], 0); 
           
            // Subtract sum from second array elements one 
            // by one and check it's present in array first 
            // or not 
            for (int j = 0; j < m; j ++) 
                if (s.containsKey(x - arr2[j])) 
                    System.out.println(x - arr2[j] + " " + arr2[j]);              
        } 
          
        /* Driver program to test above function */
        public static void main(String[] args)  
        { 
            int arr1[] = {1, 0, -4, 7, 6, 4}; 
            int arr2[] = {0 ,2, 4, -3, 2, 1}; 
            int x = 8; 
              
            findPairs(arr1, arr2, arr1.length, arr2.length, x); 
           
        } 
      } 
//给定两个未排序数组的JAVA代码,
//查找总和为x的所有对
导入java.util.*;
类GFG{
//函数查找两个数组中的所有对
//其和等于给定值x
公共静态无效findPairs(int arr1[],int arr2[],
整数n,整数m,整数x)
{ 
//在散列中插入第一个数组的所有元素
HashMap s=新的HashMap();
对于(int i=0;i

我想问的问题是:这段代码的时间复杂度是多少?如何编写此Java代码的伪代码?

鉴于您的算法必须遍历第二个数组中的所有元素才能找到解决方案,您的最坏情况(或所有情况)是n,其中n是第二个数组的长度。因此,你有一个O(n)算法

关于编写伪代码:

您在对算法的描述中基本上已经这样做了,但是在算法中编写一个正式的步骤列表可能更具建设性。通常,您可以通过编写独立于任何编程语言的算法每个步骤的抽象描述来实现这一点

例如,不必为特定于语言的(int j=0;j编写
,您可以编写“遍历arr2中的所有元素”