Notice Board

Notice Board

Friday, 14 November 2014

Nagarro Questions with Solutions


Q1. A mXn matrix was given and rows and column were sorted as shown below then we had to/write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()


{
int c,d,i,row, first, last, middle, n, search, array[10][10];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
for(d=0;d<n;d++)
scanf("%d",&array[c][d]);
printf("Enter value to find\n");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(array[i][0]<=search&&search<=array[i][n-1])
{
row=i;
break;
}
}
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[row][middle] < search )
first = middle + 1;
else if ( array[row][middle] == search )
{
printf("%d found \n", search);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}


Object 2: A string of charater is given.Find the highest occurance of a character and display that character. eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE OUTPUT: E or I,J(if equal occurance)
#include <stdio.h>
#include <string.h>
char string1[100], visited[100];
int count[100] = {0}, flag = 0;
void main()
{
int i, j = 0, k = 0, l, max, index;
printf("Enter a string : ");
scanf("%[^\n]s", string1);
l = strlen(string1);
for (i = 0; i < l; i++)
{
if (i == 0)
{
visited[j++] = string1[i];
count[j - 1]++;
}
else
{
for (k = 0; k < j; k++)
{
if (string1[i] == visited[k])
{
count[k]++;
flag = 1;
}
}
if (flag == 0)
{
visited[j++] = string1[i];
count[j - 1]++;
}
flag = 0;
}
}

for (i = 0; i < j; i++)
{
if ((i == 0) && (visited[i] != ' '))
{
max = count[i];
continue;
}
if ((max < count[i]) && (visited[i] != ' '))
{
max = count[i];
index = i;
}
}
printf("\nMax repeated character in the string = %c ", visited[index]);
printf("\nIt occurs %d times", count[index]);
}
Object 3: Write a program to get the first non repeating alphabet from the given string by the user For example: string = abcba Output : c string = abcdecbae
Output : d string =naveen Output : a
#include<stdlib.h>
#include<stdio.h>
#define NO_OF_CHARS 256
/* Returns an array of size 256 containg count
of characters in the passed char array */
int *getCharCountArray(char *str)
{
int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}
/* The function returns index of first non-repeating
character in a string. If all characters are repeating
then returns -1 */
int firstNonRepeating(char *str)
{
int *count = getCharCountArray(str);
int index = -1, i;
for (i = 0; *(str+i); i++)
{
if (count[*(str+i)] == 1)
{
index = i;
break;
}
}
free(count); // To avoid memory leak
return index;
}
/* Driver program to test above function */
int main()
{
char str[] = "ggeek";
int index = firstNonRepeating(str);
if (index == -1)
printf("Either all characters are repeating or string is empty");
else
printf("First non-repeating character is %c", str[index]);
getchar();
return 0;
}
Object4: Write a program to check whether 2 strings given by the user are anagram strings or not. For example: str1: Are you engineer. str2: You are engineer. Output: yes str1: Am i fine. str2: I'm fine. Output: No str1: Am i fine. str2: I am fire. Output: No
#include <stdio.h>
int check_anagram(char [], char []);
int main()
{
char a[100], b[100];
int flag;
printf("Enter first string\n");
gets(a);
printf("Enter second string\n");
gets(b);
flag = check_anagram(a, b);
if (flag == 1)
printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
return 0;
}
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{ printf("%d%d\t",first[c],second[c]);
if (first[c] != second[c])
return 0;
}
return 1;
}
#include <stdio.h>
int check_anagram(char [], char []);
int main()
{
char a[100], b[100];
int flag;
printf("Enter first string\n");
gets(a);
printf("Enter second string\n");
gets(b);
flag = check_anagram(a, b);
if (flag == 1)
printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
return 0;
}
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{ printf("%d%d\t",first[c],second[c]);
if (first[c] != second[c])
return 0;
}
return 1;
}
Object 5: find the all possible combination of digits ranging 1 to 9 whose sum is 10, no digit shud be repeated in any combination.
Example 1234 127 136 145 19 235 28

37 46 main()
{
int i,j,k,l,sum;
clrscr();
for(i=1;i<10;i++)
{
sum=0;
for(j=i+1;j<10;j++)
{
sum=i+j;
if(sum==10)
{
printf("%d%d\n",i,j);
break;
}
if(sum>10)
break;
for(k=j+1;k<10;k++)
{
sum=i+j+k;
if(sum==10)
{
printf("%d%d%d\n",i,j,k);
break;
}
if(sum>10)
break;
for(l=k+1;l<10;l++)
{
sum=i+j+k+l;
if(sum==10)
printf("%d%d%d%d",i,j,k,l);
break;
}
}
}
}
}
Object 6 :Write a program to find out the combination of an element of each array gives a result 0.
For example:
array 1: {2,1,4,7}
array 1: {3,-3,-8,0}
array 1: {-1,-4,-7,6}
Output:
[2,-8,6]
[1,3,-4]

[1,0,-1]
[4,3,-7]
[4,-3,-1]
[4,0,-4]
[7,-3,-4]
[7,0,-7]
package com.sdj;
public class ArraySumZero
{
public static void main(String[] args)
{
int arr1[]={2,1,4,7};
int arr2[]={3,-3,-8,0};
int arr3[]={-1,-4,-7,6};
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
if(arr1[i]+arr2[j]+arr3[k]==0)
System.out.println("["+arr1[i]+","+arr2[j]+","+arr3[k]+"]");
}
}
}
}
}
Object 7:There is a array of 99 cells and we have to enter 1-100 elements in it , no two elements would repeat , so the is one no. missing because 99 cells and 1-100 nos. so we had to
Implement a function to find that missing no.
package com.sdj;
import java.util.Scanner;
public class RandomSum
{
public static void main(String[] args)
{
int all[] = new int[99]; int sum=0;
Scanner sc = new Scanner(System.in);
for(int i=0;i<all.length;i++)
{
all[i] =sc.nextInt();
}
for(int i=0;i<all.length;i++)
{
Printf(“%d”,all[i]\t);
} sum = (all.length*(all.length+1))/2; printf(“%d”,sum);
for(int i=0;i<all.length;i++)
{ sum = sum-all[i];
} printf("Number Not Present is Sum=%d",sum);
}
}
Q8. Add integers in a string.
#include<stdio.h>
main()
{
int sum=0,i=0,j=0,k,n;
char s[100],b[10];
printf("enter string");
gets(s);
for(;s[i]!='\0';)
{
for(k=0;s[j]>='0'&&s[j]<='9';j++,k++)
{
b[k]=s[j];
//printf("no=%c",b[k]);
}
b[k]='\0';
n=atoi(b);
sum=sum+n;
strcpy(b,0);
j++;
i++;
}
printf("sum=%d",sum);
}


By Shivam Kotwalia

1 comment: