Advertisement
dmilicev

smallest positive integer that does not occur v2.c

Nov 23rd, 2021 (edited)
774
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     smallest positive integer that does not occur v2.c
  4.  
  5.     Version without array sort.
  6.  
  7.     Task:
  8.     https://www.facebook.com/groups/cprogramming123/permalink/4468570053231257/
  9.  
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. // Function to print array arr of n integers
  21. void PrintArray(char text[],int arr[],int n){
  22.     int i;
  23.  
  24.     printf("%s",text);
  25.     for(i=0;i<n;i++)
  26.         printf("%5d", arr[i]);
  27.  
  28.     printf("\n\n");
  29. }
  30.  
  31. // If num exist in arr of n integers, returns 1. Otherwise returns 0.
  32. int ifExist( int num, int arr[], int n){
  33.     int i;
  34.  
  35.     for(i=0; i<n; i++){
  36.         if( arr[i]==num )
  37.             return 1;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int solution( int arr[], int n){
  43.     int num=1;
  44.  
  45.     while( num <= 1000000 )
  46.         if( ifExist( num, arr, n) == 0 )
  47.             return num;
  48.         else
  49.             num++;
  50.  
  51.     return num;
  52. }
  53.  
  54.  
  55. int main(){
  56.     int arr[] = {1,3,6,4,1,2};
  57. //    int arr[] = {1, 2, 3};
  58. //    int arr[] = {-1, -3};
  59. //    int arr[] = {-1, 1, 2, -3};
  60. //    int arr[] = {-1, 4, -3, 1, 2};
  61. //    int arr[] = {-1, 2, -3, 4, 1};
  62. //    int arr[] = {0, 0, 0, 0};
  63.     int n=sizeof(arr)/sizeof(arr[0]);
  64.  
  65.     printf("\n n = %d \n", n);
  66.  
  67.     PrintArray("\n        arr = ", arr, n);
  68.  
  69.     printf("\n\n Smallest positive integer that does not occur in arr is %d . \n\n", solution(arr,n) );
  70.  
  71.     return 0;
  72. }
  73.  
Advertisement
RAW Paste Data Copied
Advertisement