Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- smallest positive integer that does not occur v2.c
- Version without array sort.
- Task:
- https://www.facebook.com/groups/cprogramming123/permalink/4468570053231257/
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h>
- // Function to print array arr of n integers
- void PrintArray(char text[],int arr[],int n){
- int i;
- printf("%s",text);
- for(i=0;i<n;i++)
- printf("%5d", arr[i]);
- printf("\n\n");
- }
- // If num exist in arr of n integers, returns 1. Otherwise returns 0.
- int ifExist( int num, int arr[], int n){
- int i;
- for(i=0; i<n; i++){
- if( arr[i]==num )
- return 1;
- }
- return 0;
- }
- int solution( int arr[], int n){
- int num=1;
- while( num <= 1000000 )
- if( ifExist( num, arr, n) == 0 )
- return num;
- else
- num++;
- return num;
- }
- int main(){
- int arr[] = {1,3,6,4,1,2};
- // int arr[] = {1, 2, 3};
- // int arr[] = {-1, -3};
- // int arr[] = {-1, 1, 2, -3};
- // int arr[] = {-1, 4, -3, 1, 2};
- // int arr[] = {-1, 2, -3, 4, 1};
- // int arr[] = {0, 0, 0, 0};
- int n=sizeof(arr)/sizeof(arr[0]);
- printf("\n n = %d \n", n);
- PrintArray("\n arr = ", arr, n);
- printf("\n\n Smallest positive integer that does not occur in arr is %d . \n\n", solution(arr,n) );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement