Join Regular Classroom : Visit ClassroomTech

TCS Digital Coding Questions – 3 Problems with Solution – codewindow.in

tcs digital problems

Problem Description:
Given an array Arr[] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K.
Note: Keep the first of the array unaltered.

Example 1:
5 —Value of N
10 20 30 40 50 — Element of Arr[]
2 —–Value of K

Output :
40 50 10 20 30

Example 2:
4 —Value of N
10 20 30 40 — Element of Arr[]
1 —–Value of K

Output :
40 10 20 30

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow {
    static int[] rotate(int nums[], int n, int k) {
        if (k > n)
        k = k % n;
        int[] ans = new int[n];
        
        for (int i = 0; i < k; i++)
            ans[i] = nums[n - k + i];
        
        int index = 0;
        for (int i = k; i < n; i++)
            ans[i] = nums[index++];
        return ans;
    }

    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] Array = new int[N];
        String str[] = br.readLine().split(" ");
        for(int i=0; i<N; i++)
            Array[i] = Integer.parseInt(str[i]);
        int K = Integer.parseInt(br.readLine());
        
        int[] ans = rotate(Array, N, K);
        for (int i = 0; i < N; ++i)
            System.out.print(ans[i] + " ");
    }
}

Problem Description:

Given two non-negative integers n1 and n2, where n1<n2. The task is to find the total number of integers in the range [n1, n2](both inclusive) which have no repeated digits.
For example:
Suppose n1=11 and n2=15.
There is the number 11, which has repeated digits, but 12, 13, 14, and 15 have no repeated digits. So, the output is 4.

Example 1:

Input:
• 11 — value of n1
• 15 — value of n2

Output:
• 4


Example 2:

Input:
• 101 — value of n1
• 200 — value of n2

Output:
• 72

#include<bits/stdc++.h>
using namespace std;

int find(int n1, int n2) {
    int count = 0;
    for (int i = n1 ; i <= n2 ; i++) {
        int num = i;
        vector<bool> visited;
        visited.assign(10, false);
        while (num > 0) {
            if (visited[num % 10] == true)
                break;
            visited[num % 10] = true;
            num /= 10;
        }
        if (num == 0)
            count++;
    }
    return count;
}

int main()
{
    int n1 , n2;
    cin >> n1;
    cin >> n2;
    cout << find(n1, n2);
}

Problem Definition:

Let us find out whether the sum of the digits of the given positive integer number N is UNO or not. Given a positive integer number N, reduce the number of digits of N by computing the sum of all the digits to get a new number. If this new number exceeds 9, then sum the digits of this new number to get another number and continue this way until a single digit value is obtained as the ‘digit sum’. The task here is to find out whether the result of the digit sum done this way is ‘1’ or not.

If the digit sum result is 1, display a message UNO If the digit sum is not 1, display a message NOT UNO.

Example:

Input:
51112 –Value of N
5+1+1+1+2 we get 10. Adding the digits again 1+0 we get the digit sum = 1 , so therefore output will be “UNO”

Input format for testing
The candidate must write the code to accept one input which Accept value for N (positive integer number)

Output format for testing
The output should print the message given in the problem statement (check the output in above example)

#include <stdio.h>

int main()
    {
    int n,r,sum=0;
    scanf("%d",&n);
    while(n>0) {
        r=n%10;
        sum=sum+r;
        n=n/10;
    }
    if(sum%9==1)
        printf("UNO");
    else
        printf("NOT UNO");
}

Cognizant Solutions | CodeGladiators Solutions​