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                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
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 0 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 ?    1 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 0 No
Manoj
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
WAP to accept rollno,course name & marks of a student & display grade if total marks is above 200?  2
Study the Following Points: a.One Cannot Take the address of a Bit Field b.bit fields cannot be arrayed c.Bit-Fields are machine Dependant d.Bit-fields cannot be declared as static 1. Which of the Following Statements are true w.r.t Bit- Fields A)a,b&c B)Only a & b C)Only c D)All Accenture1
f=(x>y)?x:y a) f points to max of x and y b) f points to min of x and y c)error HCL3
When you call malloc() to allocate memory for a local pointer, do you have to explicitly free() it?  2
Write a program in c to input a 5 digit number and print it in words.  1
What is an volatile variable? HP6
what is the difference between const volatile int i & volatile const int j; HCL1
program to find a smallest number in an array Microsoft2
#define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } what are the outputs? Ramco4
#define MAX(x,y) (x) > (y) ? (x) : (y) main() { int i = 10, j = 5, k = 0; k = MAX(i++, ++j); printf("%d %d %d", i,j,k); } what will the values of i , j and k? } NDS9
44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?  3
52.write a “Hello World” program in “c” without using a semicolon? 53.Give a method to count the number of ones in a 32 bit number? 54.write a program that print itself even if the source file is deleted? 55.Given an unsigned integer, find if the number is power of 2?  3
a=0; b=(a=0)?2:3; a) What will be the value of b? why b) If in 1st stmt a=0 is replaced by -1, b=? c) If in second stmt a=0 is replaced by -1, b=? Geometric-Software5
What is a class?  1
What is the value of y in the following code? x=7;y=0; if(x=6) y=7; else y=1; TCS10
how the size of an integer is decided? - is it based on processor or compiler or OS? nvidia11
write a program to display the array elements in reverse order in c language  2
Read N characters in to an array . Use functions to do all problems and pass the address of array to function. 1. Print only the alphabets . If in upper case print in lower case vice versa.  1
what is compiler  5
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell6
 
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