File tree 1 file changed +65
-0
lines changed
November 2024 GFG SOLUTION
1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ //{ Driver Code Starts
2
+ #include <limits.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+
8
+ // } Driver Code Ends
9
+ // User function Template for C
10
+
11
+ // Function to find the smallest positive number missing from the array.
12
+ int missingNumber (int arr [], int n ) {
13
+ for (int i = 0 ; i < n ; i ++ ) {
14
+ while (arr [i ] > 0 && arr [i ] <= n && arr [i ] != arr [arr [i ] - 1 ]) {
15
+ int temp = arr [i ];
16
+ arr [i ] = arr [arr [i ] - 1 ];
17
+ arr [temp - 1 ] = temp ;
18
+ }
19
+ }
20
+ for (int i = 0 ; i < n ; i ++ ) {
21
+ if (arr [i ] != i + 1 ) {
22
+ return i + 1 ;
23
+ }
24
+ }
25
+ return n + 1 ;
26
+ }
27
+
28
+
29
+ //{ Driver Code Starts.
30
+
31
+ int main () {
32
+ char ts [1001 ];
33
+ fgets (ts , sizeof (ts ), stdin );
34
+ int t = atoi (ts );
35
+ while (t -- ) {
36
+ char * line = NULL ;
37
+ size_t len = 0 ;
38
+ long read = getline (& line , & len , stdin );
39
+
40
+ int * nums = (int * )malloc (1000000 * sizeof (int ));
41
+ int n = 0 ;
42
+ char * token = strtok (line , " " );
43
+ while (token != NULL ) {
44
+ nums [n ++ ] = atoi (token );
45
+ token = strtok (NULL , " " ); // Get the next token
46
+ }
47
+ int arr [n ];
48
+ for (int i = 0 ; i < n ; i ++ ) {
49
+ arr [i ] = nums [i ];
50
+ }
51
+ // char ch;
52
+ // scanf("%c", &ch);
53
+ // getchar();
54
+ // Check if the array is sorted using the previously defined function
55
+ int ans = missingNumber (arr , n );
56
+ printf ("%d\n" , ans );
57
+
58
+ printf ("%s\n" , "~" );
59
+ free (nums ); // Free dynamically allocated memory for the nums array
60
+ free (line ); // Free dynamically allocated memory for the input line
61
+ }
62
+
63
+ return 0 ; // Return 0 to indicate successful execution
64
+ }
65
+ // } Driver Code Ends
You can’t perform that action at this time.
0 commit comments