Missing ranges of numbers | Practice | GeeksforGeeks

Given an array Arr of N positive integers, find the missing elements (if any) in the range 0 to max of Arri. 

Example 1:
Input:
N = 5
Arr[] = {62, 8, 34, 5, 332}
Output: 0-4 6-7 9-33 35-61 63-331
Explanation: Elements in the range 0-4, 6-7, 
9-33, 35-61 and 63-331 are not present.


Example 2:
Input:
N = 4
Arr[] = {13, 0, 32, 500}
Output: 1-12 14-31 33-499
Explanation: Elements in the range 1-12, 
14-31 and 33-499 are not present.


Your Task:
You don't need to read input or print anything. Your task is to complete the function findMissing() which takes the array of integers arr and its size n as input parameters and returns a string denoting the missing elements. If
there are more than one missing, collate them using hyphen (-) and separate each different range with a space. If there are no missing element then return "-1".

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints
1 <= N <= 107
0 <= Arri <= 107
-3
ankita12apr7 months ago
String findMissing(int[] arr, int n) {       // code here       Arrays.sort(arr);       String str ="";       if(arr[0] == 0 && n == 1){           return "-1";       }       if(arr[0] == 1 && n == 1){           return "0";       }       if(arr[0] != 0){           int x = arr[0]-1;           if(x>0){               str += "0"+"-"+x+" ";           }       }       if(arr[0] == 1){           str += "0"+" ";       }       for(int i = 0 ; i< n-1 ; i++){           int start = arr[i]+1;           int end = arr[i+1]-1;           if(start<end){               str += start+"-"+end+" ";           }           if(end == start){               str += end+" ";           }       }       if(str == ""){           return "0";       }       return str;   }
0
pankajkumarravi8 months ago
// Tried my self working fine accept 1 case- you are Welcome for suggestion 
class Solution {         String findMissing(int[] arr, int n) {           // code here           Arrays.sort(arr);           String resp = "";           if (arr[0] ==1)               resp =resp+"0";           else if (arr[0]>arr[1])               resp =resp+"-1";           else {               for (int i = 0; i < n - 1; i++) {                   if (i == 0 && arr[0] != 1) {                       resp = resp + printValBwRange(0, arr[i] - 1);                   }                   resp = resp + printValBwRange(arr[i] + 1, arr[i + 1] - 1);               }           }               return resp;
       }
       // print values b/w range       public static String printValBwRange(int start, int end) {           if (start <= end) {               return " " + start + " - " + end;           }           return "-1";       }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.