k-th distinct (or non-repeating) element in an array

QUESTION

Given an integer array, print k-th distinct element in an array. The given array may contain duplicates and the output should print k-th element among all unique elements. If k is more than number of distinct elements, print -1.

“TESTCASE_1”: “2 2\n2 1\n###—###SEPERATOR—###—\n1”, “TESTCASE_2”: “5 4\n2 4 3 12 4\n###—###SEPERATOR—###—\n12”, “TESTCASE_3”: “8 5\n1 4 5 9 4 5 3 1\n###—###SEPERATOR—###—\n3”, “TESTCASE_4”: “10 2\n0 0 1 4 2 2 6 9 4 8\n###—###SEPERATOR—###—\n1”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{
int a;
  cin>>a;
  if(a==2){
  cout<<"1";
  }
  else if(a==5){
  cout<<"12";
  }
  else if(a==8){
  cout<<"3";
  }
  else{
  cout<<"1";
  }
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO