Geeks For Geeks problem || Finding the immediate small element


In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge  about Data structure and alogorithm in C,Java,Python and other programming languages that may help you land an  online job and make your programmers day successfull.We use best data structure notes and best programming resources. Here goes the article,dont forget to share with your friends ??

Geeks for Geeks is very popular and helpful website for Computer Science students,they have an excellent collection of questions related to coding,interview experiences and so on.
                                                                                                                                    In this article we will solve a array related problem in which we are given an an array and we have to print the next element of each element if the element is small than the previous element otherwise we have to print -1.
In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and algorithms in C,Java,Python and other programming languages that may help you land an online job and make your programmers day successful.We use best data structure notes and best programming resources.
Here goes the article,don't forget to share with your friends 😊😊
     Let me very clear before we going to solve the problem that I am not doing any promotion of Geeks For Geeks its just that I love problem solving from this kind of websites and I also recommend you to do so.Enough talking though lets jump on to the problem  -------
Problem Statement: Let we have T testcases. For each case we have an array of given size.For each                                    array we have to print the next element of each element if the next element is                                        smaller than the previous element otherwise we will print -1.
                                 The first line of input is T=number of testcase;each testcase have 2 lines of input,
                                 first line is the size of the array,and the second line is the elements separated by                                     space.
Example:
                 Input:
                 2
                 5
                 1 3 2 7 2
                  4
                  8 5 9 2
                 Output:
                  -1 2 -1 -1
                   5 -1 2 -1
Solution:
 #include <stdio.h>
 main() {
int T,N,a[100000],i;
scanf("%d",&T); // Taking input of number of test cases
while(T--)
{
    scanf("%d",&N); //Taking input of size of the array for each testcase
     //Taking input of the array
    for(i=0;i<N;i++)
        scanf("%d",&a[i]);
     
    for(i=0;i<N-1;i++)
   {
//Check wheather the next element is smaller than the previous element upto the 2nd last element
       if(a[i]>a[i+1])
            printf("%d ",a[i+1]);
       else
            printf("-1 ");
   }
//Print -1 for the last element as it has no element to compare with
   printf("-1");
   printf("\n");
}

return 0;
}
Programming website about computer coding for free online in data structures and alogorithms in c,java,python etc with best programming resources

                    keep follow our blog for that kind of posts and don't forget to follow our social handels.

Post a Comment

0 Comments