Wednesday 27 January 2016

Largest sum of all contiguous subarrays

Write an efficient C program to find the largest sum of contiguous subarray within an one-dimensional array of integers. A contiguous subarray of an array is defined as the sequence of elements that are in any continuous set of indices that are valid within an array.

Lets take an example of an array
{5,-3, 4}. Possible contiguous subarray combinations are {5}, {-3}, {4}, {5,-3}, {-3,4} and {5,-3,4}. Note that {5,4} is not a valid subarray as the indices of 5 and 4 are not continuous.
The contiguous subarray  {5,-3,4} has the largest sum 6.

Input Constraints:
First line : array size (N), where 1<= N<=100
Second line : N integers separated by spaces
where each number Ni satisfies
-10000 <= Ni <=10000

Print Elements of a Matrix in Spiral Order

Write a program that reads an MxN matrix A and prints its elements in spiral order.
You should start from the element in the 0th row and 0th column in the matrix and proceed in a spiral order as shown below.

1→ 2 → 3 → 4                       
                      ↓
5 → 6 → 7 8
↑             ↓
9    10←11 12
↑             
13←14←15←16

Output for the above matrix: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

INPUT:
First line contains two integers M and N separated by whitespace. The next M lines contain elements of matrix A, starting with the topmost row. Within each row, the elements are given from left to right.
OUTPUT:
Elements of the matrix printed in a spiral order. All the elements should be separated by whitespace.

Find whether two given strings are permutations of each other

Write a program to find whether two given strings are permutations of each other.  A string str1 is a permutation of str2 if all the characters in str1 appear the same number of times in str2 and str2 is of the same length as str1.


Input: Two strings S1 and S2
Output:
yes - if they satisfy given criteria
no - otherwise

Constraints:
1 <= len(S1), len(S2) <= 100.
Characters from ASCII range 0 to 127.

White space will not be given in the string.

Solution-:

Find the depth of letters in a string

A string is given which has letters from English alphabet and parentheses. The depth of a letter is defined as the number of balanced parentheses it is surrounded by. Write a  C program to find the depth of each letter in the input string.

Explanation:

(a(b)((cd)e)f)g

g is at depth 0
a and f are at depth 1
b and e are at depth 2
c and d are at depth 3

Input Constraints:

1) Input string can be of length 1 to 100.
2) The input will have only ‘(‘ , ‘)’ and letters from English alphabet.
3) There will be no repetition of letters.
4) Only lowercase letters are used.
5) The letters can be in any sequence.   
6) The parentheses are always well balanced. Every '(' has a matching ')' that follows it later in the string. Every ')' has a matching '(' before it in the input string.
7) Notice that there are no space in the string.

Wednesday 20 January 2016

Points in a line (Solution to week 1 Assignment)

Given three points (x1, y1), (x2, y2) and (x3, y3) , write a program to check if all the three points fall on one straight line.


INPUT: Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.


OUTPUT: Print “Yes” if all the points fall on straight line, “No” otherwise.


CONSTRAINTS:

-1000 <= x1, y1, x2, y2, x3, y3 <= 1000

Solution -:

#include<stdio.h>

int main()
{int x1,x2,x3,y1,y2,y3;

Product of Digits (Solution to week 1 Assignment)

Write a C program that takes a positive number N and produces an output that is the product of its digits.

Explanation:

Take number 657.
Answer expected : 6 * 5 * 7 = 210

Constraint:
1<=N<=999999
Input: A single number
Output:  The value
Example 1:
Input: 657
Output: 210
Example 2:
Input: 7

Output: 7

Solution -:

#include<stdio.h> int main() {int n,d=1,i;

Perfect Number (Solution to week 1 Assignment)

Write a program to find whether a given number (say x) is a “perfect number” or not.

Definition of Perfect number:

A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself. By this definition, 1 is not a perfect number.


Explanation:
Take number 6.
Proper positive divisors of 6 is 1,2,3 and their sum is 1+2+3=6.
So, 6 is a perfect number.
Constraint:
1<=x<=1,000,000


Input: A single positive number  x
Output:
yes if given number x is a perfect number
no if given number x is not a perfect number


Example 1:
Input: 6
Output: yes


Example 2:
Input: 7
Output: no


Solution -:

#include<stdio.h>
int fact();int s=0,i;
int main()
{int n,d;
  scanf("%d",&n);i=n;
 if(n==1)
   printf("no");
 else
 {d=fact(n-1);
  if(d==i-1)
    printf("yes");
  else
    printf("no");}
return 0;
  }
  
int fact (int n)
  {if (n==1)
      return 1;
    
   else if(i%n==0)
    { s=s+n;
     fact(n-1);
      return (s);
      }
      
      else 
      fact(n-1);
   

  }

Perfect Squares (Solution to week 1 Assignment)

Write a program to find the number of perfect squares between given two numbers A and B (both inclusive). A number is called a perfect square if it can be written as x*x for some integer x.
Constraints:
Both A and B are positive. They both are less than 100,000.
Input: Two numbers A and B separated by a space
Output: Count of the number of perfect squares
Example 1:
Input: 3 10
Output: 2
Example 2:
Input: 16 70
Output: 5

Solution-:

  #include<stdio.h>
  int main()
{

Quiz 1 (Solution to week 1 Assignment)

Quiz 1

Due on 2016-02-01, 23:55 IST
Answer the following questions. All questions carry equal marks.
Find the output of the following program:
#include<stdio.h>
int main()
{
    int x=10; int y;
    {
        y=x++;
    }
    printf("%d",x);
}
 
 
 
 
Ans. 11
1 point
Find the output of the following program:
#include<stdio.h>
int main()
{
    char c='a';
    switch(c){
        case 97:
            printf("97");
            break;
        case 98:
            printf("98");
            break;
        case 99:
            printf("99");
            break;
        default:
           printf("default");
           
    }
}