Given an integer array and an element x, find if element is present in array or not. If element is present, then print index of its first occurrence. Else print -1.

Input:

First line contains an integer, the number of test cases 'T' Each test case should contain an integer, size of array 'N' in the first line. In the second line Input the integer elements of the array in a single line separated by space. Element X should be inputted in the third line after entering the elements of array.

Output:

print the output in a separate line returning the index of the element X.If element not present then print -1.

Constraints:

1 <= T <= 100

1 <= N <= 100

1 <= Arr[i] <= 100

Example:

Input:
1
4
1 2 3 4
3

Output:
2

Explanation:
There is one test case with array as {1, 2, 3 4} and element to be searched as 3.  Since 3 is present at index 2, output is 2

我的代码实现:

#include <iostream>

using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--){
        int N,j,X,index=-1;
        cin>>N;
        int *Arr=new int[N];
        for(j=0;j<N;j++)
        {
            cin>>Arr[j];
        }
        cin>>X;
        for(j=0;j<N;j++)
        {
            if(Arr[j]==X)
            {
                index=j;
                break;
            }
        }
        cout<<index<<endl;
    }
    return 0;
}

  

Search an Element in an array的更多相关文章

  1. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  2. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  3. 【刷题-LeetCode】215. Kth Largest Element in an Array

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  4. check the element in the array occurs more than half of the array length

    Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...

  5. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  6. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

  7. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  8. Majority Element in an Array

    Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...

  9. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

随机推荐

  1. Listview嵌套Listview

    今天做项目,打算模仿淘宝的订单管理,需要Listview嵌套Listview,都是两个控件都是沿着一个方向滑动的,嵌套在一起不幸福,以下是解决方案,打个笔记,以后估计还得用: 其中onMeasure函 ...

  2. Linux下安装Redis php-redis扩展 redis重启shell脚本 超详细!

    前言 前面刚写过nosql其中三款热门产品的对比,这次主要写关于Redis的一些事情,Redis的介绍.安装以及扩展(php-redis,因为我是phper)安装等等.同时是写给我的朋友(cccjjj ...

  3. 六、Hadoop学习笔记————调优之操作系统以及JVM

    内核参数overcommit_memory  它是 内存分配策略 可选值:0.1.2.0, 表示内核将检查是否有足够的可用内存供应用进程使用:如果有足够的可用内存,内存申请允许:否则,内存申请失败,并 ...

  4. opencv摄像头捕获图像

    #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace st ...

  5. 记一下flex弹性布局

    flex弹性布局也越来越广泛的在我们代码中出现了,更加方便我们的布局.自己用了查,查了用,有些还是记不住,俗话说好脑子不如烂笔头,原来都是写在本子上的,很不幸的一次次的想翻的时候总是找不到,还是写博客 ...

  6. C# Excel写入数据及图表

    开发工具:VS2017 语言:C DotNet版本:.Net FrameWork 4.0及以上 使用的DLL工具名称:GemBox.Spreadsheet.dll (版本:37.3.30.1185) ...

  7. Maven快速指南

    Maven是Apache基金会提供的项目管理工具, 其采用项目对象模型(Project Object Model, POM)描述项目配置, 并使用生命周期模型管理构建过程中各种操作. POM mave ...

  8. 程序员的自我救赎---3.1:理解Oauth2.0

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  9. 非常棒的教程记录(UML)

    这里暂且记录下看过的非常棒的博客吧! 来自 CSDN 几年前的博客专栏了,我只想说:经典实用的知识永远不会过时! http://blog.csdn.net/column/details/umlmode ...

  10. day1-Python入门

    百度云有关文档资料链接 链接:https://pan.baidu.com/s/1pLighnX 密码:j69s