fork(6) download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int array[100], i, num,diff, resDiff, startIndx, resStartIndx, countCur, countPre;
  6. printf("Enter the size of an array \n");
  7. scanf("%d", &num);
  8.  
  9. printf("Enter the elements of the array \n");
  10. for (i = 0; i < num; i++) {
  11. scanf("%d", &array[i]);
  12. }
  13.  
  14. //Now code changes
  15. startIndx =0, resStartIndx=0, countPre=0, resDiff=0;
  16. for (i = 0; i < num; /*No increment required here*/) {
  17. countCur =0;
  18. startIndx=i;
  19. countCur++;
  20.  
  21. if(++i < num)
  22. {
  23. diff = array[i] - array[startIndx];
  24. countCur++;
  25. i++;
  26. while((i < num) && (diff == (array[i] - array[i-1])))
  27. {
  28. countCur++;
  29. i++;
  30. }
  31.  
  32. if(countCur > countPre)
  33. {
  34. resStartIndx = startIndx;
  35. printf("%d | %d | %d | %d\n",array[resStartIndx], resStartIndx, startIndx, countCur);
  36. countPre = countCur;
  37. resDiff = diff;
  38. }
  39. }
  40. }
  41.  
  42. countPre += resStartIndx;
  43. printf("\n Numbers in a.p: ");
  44. for (i = resStartIndx; i < countPre; i++) {
  45. printf("%d, ", array[i]);
  46. }
  47. printf("\n Common difference:%d", resDiff);
  48. return 0;
  49. }
Success #stdin #stdout 0s 10320KB
stdin
7 5 1 2 4 6 8 12
stdout
Enter the size of an array 
Enter the elements of the array 
5 | 0 | 0 | 2
2 | 2 | 2 | 4

 Numbers in a.p: 2, 4, 6, 8, 
 Common difference:2