Advertisement
Niloy007

Sorting Array value's index

Oct 27th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int T, N, K, index = 0;
  7.     cin >> T;
  8.  
  9.     for(int k=1; k<=T; k++) {
  10.         cin >> N >> K;
  11.         int arr[N];
  12.         if (T > 50) {
  13.             return false;
  14.         } else if (K > N) {
  15.             return false;
  16.         } else {
  17.             for (int i = 0; i < N; i++) {
  18.                 cin >> arr[i];
  19.             }
  20.         }
  21.  
  22.         for (int i = 0; i < N; i++) {
  23.             for (int j = i; j < N; j++) {
  24.                 if (arr[i] > arr[j]) {
  25.                     int temp = arr[i];
  26.                     arr[i] = arr[j];
  27.                     arr[j] = temp;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         for (int i = 0; i < N; i++) {
  33.             if (arr[i] == K) {
  34.                 index = i;
  35.             }
  36.         }
  37.  
  38.         cout << index << endl;
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement