ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
If we have an array of Interger values, find out a sub array
which has a maximum value of the array and start and end
positions of the array..The sub array must be contiguious.
Take the start add to be 4000.

For Ex if we have an array arr[] =
{-1,-2,-5,9,4,3,-6,8,7,6,5,-3}

here the sub array of max would be
{8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 =
26.The start and end position is 4014(8) and 4020(5).
 Question Submitted By :: Sunil.valli
I also faced this Question!!     Rank Answer Posted By  
 
  Re: If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
Answer
# 1
# include<stdio.h>
int main()
{
       int i,j,k,sum[30],l,z =0,num = 0,sub,len;
       int a[30],index[15][15],temp;
       sum[z] = 0;
       printf("\n Enter the Max.no. of elements to be 
entered in the array  :\n");
       scanf("%d",&len);
       printf("\n Enter the array Values : \n");
       for(i=0;i<len;i++)
       scanf("%d",&a[i]);
       for(i = 0;i <len;i++)
       {
           k = len/2;
           l = i+k;
           for(j=i;j<l;j++)
           if(j<len)
           {
               num++;
           }
           if(num == k)
           {
               for(j =i;j<l;j++)
               {
                 if(j<len)
                    sum[z] = sum[z]+a[j];
                else printf("\n num <3!");
               }
               z++;
               sum[z] = 0;
           }
           num = 0;
       }
       j = 0;
       for(i = 0 ;i <z;i++)
       {
          printf(" sum[%d] = %d\n",i,sum[i]);
          index[j][i] = sum[i];
       }
       for(i = 0;i<z;i++)
       {
           for(j = i+1;j<z;j++)
           if(sum[i] < sum[j])
           {
               temp = sum[i];
               sum[i] = sum[j];
               sum[j] = temp;
           }
     }
      j= 0;
      for(i = 0;i<z;i++)
      if(index[j][i] == sum[0])
      sub = i;
      printf(" Sub array of max. sum = { ");
      for(i = sub;i<sub+k;i++)
      printf(" %d  ",a[i]);
      printf("}");
  return 0;

}
 
Is This Answer Correct ?    1 Yes 1 No
Sowmya
 
  Re: If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
Answer
# 2
Well this is not an answer to the question but the solution 
given in the example is wrong. Wont the subarray that gives 
the max sum be {9,4,3,-6,8,7,6,5} whose sum is 36???
 
Is This Answer Correct ?    7 Yes 0 No
Monica
 
 
 
  Re: If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
Answer
# 3
Another approach

# include<stdio.h>

struct index { 
int sum;
int start;
int end;
};

int main()
{
	   struct index sumidx[30],*tmp;

       int i,j,z =0,len;
       int a[30];

       printf("\n Enter the Number of elements to be entered
in the array  :\n");
       scanf("%d",&len);

       printf("\n Enter the array Values : \n");
       for(i=0;i<len;i++)
       scanf("%d",&a[i]);

       for(i = 0;i <len - 1;i++)
       {
		   sumidx[z].sum = a[i];
		   sumidx[z].start = i;
		   sumidx[z].end = i;

		   for(j=i+1;j<len;j++)
			   if(sumidx[z].sum < (sumidx[z].sum + a[j]))
			   {
				   sumidx[z].sum = sumidx[z].sum + a[j];
				   sumidx[z].start = i;
				   sumidx[z].end = j;
			   }
			   else
				   break;

			z++;
	   }

	  tmp = sumidx;

      for(i = 0;i<z;i++)
	  {
		  if(sumidx[i].start != sumidx[i].end)
			  if(tmp->sum < sumidx[i].sum)
				tmp = sumidx+i;
	  }
      printf("\n sum = %d start = %d end
%d\n",tmp->sum,tmp->start,tmp->end);

	  return 0;

}
 
Is This Answer Correct ?    2 Yes 1 No
Manoj
 
  Re: If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
Answer
# 4
Here's another way... 
Btw this way gets the correct answer of {9,4,3,-6,8,7,6,5}
with a sum of 36.

#include <stdio.h>
#include <stdlib.h>

#ifndef NULL
#define NULL 0
#endif

struct index {
	int start;
	int finish;
	int sum;
};

int main()
{
	// Init and setup variables
	struct index max;		// Max values
	struct index cur;		// Current values
	int avg = 0;			// Average values
	int len = 0;			// Array length
	int* a = NULL;			// The array
	int i;

	//Get values
	printf("Enter the number of variables in the array:");
	scanf("%d", &len);

	array = malloc(len*sizeof(int));

	if( array == NULL )
	{
		printf("Error: Out of memory");
		return 0;
	}

	for(i=0; i<len; ++i)
	{
		scanf("%d",&a[i]);
		avg += a[i];
	}
	avg /= len;			// Compute average

	max.start = max.finish = 0;
	max.sum = a[0];

	for(i=0; i<len && max.finish != len; ++i)
	{
		cur.start = cur.finish = i;
		cur.sum = a[i];
		while( cur.sum > avg && cur.finish < len )
		{
			if( cur.sum > max.sum )
			{
				max.start = cur.start;
				max.finish = cur.finish;
				max.sum = cur.sum;
			}
			++cur.finish;
			if( cur.finish < len )
				cur.sum += a[cur.finish];
		}
	}

	printf("\n Max sum = %d, start = %d, finish = %d\n",
max.sum, max.start, max.end);
	return 0;
}
 
Is This Answer Correct ?    1 Yes 0 No
Kyle
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
what will the following program do? void main() { int i; char a[]="String"; char *p="New Sring"; char *Temp; Temp=a; a=malloc(strlen(p) + 1); strcpy(a,p); //Line no:9// p = malloc(strlen(Temp) + 1); strcpy(p,Temp); printf("(%s, %s)",a,p); free(p); free(a); } //Line no 15// a) Swap contents of p & a and print:(New string, string) b) Generate compilation error in line number 8 c) Generate compilation error in line number 5 d) Generate compilation error in line number 7 e) Generate compilation error in line number 1 IBM1
Which of the following sorts is quickest when sorting the following set: 1 2 3 5 4 1) Quick Sort 2) Bubble Sort 3) Merge Sort  5
When is an interface "good"?  1
what is the output of the following program and explain the answer #include<stdio.h> exp() { main(5) } main(int a) { printf("%d",a); return; } Satyam3
wap to print "hello world" without using the main function. TCS16
what is the differance between pass by reference and pass by value. Infosys4
What is the difference between big endian form and little endian form? write a code to convert big endian form to little endian and vice versa.. Aricent4
write a Program to dispaly upto 100 prime numbers(without using Arrays,Pointer) Wipro7
What is the meaning When we write "#include" what is # and what does include does there??? HCL11
can anyone please tell me wat is backlogs... i was looking for the job openings where i read this.. eligibility criteria minimum 70% in degree without backlogs. is that arrear.. if so is it standing arrear or history of arrears... please help me...  4
how to exchnage bits in a byte b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3 please mail me the code if any one know to rajeshmb4u@gmail.com Honeywell3
Write a program to compute the following 1!+2!+...n!  3
write the function int countchtr(char string[],int ch);which returns the number of timesthe character ch appears in the string. for example the call countchtr("she lives in Newyork",'e') would return 3.  4
What's the difference between calloc() and malloc()?  3
How to write a code for random pick from 1-1000 numbers? The output should contain the 10 numbers from the range 1-1000 which should pick randomly, ie ,for each time we run the code we should get different outputs. NetApp12
enum day = { jan = 1 ,feb=4, april, may} what is the value of may? a)4 b)5 c)6 d)11 e)none of the above HCL2
What is a class?  2
How to implement variable argument functions ? HP1
9.how do you write a function that takes a variable number of arguments? What is the prototype of printf () function? 10.How do you access command-line arguments? 11.what does ‘#include<stdio.h>’ mean? 12.what is the difference between #include<> and #include”…”? 13.what are # pragma staments? 14.what is the most appropriate way to write a multi-statement macro? L&T4
main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } ME3
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com