Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Concat Sort CodeChef Solution

Problem -Concat Sort CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Concat Sort CodeChef Solution in C++17

// Problem Link - 
/* By Navneet Shukla*/
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll 				long long int
#define ld				long double
#define mod             1000000007
#define inf             1e18
#define endl			"\n"
#define pb 				emplace_back
#define vi              vector<ll>
#define vs				vector<string>
#define pii             pair<ll,ll>
#define ump				unordered_map
#define mpa 				make_pair
#define pq_max          priority_queue<ll>
#define pq_min          priority_queue<ll,vi,greater<ll> >
#define ff 				first
#define ss 				second
#define mid(l,r)        (l+(r-l)/2)
#define loop(i,a,b) 	for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) 	for(int i=(a);i>=(b);i--)
#define log(args...) 	{ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define logarr(arr,a,b)	for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;	
#define token(str,ch)	(std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;)
vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}


void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void file_i_o()
{
    ios_base::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);
	#ifndef ONLINE_JUDGE
	    freopen("input.txt", "r", stdin);
	    freopen("output.txt", "w", stdout);
	#endif
}

int modularExponentiation(int x,int n,int M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return modularExponentiation((x*x)%M,n/2,M);
    else                             //n is odd
        return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;

}

int main(int argc, char const *argv[]) {
	file_i_o();
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int m[n],m1[n];
		loop(i,0,n-1)
		{
			cin>>m[i];
			m1[i]=m[i];
		}
		sort(m1,m1+n);
		int c=0;
		int i=0,j=0;
		while(i<n and j<n)
		{
			if(m1[i]==m[j])
			{
				i++;
				j++;
				c++;
			}
			else j++;
		}
		 j=0;
		while(i<n and j<n)
		{
			if(m1[i]==m[j])
			{
				i++;
				j++;
				c++;
			}
			else j++;
		}
		if(c==n) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;


	}
	return 0;
}

Concat Sort CodeChef Solution in C++14


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define Time cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
#define pb push_back
#define mp make_pair
#define line cout << endl;
#define ff first
#define ss second
#define vi vector<int>
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define printv(v)                      \
  for (int i = 0; i < (v.size()); i++) \
  {                                    \
    cout << v[i] << " ";               \
  }                                    \
  line;
#define onesbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define sp(x, y) fixed << setprecision(y) << x
#define w(x) \
  int x;     \
  cin >> x;  \
  while (x--)
#define tk(x) \
  int x;      \
  cin >> x;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
#define debug(x)     \
  cerr << #x << " "; \
  _print(x);         \
  cerr << endl;
#else
#define debug(x)
#endif
template <class T>
void _print(T t)
{
  cerr << t;
}

template <class T, class V>
void _print(pair<T, V> p)
{
  cerr << "{";
  _print(p.ff);
  cerr << ",";
  _print(p.ss);
  cerr << "}";
}

template <class T>
void _print(vector<T> v)
{
  cerr << "[ ";
  for (T i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

template <class T>
void _print(vector<vector<T>> v)
{
  cerr << "[\n";
  for (int l = 0; l < v.size(); l++)
  {
    {
      for (int k = 0; k < v[l].size(); k++)
        cerr << v[l][k] << " ";
    }
    cerr << "\n";
  }
  cerr << "]";
}

template <class T, class V>
void _print(map<T, V> v)
{
  cerr << "[ ";
  for (auto i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

template <class T>
void _print(set<T> v)
{
  cerr << "[ ";
  for (T i : v)
  {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

const long long inf = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e6;

int numbit(int x)
{
    int ans = 0;
    while (x > 0)
    {
        x = x >> 1;
        ans++;
    }
    return ans;
}

int setbit(int x)
{
    int ans = 0;
    while (x > 0)
    {
        if (x & 1)
        {
            ans++;
        }
        x = x >> 1;
    }
    return ans;
}

bool isValid(string s)
{
  int len = s.size();
  for (int i = 0; i < len / 2; i++)
  {
    if (s[i] != s[len - 1 - i])
      return false;
  }
  return true;
}

void rotateMatrix(vector<vector<int>> &v, int n)
{
  for (int i = 0; i < n / 2; i++)
  {
    for (int j = i; j < n - i - 1; j++)
    {
      int ptr = v[i][j];
      v[i][j] = v[n - 1 - j][i];
      v[n - 1 - j][i] = v[n - 1 - i][n - 1 - j];
      v[n - 1 - i][n - 1 - j] = v[j][n - 1 - i];
      v[j][n - 1 - i] = ptr;
    }
  }
}

vector<bool> is_prime(10001, 1);
vector<int> primes;

void seive()
{
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 10001; i++)
  {
    if (is_prime[i])
    {
      primes.push_back(i);
      for (int j = i + i; j < 10001; j += i)
      {
        is_prime[j] = 0;
      }
    }
  }
}

ll expo (ll x,ll n){
    ll result=1;
    while (n) {
        if (n & 1)
            result = result * x % MOD;
        n = n / 2;
        x = x * x % MOD;
    }
    return result;
}



int32_t main() {
  ll t; cin >> t;
  // seive();
  while (t--) {
      ll n,m; cin>>n;
      ll a[n],p[n];
      for (ll i = 0; i <n; i++){
        cin>>a[i];
        p[i]=a[i];
      }

      sort(p,p+n);

      ll cnt=0,j=0;

      for (ll i = 0; i <n; i++)
      {
        /* code */
        if(p[j]==a[i]){
          j++;
          a[i]=-1;
        }
      }

      for (ll i = 0; i <n; i++)
      {
        /* code */
        if(j==n) break;
        if(p[j]==a[i]){
          j++;
          a[i]=-1;
        }
      }


      if(j==n){
        yes
      }else no
      


      // for (ll i = 0; i <n; i++)
      // {
      //   /* code */

      //   cout<<a[i]<< " ";
      // }


      // cout<<endl;
      
      



      
	    
        

          
  }
  return 0;
}

// 1b 4d

Concat Sort CodeChef Solution in PYTH 3

# cook your dish here
for _ in range(int(input())):
    n=int(input())
    nli=list(map(int,input().split()))
    srtli=list()
    srtli+=nli
    srtli.sort()
    pli=list()
    qli=list()
    ind_srt=0
    for a in nli:
        if srtli[ind_srt]==a:
            pli.append(a)
            ind_srt+=1
        else:
            qli.append(a)
    pq=pli+qli
    if srtli==pq:
        print('YES')
    else:
        print('NO')
   

Concat Sort CodeChef Solution in C

#include <stdio.h>
#include <stdlib.h>
long int cmpfn (const void * a, const void * b) {
   return ( *(long int*)a - *(long int*)b );
}

int main(void) {
	int t;
	scanf("%d", &t);
	long int arr1[100001];
	long int brr[100001];
	long int arr2[100001];
	long int arr3[100001];
	while(t--){
	    int n;
	    scanf("%d", &n);
	    for(int i = 0; i < n; i++){
	        scanf("%ld", &arr1[i]);
	        brr[i] = arr1[i];
	    }
	    qsort(brr, n, sizeof(long int), cmpfn);
	    int j = 0;
	    int k = 0;
	    int check = 0;
	    int l = 0;
	    for(int i = 0; i < n; i++){
	        if(arr1[i] == brr[l]){
	            arr2[j] = arr1[i];
	            l++;
	            j++;
	        }else{
	            if(k != 0 && arr3[k - 1] > arr1[i]){
	                printf("No\n");
	                check = 1;
	                break;
	            }
	            arr3[k] = arr1[i];
	            k++;
	        }
	    }
	    if(k == 0){
	        arr3[0] = 1000000001;
	    }
	    if(check == 1){
	        continue;
	    }
	    if(j + k == n){
	        if(arr3[0] >= arr2[j - 1]){
	            printf("Yes\n");
	        }else{
	            printf("No\n");
	        }
	    }else{
	        printf("No\n");
	    }
	}
	return 0;
}

Concat Sort CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;


class Codechef{
    
    static class FastScanner{ 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner(){
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next(){ 
            while (st == null || !st.hasMoreElements()){ 
                try{ 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e){ 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt(){ 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong(){ 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble(){ 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine(){ 
            String str = ""; 
            try{ 
                str = br.readLine(); 
            } 
            catch (IOException e){ 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }
    
	public static void main (String[] args) throws java.lang.Exception{
		FastScanner sc = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		int a = sc.nextInt();
		while(a-->0){
		    solve(sc);
		}
	}
	
	public static void solve(FastScanner sc){
	     int len = sc.nextInt();
	    int arr[] = new int[len];
	    int arr1[] = new int[len];
	    
	    for(int i=0;i<len;i++){
	        arr[i] = sc.nextInt();
	        arr1[i] = arr[i];
	    }
	    
	    Arrays.sort(arr1);
	    
	    List<Integer> list = new ArrayList<>();
	    List<Integer> list1 = new ArrayList<>();
	    int i=0;
	    int j=0;
	    
	    while(i != len && j != len){
	        if(arr[i] == arr1[j]){
	            list1.add(arr[i]);
	            i++;
	            j++;
	        }else{
	            while( i != len && arr[i] != arr1[j] ){
	                list.add(arr[i]);
	                i++;
	            }
	        }
	    }
	    
	    list1.addAll(list);

	    boolean isa = true;
	    
	    for(int k=0;k<len;k++){
	        if( arr1[k] != list1.get(k) ){
	            System.out.println("NO");
	            isa = false;
	            break;
	        }
	    }
	    
	    if(isa){
	        System.out.println("YES");
	    }
	}
}

Concat Sort CodeChef Solution in PYPY 3

from sys import stdin
input = stdin.readline

from math import ceil, floor, sqrt, log2
from heapq import heappush, heappop
from collections import deque
from functools import lru_cache
from bisect import bisect_left, bisect_right

def rl(t = int):
    return list(map(t, input().split()))

T = int(input())

for t in range(1, T + 1):
    n = int(input())
    a = rl()
    sa = sorted(a)
    used = set()
    ua = []
    si = 0
    
    for i,el in enumerate(a):
        if el == sa[si]:
            si += 1
            used.add(i)
            ua.append(el) 
            
    res = ua + [el for i,el in enumerate(a) if i not in used]
    print("YES" if res == sa else "NO")

Concat Sort CodeChef Solution in PYTH

class ConcatS:
	def __init__(self, n, v):
		self.n = n
		self.v = v

	def solve(self):
		if self.n == 2:
			return 'YES'
		idx = -1
		vc = -1
		for i in range(0, self.n - 1):
			if self.v[i] > self.v[i + 1]:
				idx = i + 1
				vc = self.v[idx]
				break

		if idx == -1 or idx == self.n - 1:
			return 'YES'
		for i in range(0, idx):
			if self.v[i] > vc:
				left_max = self.v[i]
				break
		right = self.v[idx - 1]
		i = idx + 1
		while i < self.n:
			if self.v[i] >= right:
				right = self.v[i]
			elif self.v[i] <= left_max and self.v[i] >= vc:
				vc = self.v[i]
			else:
				return 'NO'
			i += 1
		if i == self.n:
			return 'YES'
		else:
			return 'NO'

def main():
	t = int(raw_input())
	while t > 0:
		t -= 1
		n = int(raw_input())
		v = [int(a) for a in raw_input().split()]
		ob = ConcatS(n, v)
		print ob.solve()

if __name__ == '__main__':
	main()

Concat Sort CodeChef Solution in C#

using System;
using System.Collections.Generic;

public class Test
{
	public static void Main()
	{
		int t = int.Parse(Console.ReadLine());
		for (int tt=0; tt<t; tt++){
		    int n = int.Parse(Console.ReadLine());
		    string[] s = Console.ReadLine().Split(' ');
		    int[] a = Array.ConvertAll(s, int.Parse);
		    int[] b = Array.ConvertAll(s, int.Parse);
		    Array.Sort(b);
		    int bi=0;
		    
		    List<int> p = new List<int>();
		    List<int> q = new List<int>();
		    
		    for(int i=0; i<n; i++){
		        if(a[i]==b[bi]){
		           p.Add(a[i]);
		           bi++;
		        }
		        else
		        {
		            q.Add(a[i]);
		        }
		    }
		    p.AddRange(q);
		    
		    string rez="YES";
		    for(int i=0; i<n; i++){
		        if (b[i]!=p[i]){
		            rez="NO";
		            break;
		        }
		    }
		    
		    Console.WriteLine(rez);
		}
	}
}

Concat Sort CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(A)

		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func index(x, y int) int {
	if x > 0 && y > 0 {
		return 0
	}
	if x > 0 && y < 0 {
		return 1
	}
	if x < 0 && y < 0 {
		return 2
	}
	return 3
}

func solve(A []int) bool {
	n := len(A)

	for i := 0; i+1 < n; i++ {
		if A[i] <= A[i+1] {
			continue
		}

		// A[i] > A[i+1]
		// first one that > A[i+1]  in the left part
		var j int
		for j <= i && A[j] <= A[i+1] {
			j++
		}
		// A[j] is the first number greater than A[i+1] in left
		a := i
		b := i + 1
		for k := b + 1; k < n; k++ {
			if A[k] >= A[a] {
				a = k
				continue
			}
			if A[k] >= A[b] {
				b = k
				continue
			}
			return false
		}
		return A[b] <= A[j]
	}

	return true
}
Concat Sort CodeChef Solution Review:

In our experience, we suggest you solve this Concat Sort CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Concat Sort CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Concat Sort CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *