Guest User

Untitled

a guest
Aug 20th, 2019
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1) [1,2]
  2. 2) [1,2,2]
  3. 3) [1,2,2,3]
  4. 4) [1,2,2,3,3]
  5. 5) [2,2]
  6. 6) [2,2,3]
  7. 7) [2,2,3,3]
  8. 8) [2,3]
  9. 9) [2,3,3]
  10. 10) [3,3]
  11.  
  12. # include <stdio.h>
  13. # include <stdlib.h>
  14. # include <stdbool.h>
  15.  
  16. bool check(int *, int *, int, int);
  17.  
  18.  
  19. int main()
  20. {
  21. int number; // Variable name "number" which will specify the size of dynamically allocated array.
  22.  
  23. printf("Enter the size of your arrayn");
  24. scanf("%d",&number);
  25.  
  26. int *array;
  27. array = (int*)calloc(number, sizeof(int)); // Dynamically allocated memory.
  28.  
  29.  
  30. int *temp_array;
  31. temp_array = (int*)calloc(number, sizeof(int)); // Temporary variable of original array.
  32.  
  33. int i,j=0; // Counter variables for loop.
  34.  
  35. printf("Enter the elements of arraysn");
  36.  
  37. for(i = 0; i < number; i++)
  38. {
  39. scanf("%d",(array + i)); //Main original array being filled.
  40. }
  41.  
  42. for(i = 0; i < number; i++)
  43. {
  44. *(temp_array + i) = *(array + i); //Copying into temp.
  45. }
  46.  
  47.  
  48.  
  49. for(i = 0; i < number; i++)
  50. {
  51. for( j = i + 1 ; j < number; j++)
  52. {
  53. if( *(temp_array + i ) == *(temp_array + j))
  54. {
  55. *(temp_array + j) = 0; // My way of removing those numbers which are the repeated. (Assigning them value of zero).
  56. }
  57. }
  58. }
  59.  
  60. i=0;j=0;
  61.  
  62. int sub_number = 0;
  63.  
  64. while(i < number)
  65. {
  66. if(*(temp_array + i) != 0)
  67. {
  68. sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
  69. }
  70. i++;
  71. }
  72.  
  73. int *sub_array ;
  74. sub_array = (int*)calloc(sub_number,sizeof(int));
  75.  
  76. j=0;
  77. for(i = 0;i < number ;i++)
  78. {
  79. if( *(temp_array + i ) != 0)
  80. {
  81. *(sub_array + j) = * (temp_array + i ); //Transferring all the distinct values from temp_array to sub_array.
  82. j++;
  83. }
  84. }
  85.  
  86. free(temp_array); //Freed "temp_array". No longer needed.
  87. temp_array = NULL;
  88.  
  89. for(i = 0;i < sub_number; i++)
  90. {
  91. printf("%d ",*(sub_array + i)); // Desired array which only contains distinct and unique variables.
  92. }
  93.  
  94. printf("n");
  95.  
  96. int ans = 0; //Variable which shall calculate the answer.
  97. int k=0; //New variable counter
  98. j=0;
  99.  
  100.  
  101. for(i=0; i < number; i++) //This loop will traverse variable "i" on array "array".
  102. {
  103. k = i;
  104. while(k < number) //This loop will traverse variable "k" on array "array"
  105. {
  106. int *new_array;
  107. new_array = (int*) calloc ((k-i+1),sizeof(int));
  108.  
  109. for(j = i; j <= k; j++) //This loop will assign the subset values of array "array" to array "new_array".
  110. {
  111. *(new_array + (j - i)) = *(array + j);
  112. }
  113. if(check(new_array, sub_array, (k-i+1), sub_number) == true) //This will check if ALL the values in "sub_array" are present in "new_array" or not.
  114. {
  115. ans++;
  116. }
  117.  
  118. free(new_array);
  119. new_array = NULL;
  120. k++;
  121. }
  122. }
  123.  
  124. printf("%d",ans);
  125.  
  126. return 0;
  127. }
  128.  
  129. bool check(int * new_array, int *sub_array, int new_number, int sub_number) //Function to check if ALL the values in "sub_array" are present in "new_array" or not.
  130. {
  131. int i = 0;
  132. int j = 0;
  133. for(i = 0; i < new_number; i++) //new_number is nothing but (k - i + 1)
  134. {
  135. if(*(new_array + i) == *(sub_array + j))
  136. {
  137. j++;
  138. if(j == sub_number)
  139. {
  140. return true;
  141. }
  142. i = -1;
  143. }
  144. }
  145. return false;
  146. }
RAW Paste Data