Posts

Showing posts from July, 2022

TCS digital coding problems and solutions 2022-2023

TCS Digital Coding Problem -2   Problem Description -:   Given two non-negative integers n1 and n2 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. Example1: Input: 11 — Vlaue of n1 15 — value of n2 Output: 4 Example 2: Input: 101 — value of n1 200 — value of n2 Output: 72 Python - n1=int(input()) n2=int(input()) l=[] c=0 for i in range(n1,n2+1):     while i!=0:         r=i%10         l.append(r)         i=i//10     if not(len(set(l))<len(l)):         c+=1     l.clear()      print(c)     

TCS digital coding question with solution 2022-2023

TCS Digital Coding problem -1   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  Python - n=int(input()) arr=[int(i) for i in input().split()] k=int(input()) rfirst=arr[0:n-k] rsecond=arr[n-k:] print(rsecond+rfirst)     

Remove consecutive characters from string

 def string(s):     n=len(s)     if n<2:         return     j=0     for i in range(n):         if s[j]!=s[i]:             j+=1              s[j]=s[i]     j+=1      s=s[:j]     return s       s="aabaa" r=string(list(s)) print(*r,sep='') output=aba

find string is Anagram or not solution

 def palindrome(string,s):          if sorted(string)==sorted(s):         return True     else:         return False          string="nagaram" s="anagram" print(palindrome(string,s)) output = True

Next permutation solution

 def next_per(nums):     t=0     if max(nums)==nums[0]:        t=max(nums)        nums[0]=nums[len(nums)-1]        nums[len(nums)-1]=t          return nums      else:         m=max(nums)         t=nums.index(max(nums))         nums[t] = nums[t-1]         nums[t-1]=m          return nums          nums=[1,2,3] print(next_per(nums)) output = [1,3,2]

Search in Rotated Sorted Array easy solution

 def serach(nums,l,u,target):     if target in nums[l:u+1]:         for i in range(l,u):             if target==nums[i]:                 return nums.index(nums[i])     else:         for i in range(u,len(nums)):             if target==nums[i]:                 return nums.index(nums[i])         else:             return -1              nums=[4,0,6,7,8,1,2] l=0 u=(l+len(nums))//2 target=1 print(serach(nums,l,u,target)) output=5

Maximum sum of subarray solution

 def subarray(nums):     temp=nums[0]     result=nums[0]          for i in range(1,len(nums)):         temp=max(nums[i],temp+nums[i])         result=max(result,temp)              return result      nums=[-2,-3,4,-1,-2,1,5,-3] print(subarray(nums)) output=7