Thursday, 26 March 2015

Hackerrank Bigger is Greater Solution

Problem Statement 

Given a word w, rearrange the letters of w to construct another word s in such a way that, s is lexicographically greater than w. In case of multiple possible answers, find the lexicographically smallest one.

Source Code:

This program uses STL, if you have solved it without using STL comment the source code below.

 #include <cmath>  
 #include <cstdio>  
 #include <vector>  
 #include <iostream>  
 #include <algorithm>  
 #include <string.h>  
 using namespace std;  
 int main() {  
   int t;  
   cin>>t;  
   while(t--){  
     char a[1000];  
     int aa[1000]={0};  
     cin>>a;  
     int n = strlen(a);  
     if(next_permutation(a,a+n)){  
       cout<<a<<endl;  
     }else{  
       cout<<"no answer"<<endl;  
     }  
   }  
   return 0;  
 }  

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

3 comments:

  1. next_permutation(a,a+n) what does it do? it got me curios

    ReplyDelete
  2. Hackerrank bigger is greater solution, Problem state here with a complete source code. With the help of this source code you can make your https://www.topdissertations.org/papersowl-review/ solution better and it is interested if you guys show me any issue in my source code.

    ReplyDelete