As usual Babul is again back with his problem and now with numbers. He thought of an array of numbers in which he does two types of operation that is rotation and deletion. His process of doing these 2 operations are that he first rotates the array in a clockwise direction then delete the last element. In short he rotates the array nth times and then deletes the nth last element. If the nth last element does not exists then he deletes the first element present in the array. So your task is to find out which is the last element that he deletes from the array so that the array becomes empty after removing it.

For example
A = {1,2,3,4,5,6}.
He rotates the array clockwise i.e. after rotation the array A = {6,1,2,3,4,5} and delete the last element that is {5} so A = {6,1,2,3,4}. Again he rotates the array for the second time and deletes the second last element that is {2} so A = {4,6,1,3}, doing these steps when he reaches 4th time, 4th last element does not exists so he deletes 1st element ie {1} so A={3,6}. So continuing this procedure the last element in A is {3}, so o/p will be 3.
 
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line of each test case contains an integer N. Then in the next line are N space separated values of the array A.
 
Output:
For each test case in a new line print the required result.
 
Constraints:
1<=T<=200
1<=N<=100
1<=A[i]<=10^7
 
Example:
Input
2
4
1 2 3 4
6
1 2 3 4 5 6
Output:
2
3
### C++(gcc5.4)代码:
        #include <iostream>
        using namespace std;
        int main() {
        //code
       // define the number of test cases
       int T;
       cin>>T;
       
       for(int t=0; t<T; t++)
        {
           //get the two line input
           int N;
           cin>>N;
           int a[N];
           int i = 0;  
                for(i=0;i<N;i++)
               cin>>a[i];
        
                //Rotate and delete
                int index_delete = 1;
                int array_length = N;
                int tmp;
                while(array_length>1)  
                {
                    //Rotate
                    tmp = a[array_length - 1];
                    for(int j=array_length-1; j>0; j--)
                    {
                        a[j] = a[j-1];
                    }
                    a[0] = tmp;
                    
                    //delete
                    for(int k=array_length<index_delete?0:array_length-index_delete; k<array_length-1; k++)
                    {
                        a[k]=a[k+1];
                    }    
                    
                    index_delete += 1;
                    array_length -= 1; 
                }
                cout<<a[0]<<endl;
        }
       return 0;
        }
####注:更加严谨的将一行数字存入数组的代码如下,但是在测试时包含这部分代码的程序会提示超出时间限制!
·#include<iostream>  
using namespace std;  
int main()  
{  
    int a[50];  
    int i = 0;  
    char c;  
    while((c=getchar())!='\n')  
    {  
        if(c!=' ')//把这句判断条件改动  
        {  
            ungetc(c,stdin);  
            cin>>a[i++];  
        }  
    }  
    for(int j=0;j<i;j++)  
    {  
        cout<<"a["<<j<<"]:"<<a[j]<<endl;  
    }  
---
### python代码

geeksforgeeks-Array-Rotation and deletion的更多相关文章

  1. Data Structure Array: Program for array rotation

    http://www.geeksforgeeks.org/array-rotation/ O(n), O(1) #include <iostream> #include <vecto ...

  2. geeksforgeeks-Array-Rotate and delete

    As usual Babul is again back with his problem and now with numbers. He thought of an array of number ...

  3. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  4. geeksforgeeks@ Largest Number formed from an Array

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...

  5. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...

  6. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  7. Suffix array

    A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suf ...

  8. 算法最坏,平均和最佳情况(Worst, Average and Best Cases)-------geeksforgeeks 翻译

    最坏,平均和最佳运行时间(Worst, Average and Best Cases) 在上一篇文章中,我们讨论到了渐进分析可以解决分析算法的问题,那么在这一篇中,我们用线性搜索来举例说明一下如何用渐 ...

  9. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

随机推荐

  1. Launch4j Java 转可执行程序工具

    launch4j 可以用来将Java应用程序转成Windows本地可执行文件 (.exe).提供了本地弹出屏幕,应用程序图标,JRE搜索或使用绑定的JRE,启动失败反馈,传递命令行参数,ANT编译脚本 ...

  2. BZOJ3862Little Devil I——树链剖分+线段树

    题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...

  3. BZOJ1018[SHOI2008]堵塞的交通——线段树

    题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路,所以总 ...

  4. BZOJ3625 [Codeforces Round #250]小朋友和二叉树(生成函数+多项式开根)

    设f(n)为权值为n的神犇二叉树个数.考虑如何递推求这个东西. 套路地枚举根节点的左右子树.则f(n)=Σf(i)f(n-i-cj),cj即根的权值.卷积的形式,cj也可以通过卷上一个多项式枚举.可以 ...

  5. 关于min_25筛的一些理解

    关于min_25筛的一些理解 如果想看如何筛个普通积性函数啥的,就别往下看了,下面没有的(QwQ). 下文中,所有的\(p\)都代表质数,\(P\)代表质数集合. 注意下文中定义的最小/最大质因子都是 ...

  6. PKUWC 2019 记

     “连剑都插在了地上,可是我不应该就这么承认失败,想要到达山顶的人,不应该在山脚下就倒下啊” Day -5 (2019.1.15) 学考结束了,文化课暂停一段.早上飞机前往中山纪念中学.纪中好大呀,果 ...

  7. 笔记: c开发gui程序 (WM_CREATE, WS_CLIPCHILDREN , SetWindowPos)

    过去两年,用c写的gui程序我一般使用的套路是: 在 winMain()中, 先创建一个主窗口, 紧接着就是在下面创建子窗口(子控件). 可能是因为写这方面的程序较少,所以也没遇到什么大问题,之前就是 ...

  8. request请求地址

    1.String contextPath = httpServletRequest.getServletContext().getContextPath(); /项目名称 2.String conte ...

  9. powershell 删除8天前的日志

    把以下命令保存为ps1脚本,添加到Windows计划任务中设定每天固定时间执行即可: #delete logs in specify website, just save logs in eight ...

  10. 用Java实现几种常见的排序算法

    用Java语言实现的各种排序,包括插入排序.冒泡排序.选择排序.Shell排序.快速排序.归并排序.堆排序.SortUtil等. 插入排序: package org.rut.util.algorith ...