Accenture
Topic/Course
Sub-Topic (Example: name of college)
Programs
Question 1
You are given a function: def MinimumUnfairness(arr, k): The function
accepts an integer array 'arr' of length n' and an integer k. If (x1, x2, x3,...
xk) are k numbers randomly selected from the array 'arr', the Unfairness
is defined as max(x1, x2,..., xk) - min(xl, x2, ..., xk), where max denotes the
largest integer among the k elements, and min denotes the smallest
integer among the k elements. Select k integers from the array 'arr' such
that its unfairness is minimized and return minimized unfairness value.
Sample Sample Output:
Input:
7 20
10 100 300 200 1000 20 30
3
1 def minDiff(arr,n,k):
2 result = +2147483647
3 arr.sort()
4 for i in range(n-k+1):
5 result = int(min(result, arr[i+k-1] - arr[i]))
6
7 return result
8 n =int(input())
9 arr = list(map(int,input().split()))
10 k = int(input())
11
12 print(minDiff(arr, n, k))
13
14
15
16
17
18
19
20
21
22
Java Solution
1 import java.util.*;
2 class Main {
3 static int minDiff(int arr[], int n, int k) {
4 int result = Integer.MAX_VALUE;
5 Arrays.sort(arr);
6 for (int i = 0; i <= n - k; i++)
7 result = Math.min(result, arr[i + k - 1] - arr[i]);
8
9 return result;
10 }
11 public static void main(String[] args)
12 {
13 Scanner sc = new Scanner(System.in);
14 int n = sc.nextInt();
15 int[] arr = new int[n];
16 int i;
17 for(i = 0; i < n;i++)
18 {
19 arr[i] = sc.nextInt();
20 }
21 int k = sc.nextInt();
22 System.out.println(minDiff(arr, n, k)); } }
Question 2
Given two positive numbers N and M, the task is to count the number of digits that
are present in both N and M.
Sample Sample Output:
Input:
N = 748294 4
M = 34298156
Explanation:
The digits that are present in both the numbers are {4, 8, 2, 9}. Therefore, the required
count is 4.
1 def CommonDigits(N, M):
2 count = 0
3
freq1 = [0] * 10
4
5 freq2 = [0] * 10
6 while (N > 0):
7 freq1[N % 10] += 1
8 N = N // 10
9
10
11 while (M > 0):
12 freq2[M % 10] += 1
13 M = M // 10
14
15 for i in range(10):
16
17
if (freq1[i] > 0 and freq2[i] > 0):
18 count += 1
19
20 return count
21
22
23
24
N = int(input())
25 M = int(input())
26
27
28 print (CommonDigits(N, M))
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Java Solution
1 import java.util.*;
2
3
class Main{
4
5 static int CommonDigits(int N, int M)
6 {
7 int count = 0;
8 int freq1[] = new int[10];
9 int freq2[] = new int[10];
10
11 while (N > 0)
12 {
13 freq1[N % 10]++;
14 N = N / 10;
15 }
16
17
while (M > 0)
18 {
19 freq2[M % 10]++;
20 M = M / 10;
21 }
22
23 for(int i = 0; i < 10; i++)
24 {
25
if (freq1[i] > 0 & freq2[i] > 0)
26
27 {
28 count++;
29 }
30 }
31 return count;
32
33 }
34
35 public static void main(String[] args)
36 {
37 Scanner sc = new Scanner(System.in);
38
39
int N = sc.nextInt();
40 int M = sc.nextInt();
41
42 System.out.print(CommonDigits(N, M));
43 }
44
Question 3
Given a positive integer N, count all possible distinct binary strings of
length N such that there are no consecutive 1’s.
Sample Input: Sample Output:
N=2 3
Explanation:
The 3 strings are 00, 01, 10
1 def countStrings(n):
2
3
4 a=[0 for i in range(n)]
5
6
b=[0 for i in range(n)]
7 a[0] = b[0] = 1
8 for i in range(1,n):
9
10 a[i] = a[i-1] + b[i-1]
11 b[i] = a[i-1]
12
13
14 return a[n-1] + b[n-1]
15
16
17 n = int(input())
18 print(countStrings(n))
19
20
21
22
Java Solution
1 import java.util.*;
2 class Main
3 {
4 static int countStrings(int n)
5 {
6 int a[] = new int [n];
7 int b[] = new int [n];
8 a[0] = b[0] = 1;
9 for (int i = 1; i < n; i++)
10 {
11 a[i] = a[i-1] + b[i-1];
12 b[i] = a[i-1];
13 }
14 return (a[n-1] + b[n-1])%1000000007;
15 }
16 public static void main (String args[])
17 {
18 Scanner sc = new Scanner(System.in);
19 int n = sc.nextInt();
20 System.out.println(countStrings(n));
21 }
22 }
Question 4
The program is supposed to calculate the sum of distance between three
points from each other.
For
x1 = 1 y1 = 1
x2 = 2 y2 = 4
x3 = 3 y3 = 6
Distance is calculated as : sqrt(x2-x1)2 + (y2-y1)2
1 import math
2
3 x1, y1 = 1, 1
4 x2, y2 = 2, 4
5
6
x3, y3 = 3, 6
7 first_diff = math.sqrt(math.pow(x2-x1, 2) + math.
8 pow(y2-y1, 2))
9
10 second_diff = math.sqrt(math.pow(x3-x2, 2) + math.
11 pow(y3-y2, 2))
12
13
third_diff = math.sqrt(math.pow(x3-x1, 2) + math.
14 pow(y3-y1, 2))
15 print(round(first_diff,2), round(second_diff,2),
16
17 round(third_diff,2))
18
19
20
21
22
Question 5
An Autobiographical Number is a number N such that the first digit of N represents the count of how
many zeroes are there in N, the second digit represents the count of how many ones are there in N and
so on.
You are given a function, def FindAutoCount(n):
The function accepts string “n” which is a number and checks whether the number is an
autobiographical number or not. If it is, an integer is returned, i.e. the count of distinct numbers in ‘n’. If
not, it returns 0.
Assumption:
The input string will not be longer than 10 characters.
Input string will consist of numeric characters.
Note:
If string is None return 0.
Question 5
Sample Input: Sample Output:
N = 1210 3
Explanation:
0th position in the input contains the number of 0 present in input, i.e. 1, in 1st position the count of number of 1s
in input i.e. 2, in 2nd position the count of 2s in input i.e. 1, and in 3rd position the count of 3s i.e. 0, so the number
is an autobiographical number.
Now unique numbers in the input are 0, 1, 2, so the count of unique numbers is 3. So 3 is returned.
1 def find_auto_count(n: str) -> int:
2 digit_sum = sum(int(char) for char in n)
3 if digit_sum == len(n):
4 count = 0
5 digit_count = [0] * 10 # Array to count occurrences of each digit
6
7 # Count the occurrences of each digit
8 for char in n:
9 digit_count[int(char)] += 1
10
11 # Count how many different digits are present
12 for count_occurrences in digit_count:
13 if count_occurrences != 0:
14 count += 1
15
16 return count
17 else:
18 return 0
19
20 if __name__ == "__main__":
21 str_input = input("Enter a number: ")
22 print(find_auto_count(str_input))
THANK YOU