Posts

Amazon Question - 3 Rotate Array

Given an array of size  N , rotate it by  D  elements.  Input:  The first line of the input contains  T  denoting the number of testcases. First line of test case is the number of elements  N , next line contains  D . Subsequent line will be the array elements. Output:  For each testcase, in a new line, output the rotated array. Constraints: 1 <= T <= 200 1 <= N <= 10 7 1 <= D <= N 1 <= arr[i] <= 10 3 Example: Input: 2 5 2 1 2 3 4 5  10 3 2 4 6 8 10 12 14 16 18 20 Output: 3 4 5 1 2 8 10 12 14 16 18 20 2 4 6 Code C - Language  :  #include<stdio.h> int main() { //code int tc,n,i,j,d; scanf("%d",&tc); while(tc--){     scanf("%d",&n);     scanf("%d",&d);     int a[n];          for(i=0;i<n;i++)     scanf("%d",&a[i]);          for...

Amazon Question - 2 Greater on right side

You are given an array  A  of size  N . Replace every element with the  next greatest element (greatest element on its right side) in the array. Also, since there is no element next to the last element, replace it with  -1 . Input: The first line of input contains an integer  T  denoting the number of test cases.  T  testcases follow. Each testcase contains two lines of input. The first line is  N , the size of tha array. The second line contains  N  space separated integers. Output: For each testcase, print the modified array. Constraints: 1 <= T <= 50 1 <= N <= 100 1 <= A i  <= 1000 Example: Input: 2 6 16 17 4 3 5 2 4 2 3 1 9 Output: 17 5 5 5 2 -1 9 9 9 -1 Code in C-Language : #include<stdio.h> int main() { int t; scanf("%d\n",&t); while(t>0) {     int n;     scanf("%d",&n);     int arr[n];     for(...

Amazon Question - 1 Maximum money

Given street of houses (a row of houses), each house having some amount of money kept inside; now there is a thief who is going to steal this money but he has a constraint/rule that he cannot steal/rob two adjacent houses. Find the maximum money he can rob. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and money. Output: Print maximum money he can rob. Constraints: 1 ≤ T ≤ 100 1 ≤ money ≤ 100 1 ≤ N ≤ 1000 Example: Input: 2 5 10 2 12 Output: 30 12 Code in C-Language :  #include<stdio.h> int main() { int n; int house, money; scanf("%d", &n); while (n > 0){ scanf("%d %d", &house, &money); if (house % 2 == 1) house++; printf("%d\n", money*house/2);  n--; } return 0; }

C Program to reverse a string using Array's

                   C Program to reverse a string using Array's   Output: Enter string: abc cba C Program : #include <stdio.h> int main() {    char s[1000], r[1000];    int i, j, c = 0;    printf("Input a string\n");    gets(s);    while (s[c] != '\0')       c++;    j = c - 1;    for (i = 0; i < c; i++) {       r[i] = s[j];       j--;    }    r[i] = '\0';    printf("%s\n", r);    return 0; }

C Program for Alternative Sorting

                        C Program for Alternative Sorting  C Program : #include<stdio.h> #include<stdlib.h> int main() {  int i,n,t,j;  scanf("%d",&n);  int *a=(int *)malloc(n*sizeof(int)); // creating dynamic array    for(i=0;i<n;i++)   {    scanf("%d",&a[i]);   }   //sorting   for(i=0;i<n;i++)   {    for(j=0;j<n;j++)    {       if(a[i]<a[j])   {     t=a[i];    a[i]=a[j];    a[j]=t;   }    }   }   t=n-1;j=0;   //printing alternate min max   for(i=0;i<n;i++)   {      if(i%2!=0){      printf("%d ",a[j] ); ++j;}     else{     printf("%d ",a[t] ); --t;}   }    ...

C Program to find the Frequency of Elements in an Array

     C Program to find the Frequency of Elements in an Array C Program : #include <stdio.h> int main() {     int arr[100], freq[100];     int size, i, j, count;     /* Input size of array */     printf("Enter size of array: ");     scanf("%d", &size);     /* Input elements in array */     printf("Enter elements in array: ");     for(i=0; i<size; i++)     {         scanf("%d", &arr[i]);         /* Initially initialize frequencies to -1 */         freq[i] = -1;     }     for(i=0; i<size; i++)     {         count = 1;         for(j=i+1; j<size; j++)         {             /* If duplicate element is found */    ...

String Matched and returning only the count, C Program to count the substring from the main String

         C Program to count the substring from the main String           Input 1                  : "AGCVbjasudsfabcaugisabcaabcAA" Input 2                  : "abc" Output                   : 3 C Program : #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size /* Function declaration */ int countOccurrences(char * str, char * toSearch); int main() {     char str[MAX_SIZE];     char toSearch[MAX_SIZE];     int count;     /* Input string and word from user */     printf("Enter any string: ");     gets(str);     printf("Enter word to search occurrences: ");     gets(toSearch);     count = countOccurrences(str, toSearc...