0% found this document useful (0 votes)
58 views30 pages

Accenture19-D1 Set A Coding Abcdpdf PPT To PDF

Uploaded by

1minutemail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views30 pages

Accenture19-D1 Set A Coding Abcdpdf PPT To PDF

Uploaded by

1minutemail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Accenture

Topic/Course
Sub-Topic (Example: name of college)

Programs
Question 1
Write a program to eliminate the common elements in the given 2
arrays and print only the non repeating elements and the total.

Sample Sample
Input: Output:
1 5 10
3
54
12865
2 6 8 10
1 #include<stdio.h>
2 int Not_common (int *arr1, int *arr2, int l1, int l2)
3 {
4 int count =0, flag, i, j;
5 for(i=0; i<l1; i++)
6 {
7 flag=0;
8 for(j=0; j<l2; j++)
9 {
10 if(arr1[i] == arr2[j])
11 {
12 flag=1;
13 break;
14 }
15 }
16 if(flag ==0)
17 {
18 count++;
19 printf("%d ", arr1[i]);
20 }
21 }
22
1 for(i=0; i<l2; i++)
2 {
3 flag=0;
4 for(j=0; j<l1; j++)
5 {
6 if(arr2[i] == arr1[j])
7 {
8 flag=1;
9 break;
10 }
11 }
12 if(flag ==0)
13 {
14 count++;
15 printf("%d ", arr2[i]);
16 }
17 }
18 return count;
19 }
20
21
22
1 int main()
2 {
3 int len1,len2, result, i, j, arr1[10],arr2[10];
4 scanf("%d %d", &len1, &len2);
5 for(i=0; i<len1; i++)
6 scanf("%d", &arr1[i]);
7 for(i=0; i<len2; i++)
8 scanf("%d", &arr2[i]);
9 result = Not_common (arr1,arr2,len1,len2);
10 printf("\n%d", result);
11 return 0;
12 }
13
14
15
16
17
18
19
20
21
22
Question 2
Write a program to search an element in an array and print its
index value. If the element if not present, then print -1.

Sample Sample
Input: Output:
3
5
46728
3
1 #include<stdio.h>
2 int search (int arr[], int ele, int n);
3
4 int main ()
5 {
6 int n, i, ele;
7 scanf ("%d", &n);
8 int arr[n];
9 for (i = 0; i < n; i++)
10 {
11 scanf ("%d", &arr[i]);
12 }
13 scanf ("%d", &ele);
14 search (arr, ele, n);
15 return 0;
16 }
17
18
19
20
21
22
23 int search (int arr[], int ele, int n)
24 {
25 int i, flag = 0;
26
27 for (i = 0; i < n; i++)
28 {
29 if (ele == arr[i])
30 {
31 printf ("index = %d", i);
32 flag = 1;
33 break;
34 }
35 }
36
37 if (flag == 0)
38 {
39 printf ("-1");
40 }
41 }
42
43
44
Question 3
Given an array arr and two integer value n and m as input, take the
first element and find the difference between the first element and
the integer value n. if the difference is less than m then increment
a value. Do this for all the element in an array and print the final
result as output.

Sample Sample
Input: Output:
3 2557
5
21457
32
1 #include<stdio.h>
2 int search (int arr[], int ele, int n);
3
4 int main ()
5 {
6 int x, i, n,m;
7 scanf ("%d", &x);
8 int arr[x];
9 for (i = 0; i < x; i++)
10 {
11 scanf ("%d", &arr[i]);
12 }
13 scanf ("%d%d", &n,&m);
14 result (arr, x, n, m);
15 for(i=0;i<x;i++)
16 {
17 printf("%d ",arr[i]);
18 }
19 return 0;
20 }
21
22
23 int result (int arr[], int x, int n,int m)
24 {
25 int i;
26 for (i = 0; i < x; i++)
27 {
28 if ( (arr[i]-n ) < m)
29 {
30 arr[i]=arr[i]+1;
31 }
32 }
33 }
34
35
36
37
38
39
40
41
42
43
44
Question 4
Write a program to convert decimal number to binary equivalent
number.

Sample Sample
Input: Output:
1010
10
1 #include<stdio.h>
2 int main()
3 {
4 int a[10],n,i=0;
5 scanf("%d",&n);
6 while(n>0)
7 {
8 a[i++]=n%2;
9 n=n/2;
10 }
11 for(i=i-1;i>=0;i--)
12 printf("%d",a[i]);
13 return 0;
14 }
15
16
17
18
19
20
21
22
Question 5
Write a program to convert a binary number to an equivalent
decimal value.

Sample Sample
Input: Output:
8
1000
1 #include <stdio.h>
2 void main()
3 {
4 int num, binary, decimal = 0, base = 1, rem;
5 scanf("%d", &num);
6 binary = num;
7 while (num > 0)
8 {
9 rem = num % 10;
10 decimal = decimal + rem * base;
11 num = num / 10 ;
12 base = base * 2;
13 }
14 printf("%d", decimal);
15 }
16
17
18
19
20
21
22
Question 6
Write a program to print the second greatest element in the given
array.

Sample Sample
Input: Output:
12
5
12 5 7 3 90
1 #include <stdio.h>
2 #include <limits.h>
3 void print2largest(int arr[], int arr_size)
4 {
5 int i, first, second;
6 if (arr_size < 2)
7 {
8 printf(" Invalid Input ");
9 return;
10 }
11 first = second = INT_MIN;
12 for (i = 0; i < arr_size ; i ++)
13 {
14 if (arr[i] > first)
15 {
16 second = first;
17 first = arr[i];
18 }
19 else if (arr[i] > second && arr[i] != first)
20 second = arr[i];
21 }
22
23 if (second == INT_MIN)
24 printf("There is no second largest element");
25 else
26 printf("%d", second);
27 }
28
29 int main()
30 {
31 int n;
32 scanf("%d",&n);
33 int arr[n];
34 for(int i=0;i<n;i++)
35 scanf("%d",&arr[i]);
36 print2largest(arr, n);
37 return 0;
38 }
39
40
41
42
43
44
Question 7
Write a program to segregate all the 0’s in left side and 1’s in right
side in the same array with O(n) complexity.

Sample Sample
Input: Output:
0 0011
5
01010
1 #include <stdio.h> 23 int main()
2 void segregate0and1(int arr[],int n) 24 {
3 { 25 int n;
4 int left = 0, right = n-1; 26 scanf("%d",&n);
5 while(1) 27 int arr[n];
6 { 28 for(int i=0;i<n;i++)
7 if(left >= right) 29 scanf("%d",&arr[i]);
8 break; 30 segregate0and1(arr, n);
9 if(arr[left] == 0) 31 for(int i=0;i<n;i++)
10 left++; 32 printf("%d ",arr[i]);
11 else if(arr[right] == 1) 33 return 0;
12 right--; 34 }
13 else 35
14 { 36
15 arr[left] = 0; 37
16 arr[right] = 1; 38
17 left++; 39
18 right--; 40
19 } 41
20 } 42
21 } 43
22 44
Question 8
Write a program to find the most occurring character in the string.

Sample Sample
Input: Output:
p

Happy coding
1 #include <stdio.h> 23 printf("%c", result);
2 #include <string.h> 24 return 0;
3 int main() 25 }
4 { 26
5 char str[100], result; 27
6 int i, len; 28
7 int max = -1; 29
8 int freq[256] = {0}; 30
9 scanf("%[^\n]s",str); 31
10 len = strlen(str); 32
11 for(i = 0; i < len; i++) 33
12 { 34
13 freq[str[i]]++; 35
14 } 36
15 for(i = 0; i < len; i++) 37
16 { 38
17 if(max < freq[str[i]]) 39
18 { 40
19 max = freq[str[i]]; 41
20 result = str[i]; 42
21 } 43
22 } 44
Question 9
Write a program to find the whether the given string is the
anagram of the first string.

Sample Sample
Input: Output:
Anagram
eat
ate
1 #include <stdio.h>
2 int find_anagram(char [], char []);
3 int main()
4 {
5 char array1[100], array2[100];
6 int flag;
7 scanf("%s %s",array1,array2);
8 flag = find_anagram(array1, array2);
9 if (flag == 1)
10 printf("Anagram", array1, array2);
11 else
12 printf("Not anagrams", array1, array2);
13 return 0;
14 }
15
16
17
18
19
20
21
22
23 int find_anagram(char array1[], char array2[])
24 {
25 int num1[26] = {0}, num2[26] = {0}, i = 0;
26 while (array1[i] != '\0')
27 {
28 num1[array1[i] - 'a']++;
29 i++;
30 }
31 i = 0;
32 while (array2[i] != '\0')
33 {
34 num2[array2[i] -'a']++;
35 i++;
36 }
37 for (i = 0; i < 26; i++)
38 {
39 if (num1[i] != num2[i])
40 return 0;
41 }
42 return 1;
43 }
44
Question 10
Write a program to find the equilibrium element in the given array.
Hint : A[i] is equilibrium element if A[0] + A[1] + … A[i-1] ==
A[i+1] + A[i+2] + … + A[n-1]

Sample Sample
Input: Output:
4
6
123433
1 #include <stdio.h>
2 int findElement(int arr[], int n)
3 {
4 int prefixSum[n];
5 prefixSum[0] = arr[0];
6 for (int i = 1; i < n; i++)
7 prefixSum[i] = prefixSum[i - 1] + arr[i];
8 int suffixSum[n];
9 suffixSum[n - 1] = arr[n - 1];
10 for (int i = n - 2; i >= 0; i--)
11 suffixSum[i] = suffixSum[i + 1] + arr[i];
12 for (int i = 1; i < n - 1; i++)
13 if (prefixSum[i] == suffixSum[i])
14 return arr[i];
15 return -1;
16 }
17
18
19
20
21
22
23 int main()
24 {
25 int n;
26 scanf("%d",&n);
27 int arr[n];
28 for(int i=0;i<n;i++)
29 scanf("%d",&arr[i]);
30 int result = findElement(arr, n);
31 if(result == -1)
32 printf("No Equilibrium element found");
33 else
34 printf("%d",result);
35 return 0;
36 }
37
38
39
40
41
42
43
44
THANK YOU

You might also like