Program to find subarray from array in C
For example
a={1,2,3,4}
Subarray of array is
1,12,123,1234,2,23,234,3,34,4
That is n(n+1)/2
4(4+1)/2=10
Code
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
for(k=i;k<=j;k++)
{
printf("%d",a[k]);
}
printf("\n");
}
}
C code to find subarray from array
#include <stdio.h>
int main() {
int a[4]={1,2,3,4};
int i,j,k;
int n=4;
printf("subarray of a={1,2,3,4}");
printf("\n");
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
for(k=i;k<=j;k++)
{
printf("%d",a[k]);
}
printf("\n");
}
}
}
Output
subarray of a={1,2,3,4}
1
12
123
1234
2
23
234
3
34
4
0 Comments