Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov
Arrays – warm up questions Given array “arr”, print its content #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=0 ; i<N ; i++ ){ printf("%d ",arr[i]); } Output:
Given array “arr”, print its content backwards #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=N-1 ; i>=0 ; i-- ){ printf("%d ",arr[i]); } Output:
Given array “arr”, print its content backwards discarding the leading zeroes int i, leading_zeros = 1; //true int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1; i>=0 ; i-- ){ if(leading_zeros && arr[i]==0) continue; printf("%d ",arr[i]); leading_zeros = 0; //false } Output: Solution #1 – straightforward
Given array “arr”, print its content backwards discarding the leading zeroes int i; int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1 ; i>=0 && arr[i]==0 ; i-- ); for( ; i>=0 ; i--){ printf("%d ",arr[i]); } Output: Solution #2 – a bit more efficient
2D Arrays To print a multiplication table:
2D Arrays To print a multiplication table: int i, j; for( i=1 ; i<=N ; i++){ for( j=1 ; j<=N ; j++){ printf("%2d ",i*j); } printf("\n"); }
2D Arrays To print the “flag of Britain”: o o o ooooooooo ooo
2D Arrays To print the “flag of Britain”: int i,j; char c; for(i=0 ; i<N; i++){ for(j=0 ; j<N ; j++){ c = (i==j || i+j==N-1 || i==N/2 || j==N/2)?'o':' '; printf("%c",c); } printf("\n"); }
Given a 2D array arr[N][N], we have to print TRUE if there is at least one sorted column, FALSE otherwise. int i,j; for( j=0 ;j<N ; ++j ) { for( i=1; i<N ;++i){ if( arr[i][j] < arr [i-1][j] ) break; } if( i==N ) {printf(“TRUE”); return 1; } } printf(“FALSE”);
Your optional challenging home- assignment To print a “number spiral”
Given two arrays, arr1 & arr2, with sorted non- repeating numbers, print the intersection of these arrays. int arr1[N] = {1,5,7,8,15,20,22,23,40,43}; int arr2[N] = {2,3,4,5, 6,11,15,19,20,43}; int i,j; for( i=j=0 ; i<N && j<N ; ){ if(arr1[i] == arr2[j]){ printf("%d ", arr1[i]); i++; j++; } else if(arr1[i] > arr2[j]) { j++; } else { i++; } } Output:
Given a sorted array of size N, efficiently find if there are 2 numbers summing up to NUM. int i,j,arr[N] = {1,4,7,8,12,19,22}; //N=7, NUM = 16 for(i=0,j=N-1; i<j; ){ if(arr[i] + arr[j] == NUM){ printf("%d + %d = %d\n",arr[i],arr[j], NUM); return 0; }else if(arr[i] + arr[j] > NUM){ j--; }else{ i++; } } printf("There are no such numbers..."); Output: = 16
Medium to Hard questions Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number. int i, arr[N] = {1,6,2,5,0,3,7}; //N=7, values=0..7, missing "4" int count[N+1] = {0}; for(i=0;i<N;i++){ count[ arr[i] ] = 1; } for( i=0 ; i<N+1 ; i++){ if(count[i]==0) printf("The missing number is %d\n",i); } Output: The missing number is 4 Version 1 Complicated…
int i,arr[N-1] = {1,6,2,5,0,3}; //N=7, missing "4" int count = 0; for( i=0 ; i<N-1 ; i++ ){ count += arr[i]; } printf("The missing number is: %d\n",(N-1)*N/2-count); Output: The missing number is 4 Version 2 The tricky one Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number.
Given an array of size N with random values which might be negative, find the sum of a maximal sequence. int i,arr[N] = {7, -3, -8, 5, -1, 10, -19, 6, 6, -4}; //N=10 int currSum=0, maxSum = arr[0]; for(i=0; i<N; i++ ){ currSum +=arr[i]; if(currSum > maxSum){ maxSum = currSum; } if(currSum < 0){ currSum = 0; } } printf(“Max sum = %d\n",maxSum); Output: Max sum = 14
Given an array of size N with values between 0..99, find the number that appears most frequently. int arr[N] = {0, 6, 81, 6, 92, 1, 34, 0, 37, 6}; //N=10 int count[100] = {0}; int i, maxIndex, maxCount=0; for (i=0; i<N; ++i) { count[ arr[i] ]++; } for (i=0; i<100; ++i) { if (count[i] > maxCount) { maxCount = count[i]; maxIndex = i; } printf("Max=%d appeared %d times\n", maxIndex, maxCount); Output: Max=6 appeared 3 times
Given 2 arrays of small letters of size N, we must decide if one is a permutation of another. char arr1[N] = {'a','b','c','d'}, arr2[N] = {'b','d','c','a'}; int count[26] = {0}, i; for(i=0 ; i < N ; i++) { count[ arr1[i] - 'a']++; count[ arr2[i] - 'a']--; } for (i=0 ; i<26 ; ++i){ if (count[i] != 0){ printf("Illegal permutation"); return 0; } printf("Legal permutation"); return 1; Output: Legal permutation
Given an array of size N (N is even), we need to find the minimum and the maximum values by using at most 3N/2 cells comparisons. int i,min,max,tmpMin,tmpMax, arr[N] = {7,3,2,5,8,3,4,9}; //N=8 min=max=arr[0]; for(i = 0; i < N-1 ; i+=2){ if(arr[i] > arr[i+1]){ tmpMin = arr[i+1]; tmpMax = arr[i]; }else{ tmpMin = arr[i]; tmpMax = arr[i+1]; } if(tmpMin < min) min = tmpMin; if(tmpMax > max) max = tmpMax; } printf("min = %d, max = %d",min,max); Output: min = 2, max = 9