Sunday 13 January 2013

ekSMS.com and YourStory.in Hiring Challenge


Although this is giving a wrong answer according to the compiler of hackers earth but according to me it is appropriate solution for the problem. so if you find any bug in this plz do comment on it.

PROBLEM:
There are many ways to order a list of integers from 1 to n. For example, if n = 3, the list could be : [3 1 2].
But there is a special way to create another list from the given list of integers. In this list, position of integer i is the i-th number in the given list. So, the above list will be written as: [2 3 1]. This list is called inverse list. Now there exists some list whose inverse list is identical. For example, inverse list of [1 2 3] is same as original list. Give a list of integers you have to determine whether the list is inverse or not.
The input contains several test cases. The first line is the number of test cases t (1 <= t <= 100) . The first line of each test case contains an integer n (1 <= n <= 100000). Then a list of the integers 1 to n follows in the next line.
SOLUTION:   I have write this simple code for this problem.

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
class saurabh
{
      public:
             void sau(int t)
             {
                  long n = 0;
                  long tmp = 0;
                 
                  for(int l = 0 ; l < t ; l ++)
                  {
                  scanf("%ld",&n);
                  vector <long> num(n);
                    vector<long>cm(n);
                   tmp = 0;
                    for(long i = 0 ; i < num.size() ; i++)
                    {
                             scanf("%ld",&tmp);
                             num[i] = tmp;
                             cm[tmp-1] = i+1;
                    }
                    if(cm == num)
                    {
                          printf("inverse");
                    }
                    else
                    {
                        printf("not inverse");
                    }
                    num.clear();
                    cm.clear();
                  }
             }
             };
             int main()
             {
                 saurabh obj;
                 int t = 0;
                 scanf("%d",&t);
                 obj.sau(t);
                 cin.get();
                 return 0;
             }
                                                                    BY: SAURABH BHATIA            


No comments:

Post a Comment