Problem Statement TCS
Problem Statement TCS
Problem Statement –
A chocolate factory is packing chocolates into the packets. The chocolate packets here represent an
array of N number of integer values. The task is to find the empty packets(0) of chocolate and push it to
the end of the conveyor belt(array).
Example 1 :
There are 3 empty packets in the given set. These 3 empty packets represented as O should be pushed
towards the end of the array
Input :
8 – Value of N
Output:
45195000
Example 2:
Input:
6 — Value of N.
[6,0,1,8,0,2] – Element of arr[0] to arr[N-1], While input each element is separated by newline
Output:
618200
Code-
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i< n;i++)
arr[i]=sc.nextInt();
int count=0;
for(int i=0;i< n;i++)
if(arr[i]!=0)
arr[count++]=arr[i];
for(int i=count;i < n;i++)
arr[i]=0;
for(int i=0;i< n;i++)
System.out.print(arr[i]+" ");
}
}
2. Problem Statement –
Joseph is learning digital logic subject which will be for his next semester. He usually tries to solve unit
assignment problems before the lecture. Today he got one tricky question. The problem statement is “A
positive integer has been given as an input. Convert decimal value to binary representation. Toggle all bits
of it after the most significant bit including the most significant bit. Print the positive integer value after
toggling all bits”.
Constrains-
1<=N<=100
Example 1:
Input :
10 -> Integer
Output :
Explanation:
Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”.
Hence output will print “5”.
Code –
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
String bin="";
while(no!=0)
{
bin=(no&1)+bin;
no=no>>1;
}
bin=bin.replaceAll("1","2");
bin=bin.replaceAll("0","1");
bin=bin.replaceAll("2","0");
int res=Integer.parseInt(bin,2);
System.out.println(res);
}
}
Jack is always excited about sunday. It is favourite day, when he gets to play all day. And goes to cycling
with his friends.
So every time when the months starts he counts the number of sundays he will get to enjoy. Considering
the month can start with any day, be it Sunday, Monday…. Or so on.
Count the number of Sunday jack will get within n number of days.
Example 1:
Input
13 -> input integer denoting the number of days from the start of the month.
Output :
Explanation:
The month start with mon(Monday). So the upcoming sunday will arrive in next 6 days. And then next
Sunday in next 7 days and so on.
Now total number of days are 13. It means 6 days to first sunday and then remaining 7 days will end up in
another sunday. Total 2 sundays may fall within 13 days.
Code –
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int n=sc.nextInt();
String arr[]={"mon","tue,","wed","thu","fri","sat","sun"};
int i=0;
for(i=0;i< arr.length;i++) if(arr[i].equals(str)) break; int
res=1; int rem=6-i; n=n-rem; if(n >0)
res+=n/7;
System.out.println(res);
}
}
Given an integer array Arr of size N the task is to find the count of elements whose value is greater than all
of its prior elements.
Note : 1st element of the array should be considered in the count of the result.
For example,
Arr[]={7,4,8,2,9}
8 and 9 are also the elements that are greater than all of its previous elements.
Since total of 3 elements is present in the array that meets the condition.
Example 1:
Input
Output :
Example 2:
Output :
Constraints
1<=N<=20
1<=Arr[i]<=10000
Code-
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i< n;i++)
arr[i]=sc.nextInt();
int max=Integer.MIN_VALUE;
int count=0;
for(int i=0;i< n;i++) { if(arr[i]>max)
{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
Problem Statement
An intelligence agency has received reports about some threats. The reports consist of numbers in a
mysterious method. There is a number “N” and another number “R”. Those numbers are studied
thoroughly and it is concluded that all digits of the number ‘N’ are summed up and this action is performed
‘R’ number of times. The resultant is also a single digit that is yet to be deciphered. The task here is to find
the single-digit sum of the given number ‘N’ by repeating the action ‘R’ number of times.
Example 1:
Input :
99 -> Value of N
3 -> Value of R
Output :
Explanation:
Example 2:
Input :
2 -> Value of R
Output :
Explanation:
Here, the number N=1234
Constraints:
0<N<=1000
0<=R<=50
The output should be a positive integer number or print the message (if any) given in the problem
statement. (Check the output in Example 1, Example 2).
Code –
import java.util.*;
class Main
{
public static int sumOfDigits(int n)
{
int sum=0;
while(n>0)
{
sum+=n%10;
n=n/10;
}
return sum;
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int r=sc.nextInt();
if(r==0)
System.out.println("0");
else
{
int res=sumOfDigits(n)*r;
int sum=0;
while(true)
{
while(res>0)
{
sum=sum+res%10;
res=res/10;
}
if((sum/10)==0)
break;
else
res=sum;
}
System.out.println(sum);
}
}
1. Reverse a string in Python.
def reverse_string(s):
return s[::-1]
# Example usage:
def is_palindrome(s):
return s == s[::-1]
# Example usage:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Example usage:
if arr[mid] == target:
return mid
low = mid + 1
else:
high = mid - 1
return -1
# Example usage:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
return arr
# Example usage:
def find_max(arr):
max_element = arr[0]
max_element = num
return max_element
# Example usage:
def fibonacci(n):
fib_sequence = [0, 1]
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:-1]
# Example usage:
def remove_duplicates(arr):
return list(set(arr))
# Example usage:
def is_prime(n):
if n <= 1:
return False
if n % i == 0:
return False
return True
# Example usage:
def sum_of_digits(n):
# Example usage:
print(sum_of_digits(12345)) # Output: 15
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[-1]
def size(self):
return len(self.items)
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
print(stack.peek()) # Output: 2
stack.pop()
print(stack.peek()) # Output: 1
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Example usage:
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
print(queue.dequeue()) # Output: 1
# Example usage:
def word_count(sentence):
words = sentence.split()
word_count_dict = {}
if word in word_count_dict:
word_count_dict[word] += 1
else:
word_count_dict[word] = 1
return word_count_dict
# Example usage:
class Node:
def
16. Find the missing number in an array containing 1 to n in Python.
def find_missing_number(nums):
n = len(nums) + 1
total_sum = n * (n + 1) // 2
actual_sum = sum(nums)
# Example usage:
# Example usage:
def first_non_repeating_char(s):
char_count = {}
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
for char in s:
if char_count[char] == 1:
return char
return None
# Example usage:
k %= len(nums)
# Example usage:
nums = [1, 2, 3, 4, 5]
rotate_array(nums, 2)
class ListNode:
self.val = val
self.next = next
return pointerA
# Example usage:
intersect_node = ListNode(3)
intersect_node.next = ListNode(4)
headA = ListNode(1)
headA.next = ListNode(2)
headA.next.next = intersect_node
headB = ListNode(5)
headB.next = ListNode(6)
headB.next.next = intersect_node
intersection = get_intersection_node(headA, headB)
print(intersection.val) # Output: 3