TCS Coding Problem 16

14 comments on “TCS Coding Problem 16”


  • Gourav

    arr= [12, 35, 1, 10, 34, 1]
    for i in range (2):
    for j in range (len(arr)-1):
    if(arr[j]>arr[j+1]):
    arr[j],arr[j+1]=arr[j+1],arr[j]
    print(arr[len(arr)-2])


  • sunny

    # python
    n=int(input())
    l=list(set(map(int,input().split())))
    l.sort()
    if (len(l)<2):
    print('2nd largest does not exists')
    else:
    print(l[-2])


  • Kritika

    num=[4,6,2,3,1,9]
    k=2

    def quickhoarse(num,left,right,indextofind):

    partindx=partition(num,left,right)
    if partindx==indextofind:
    return num[indextofind]
    elif partindx<indextofind :
    return quickhoarse(num,partindx+1,right,indextofind)
    else:
    return quickhoarse(num,left,partindx-1,indextofind)

    def partition(num,left,right):
    i=left
    for j in range(left,right+1):
    if num[j]<=num[right]:
    num[i],num[j]=num[j],num[i]
    i=i+1
    #num[i],num[right]=num[right],num[i]
    return i-1

    def index(num,k):
    indextofind=len(num)-k
    return quickhoarse(num,0,len(num)-1,indextofind)

    if __name__=='__main__':
    x=index(num,k)
    print(x)


  • Ankita

    public class Second_largest {

    public static void main(String[] args) {
    int[] arr= {20,5,85,4,66,12,101,95};
    int max=arr[0];
    int secmax=arr[0];

    for(int i=0;imax)
    {
    secmax=max;
    max=arr[i];

    }
    if(arr[i]secmax)
    {
    secmax=arr[i];
    }
    }
    System.out.println(“Second maximum is “+secmax);
    }

    }


  • Ankita

    package PrepInsta;

    public class Second_largest {

    public static void main(String[] args) {
    int[] arr= {20,5,85,4,66,12,101,95};
    int max=arr[0];
    int secmax=arr[0];

    for(int i=0;imax)
    {
    secmax=max;
    max=arr[i];

    }
    if(arr[i]secmax)
    {
    secmax=arr[i];
    }
    }
    System.out.println(“Second maximum is “+secmax);
    }

    }


  • Debanjan

    # a=[10,5,10]
    a=[10, 10, 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,6,]
    a.sort(reverse=True)
    print(a)
    for i in range(1,len(a)):
    if a[i]!=a[i-1]:
    print(a[i])
    break
    if i==len(a)-1:
    if a[len(a)-1]==a[len(a)-2]:
    print(“All Values are same”)


  • amit

    #include
    #include
    int main()
    {
    int i, n,large;
    int arr[100];
    printf(“Enter the number of elements (1 to 100): “);
    scanf(“%d”, &n);

    for (i = 0; i < n; ++i) {
    printf("Enter number%d: ", i + 1);
    scanf("%d", &arr[i]);
    }
    large=arr[0];
    // storing the largest number to arr[0]
    for (i = 1; i < n; ++i) {
    if (large< arr[i]){
    large = arr[i];
    }
    }

    printf("Largest element = %d", large);

    for (i = 1; i < n; ++i) {
    if(arr[i]!=large){
    if (arr[0] < arr[i]){
    arr[0] = arr[i];
    }
    }

    }
    printf("2ndLargest element = %d", arr[0]);

    return 0;
    }


  • Raman

    // Online C compiler to run C program online
    #include

    int main() {
    int a[20],n,i;
    int max1=0,max2=0;

    printf(“enter the size of the array :”);
    scanf(“%d”,&n);

    printf(“enter the element of the array :”);
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }

    for(i=0;imax1){
    max1=a[i];
    }
    }

    for(i=0;imax2 && a[i]<max1){
    max2=a[i];
    }
    }
    printf("2 nd largest %d",max2);

    return 0;
    }


  • Pogakula

    #python3
    def seclar(ar):
    if len(set(ar))!=1:
    maxi = max(ar)
    for i in sorted(ar)[::-1]:
    if i == maxi:
    continue
    else:
    return ‘the second largest element is {}’.format(i)
    else:
    return ‘no second max item..’
    arr=[]
    n=int(input(‘enter the array length:\t’))
    for i in range(n):
    inp=int(input(‘enter the number:\t’))
    arr.append(inp)
    print(seclar(arr))


  • Sadique Gametor

    #include
    int main()
    {
    int n;
    scanf(“%d”,&n);
    int b[n],i,j,temp;
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
    {
    if(b[i]<b[j])
    {

    temp=b[i];
    b[i]=b[j];
    b[j]=temp;
    }
    }
    }
    printf("%d",b[1]);
    return 0;
    }